Skip to contentSkip to conversation

env: sf: single function env_sf_save()

PATCH New Harry Waschkeit 2 participants 10 messages Jan 28, 2021 – Feb 1, 2021

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 env: sf: single function env_sf_save()

Instead of implementing redundant environments in two very similar functions env_sf_save(), handle redundancy in one function, placing the few differences in appropriate pre-compiler sections depending on config option CONFIG_ENV_OFFSET_REDUND.

Additionally, several checkpatch complaints were addressed.

This patch is in preparation for adding support for env erase.

---
 env/sf.c | 132 ++++++++++++++++++-------------------------------------
 1 file changed, 43 insertions(+), 89 deletions(-)
env/sf.c +43 -89
index 937778aa37..c60bd1deed 100644
@@ -66,13 +66,16 @@ static int setup_flash_device(void)
 	return 0;
 }
 
-#if defined(CONFIG_ENV_OFFSET_REDUND)
 static int env_sf_save(void)
 {
 	env_t	env_new;
-	char	*saved_buffer = NULL, flag = ENV_REDUND_OBSOLETE;
+	char	*saved_buffer = NULL;
 	u32	saved_size, saved_offset, sector;
+	ulong	offset;
 	int	ret;
+#if CONFIG_IS_ENABLED(SYS_REDUNDAND_ENVIRONMENT)
+	char	flag = ENV_REDUND_OBSOLETE;
+#endif
 
 	ret = setup_flash_device();
 	if (ret)
@@ -81,27 +84,33 @@ static int env_sf_save(void)
 	ret = env_export(&env_new);
 	if (ret)
 		return -EIO;
-	env_new.flags	= ENV_REDUND_ACTIVE;
 
-	if (gd->env_valid == ENV_VALID) {
-		env_new_offset = CONFIG_ENV_OFFSET_REDUND;
-		env_offset = CONFIG_ENV_OFFSET;
-	} else {
-		env_new_offset = CONFIG_ENV_OFFSET;
-		env_offset = CONFIG_ENV_OFFSET_REDUND;
-	}
+#if CONFIG_IS_ENABLED(SYS_REDUNDAND_ENVIRONMENT)
+		env_new.flags	= ENV_REDUND_ACTIVE;
+
+		if (gd->env_valid == ENV_VALID) {
+			env_new_offset = CONFIG_ENV_OFFSET_REDUND;
+			env_offset = CONFIG_ENV_OFFSET;
+		} else {
+			env_new_offset = CONFIG_ENV_OFFSET;
+			env_offset = CONFIG_ENV_OFFSET_REDUND;
+		}
+		offset = env_new_offset;
+#else
+		offset = CONFIG_ENV_OFFSET;
+#endif
 
 	/* Is the sector larger than the env (i.e. embedded) */
 	if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
 		saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
-		saved_offset = env_new_offset + CONFIG_ENV_SIZE;
+		saved_offset = offset + CONFIG_ENV_SIZE;
 		saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
 		if (!saved_buffer) {
 			ret = -ENOMEM;
 			goto done;
 		}
-		ret = spi_flash_read(env_flash, saved_offset,
-					saved_size, saved_buffer);
+		ret = spi_flash_read(env_flash, saved_offset, saved_size,
+				     saved_buffer);
 		if (ret)
 			goto done;
 	}
@@ -109,35 +118,39 @@ static int env_sf_save(void)
 	sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
 
 	puts("Erasing SPI flash...");
-	ret = spi_flash_erase(env_flash, env_new_offset,
-				sector * CONFIG_ENV_SECT_SIZE);
+	ret = spi_flash_erase(env_flash, offset,
+			      sector * CONFIG_ENV_SECT_SIZE);
 	if (ret)
 		goto done;
 
 	puts("Writing to SPI flash...");
 
-	ret = spi_flash_write(env_flash, env_new_offset,
-		CONFIG_ENV_SIZE, &env_new);
+	ret = spi_flash_write(env_flash, offset,
+			      CONFIG_ENV_SIZE, &env_new);
 	if (ret)
 		goto done;
 
 	if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
-		ret = spi_flash_write(env_flash, saved_offset,
-					saved_size, saved_buffer);
+		ret = spi_flash_write(env_flash, saved_offset, saved_size,
+				      saved_buffer);
 		if (ret)
 			goto done;
 	}
 
-	ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
-				sizeof(env_new.flags), &flag);
-	if (ret)
-		goto done;
+#if CONFIG_IS_ENABLED(SYS_REDUNDAND_ENVIRONMENT)
+		ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
+				      sizeof(env_new.flags), &flag);
+		if (ret)
+			goto done;
 
-	puts("done\n");
+		puts("done\n");
 
-	gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
+		gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
 
-	printf("Valid environment: %d\n", (int)gd->env_valid);
+		printf("Valid environment: %d\n", (int)gd->env_valid);
+#else
+		puts("done\n");
+#endif
 
  done:
 	if (saved_buffer)
@@ -146,6 +159,7 @@ static int env_sf_save(void)
 	return ret;
 }
 
+#if CONFIG_IS_ENABLED(SYS_REDUNDAND_ENVIRONMENT)
 static int env_sf_load(void)
 {
 	int ret;
@@ -183,66 +197,6 @@ out:
 	return ret;
 }
 #else
-static int env_sf_save(void)
-{
-	u32	saved_size, saved_offset, sector;
-	char	*saved_buffer = NULL;
-	int	ret = 1;
-	env_t	env_new;
-
-	ret = setup_flash_device();
-	if (ret)
-		return ret;
-
-	/* Is the sector larger than the env (i.e. embedded) */
-	if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
-		saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
-		saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
-		saved_buffer = malloc(saved_size);
-		if (!saved_buffer)
-			goto done;
-
-		ret = spi_flash_read(env_flash, saved_offset,
-			saved_size, saved_buffer);
-		if (ret)
-			goto done;
-	}
-
-	ret = env_export(&env_new);
-	if (ret)
-		goto done;
-
-	sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
-
-	puts("Erasing SPI flash...");
-	ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
-		sector * CONFIG_ENV_SECT_SIZE);
-	if (ret)
-		goto done;
-
-	puts("Writing to SPI flash...");
-	ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
-		CONFIG_ENV_SIZE, &env_new);
-	if (ret)
-		goto done;
-
-	if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
-		ret = spi_flash_write(env_flash, saved_offset,
-			saved_size, saved_buffer);
-		if (ret)
-			goto done;
-	}
-
-	ret = 0;
-	puts("done\n");
-
- done:
-	if (saved_buffer)
-		free(saved_buffer);
-
-	return ret;
-}
-
 static int env_sf_load(void)
 {
 	int ret;
@@ -258,8 +212,8 @@ static int env_sf_load(void)
 	if (ret)
 		goto out;
 
-	ret = spi_flash_read(env_flash,
-		CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
+	ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE,
+			     buf);
 	if (ret) {
 		env_set_default("spi_flash_read() failed", 0);
 		goto err_read;
@@ -292,7 +246,7 @@ static int env_sf_init(void)
 	env_t *env_ptr = (env_t *)env_sf_get_env_addr();
 
 	if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
-		gd->env_addr	= (ulong)&(env_ptr->data);
+		gd->env_addr	= (ulong)&env_ptr->data;
 		gd->env_valid	= 1;
 	} else {
 		gd->env_addr = (ulong)&default_environment[0];
Signature · 1 line
2.29.0
#1 lore raw

PATCH 1/1 env: sf: single function env_sf_save()

Hi Harry,

Quoted 10 lines · Harry Waschkeit

Instead of implementing redundant environments in two very similar functions env_sf_save(), handle redundancy in one function, placing the few differences in appropriate pre-compiler sections depending on config option CONFIG_ENV_OFFSET_REDUND.

Additionally, several checkpatch complaints were addressed.

This patch is in preparation for adding support for env erase.

I like this idea very much, thanks for working on this. One comment below..

Quoted 14 lines
---
  env/sf.c | 132 ++++++++++++++++++-------------------------------------
  1 file changed, 43 insertions(+), 89 deletions(-)
env/sf.c +0 -1
index 937778aa37..c60bd1deed 100644
@@ -66,13 +66,16 @@ static int setup_flash_device(void)
  	return 0;
  }
 
-#if defined(CONFIG_ENV_OFFSET_REDUND)
  static int env_sf_save(void)

env/sf.c static int setup_flash_device(void) · line 78

Quoted 10 lines
env/sf.c +5 -1
  {
  	env_t	env_new;
-	char	*saved_buffer = NULL, flag = ENV_REDUND_OBSOLETE;
+	char	*saved_buffer = NULL;
  	u32	saved_size, saved_offset, sector;
+	ulong	offset;
  	int	ret;
+#if CONFIG_IS_ENABLED(SYS_REDUNDAND_ENVIRONMENT)
+	char	flag = ENV_REDUND_OBSOLETE;
+#endif

No more #idef's if possible please. See below...

Quoted 20 lines
env/sf.c +4 -8
  	ret = setup_flash_device();
  	if (ret)
@@ -81,27 +84,33 @@ static int env_sf_save(void)
  	ret = env_export(&env_new);
  	if (ret)
  		return -EIO;
-	env_new.flags	= ENV_REDUND_ACTIVE;
 
-	if (gd->env_valid == ENV_VALID) {
-		env_new_offset = CONFIG_ENV_OFFSET_REDUND;
-		env_offset = CONFIG_ENV_OFFSET;
-	} else {
-		env_new_offset = CONFIG_ENV_OFFSET;
-		env_offset = CONFIG_ENV_OFFSET_REDUND;
-	}
+#if CONFIG_IS_ENABLED(SYS_REDUNDAND_ENVIRONMENT)
+		env_new.flags	= ENV_REDUND_ACTIVE;
+
+		if (gd->env_valid == ENV_VALID) {

env/sf.c static int env_sf_save(void) · line 101

Quoted 10 lines
env/sf.c +10 -0
+			env_new_offset = CONFIG_ENV_OFFSET_REDUND;
+			env_offset = CONFIG_ENV_OFFSET;
+		} else {
+			env_new_offset = CONFIG_ENV_OFFSET;
+			env_offset = CONFIG_ENV_OFFSET_REDUND;
+		}
+		offset = env_new_offset;
+#else
+		offset = CONFIG_ENV_OFFSET;
+#endif

Can you please try to add this without adding #ifdef's but using if (CONFIG_IS_ENABLE(SYS_REDUNDAND_ENVIRONMENT)) instead?

Thanks,
Stefan

Quoted 159 lines
env/sf.c +23 -78
  	/* Is the sector larger than the env (i.e. embedded) */
  	if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
  		saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
-		saved_offset = env_new_offset + CONFIG_ENV_SIZE;
+		saved_offset = offset + CONFIG_ENV_SIZE;
  		saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
  		if (!saved_buffer) {
  			ret = -ENOMEM;
  			goto done;
  		}
-		ret = spi_flash_read(env_flash, saved_offset,
-					saved_size, saved_buffer);
+		ret = spi_flash_read(env_flash, saved_offset, saved_size,
+				     saved_buffer);
  		if (ret)
  			goto done;
  	}
@@ -109,35 +118,39 @@ static int env_sf_save(void)
  	sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
 
  	puts("Erasing SPI flash...");
-	ret = spi_flash_erase(env_flash, env_new_offset,
-				sector * CONFIG_ENV_SECT_SIZE);
+	ret = spi_flash_erase(env_flash, offset,
+			      sector * CONFIG_ENV_SECT_SIZE);
  	if (ret)
  		goto done;
 
  	puts("Writing to SPI flash...");
 
-	ret = spi_flash_write(env_flash, env_new_offset,
-		CONFIG_ENV_SIZE, &env_new);
+	ret = spi_flash_write(env_flash, offset,
+			      CONFIG_ENV_SIZE, &env_new);
  	if (ret)
  		goto done;
 
  	if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
-		ret = spi_flash_write(env_flash, saved_offset,
-					saved_size, saved_buffer);
+		ret = spi_flash_write(env_flash, saved_offset, saved_size,
+				      saved_buffer);
  		if (ret)
  			goto done;
  	}
 
-	ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
-				sizeof(env_new.flags), &flag);
-	if (ret)
-		goto done;
+#if CONFIG_IS_ENABLED(SYS_REDUNDAND_ENVIRONMENT)
+		ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
+				      sizeof(env_new.flags), &flag);
+		if (ret)
+			goto done;
 
-	puts("done\n");
+		puts("done\n");
 
-	gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
+		gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
 
-	printf("Valid environment: %d\n", (int)gd->env_valid);
+		printf("Valid environment: %d\n", (int)gd->env_valid);
+#else
+		puts("done\n");
+#endif
 
   done:
  	if (saved_buffer)
@@ -146,6 +159,7 @@ static int env_sf_save(void)
  	return ret;
  }
 
+#if CONFIG_IS_ENABLED(SYS_REDUNDAND_ENVIRONMENT)
  static int env_sf_load(void)
  {
  	int ret;
@@ -183,66 +197,6 @@ out:
  	return ret;
  }
  #else
-static int env_sf_save(void)
-{
-	u32	saved_size, saved_offset, sector;
-	char	*saved_buffer = NULL;
-	int	ret = 1;
-	env_t	env_new;
-
-	ret = setup_flash_device();
-	if (ret)
-		return ret;
-
-	/* Is the sector larger than the env (i.e. embedded) */
-	if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
-		saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
-		saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
-		saved_buffer = malloc(saved_size);
-		if (!saved_buffer)
-			goto done;
-
-		ret = spi_flash_read(env_flash, saved_offset,
-			saved_size, saved_buffer);
-		if (ret)
-			goto done;
-	}
-
-	ret = env_export(&env_new);
-	if (ret)
-		goto done;
-
-	sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
-
-	puts("Erasing SPI flash...");
-	ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
-		sector * CONFIG_ENV_SECT_SIZE);
-	if (ret)
-		goto done;
-
-	puts("Writing to SPI flash...");
-	ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
-		CONFIG_ENV_SIZE, &env_new);
-	if (ret)
-		goto done;
-
-	if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
-		ret = spi_flash_write(env_flash, saved_offset,
-			saved_size, saved_buffer);
-		if (ret)
-			goto done;
-	}
-
-	ret = 0;
-	puts("done\n");
-
- done:
-	if (saved_buffer)
-		free(saved_buffer);
-
-	return ret;
-}
-
  static int env_sf_load(void)
  {
  	int ret;
@@ -258,8 +212,8 @@ static int env_sf_load(void)
  	if (ret)
  		goto out;
 
-	ret = spi_flash_read(env_flash,
-		CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
+	ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE,
+			     buf);
  	if (ret) {
  		env_set_default("spi_flash_read() failed", 0);
  		goto err_read;

env/sf.c static int env_sf_init(void) · line 252

Quoted 8 lines
env/sf.c +1 -1
@@ -292,7 +246,7 @@ static int env_sf_init(void)
  	env_t *env_ptr = (env_t *)env_sf_get_env_addr();
 
  	if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
-		gd->env_addr	= (ulong)&(env_ptr->data);
+		gd->env_addr	= (ulong)&env_ptr->data;
  		gd->env_valid	= 1;
  	} else {
  		gd->env_addr = (ulong)&default_environment[0];

Viele Gr??e,
Stefan

Signature · 3 lines
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-51 Fax: (+49)-8142-66989-80 Email: sr at denx.de
#2 lore raw

PATCH 1/1 env: sf: single function env_sf_save()

?Hi Stefan,

thanks a lot for your prompt reply :-)

And sorry that I didn't manage to continue on that for such a long time ...

Quoted 34 lines · Stefan Roese

Hi Harry,

Quoted 10 lines · Harry Waschkeit

Instead of implementing redundant environments in two very similar functions env_sf_save(), handle redundancy in one function, placing the few differences in appropriate pre-compiler sections depending on config option CONFIG_ENV_OFFSET_REDUND.

Additionally, several checkpatch complaints were addressed.

This patch is in preparation for adding support for env erase.

I like this idea very much, thanks for working on this. One comment below..

Quoted 17 lines

---
? env/sf.c | 132 ++++++++++++++++++------------------------------------- ? 1 file changed, 43 insertions(+), 89 deletions(-)

env/sf.c +1 -2
index 937778aa37..c60bd1deed 100644
@@ -66,13 +66,16 @@ static int setup_flash_device(void)
 ????? return 0;
 ? }
-#if defined(CONFIG_ENV_OFFSET_REDUND)
 ? static int env_sf_save(void)
 ? {
 ????? env_t??? env_new;
-??? char??? *saved_buffer = NULL, flag = ENV_REDUND_OBSOLETE;
+??? char??? *saved_buffer = NULL;
Quoted 8 lines

env/sf.c static int setup_flash_device(void) · line 77

Quoted 6 lines
env/sf.c +4 -0
 ????? u32??? saved_size, saved_offset, sector;
+??? ulong??? offset;
 ????? int??? ret;
+#if CONFIG_IS_ENABLED(SYS_REDUNDAND_ENVIRONMENT)
+??? char??? flag = ENV_REDUND_OBSOLETE;
+#endif

No more #idef's if possible please. See below...

As checkpatch.pl throws warnings for each such #if[def] occurrence, I already tried to get rid of them. But I can't see how this shall be possible in a structure declaration.
I'm eager to learn how to do that if possible, though :-)

Quoted 24 lines
Quoted 24 lines
env/sf.c +9 -8
 ????? ret = setup_flash_device();
 ????? if (ret)
@@ -81,27 +84,33 @@ static int env_sf_save(void)
 ????? ret = env_export(&env_new);
 ????? if (ret)
 ????????? return -EIO;
-??? env_new.flags??? = ENV_REDUND_ACTIVE;
-??? if (gd->env_valid == ENV_VALID) {
-??????? env_new_offset = CONFIG_ENV_OFFSET_REDUND;
-??????? env_offset = CONFIG_ENV_OFFSET;
-??? } else {
-??????? env_new_offset = CONFIG_ENV_OFFSET;
-??????? env_offset = CONFIG_ENV_OFFSET_REDUND;
-??? }
+#if CONFIG_IS_ENABLED(SYS_REDUNDAND_ENVIRONMENT)
+??????? env_new.flags??? = ENV_REDUND_ACTIVE;
+
+??????? if (gd->env_valid == ENV_VALID) {
+??????????? env_new_offset = CONFIG_ENV_OFFSET_REDUND;
+??????????? env_offset = CONFIG_ENV_OFFSET;
+??????? } else {
+??????????? env_new_offset = CONFIG_ENV_OFFSET;
+??????????? env_offset = CONFIG_ENV_OFFSET_REDUND;
Quoted 8 lines

env/sf.c static int env_sf_save(void) · line 100

Quoted 5 lines
env/sf.c +5 -0
+??????? }
+??????? offset = env_new_offset;
+#else
+??????? offset = CONFIG_ENV_OFFSET;
+#endif

Can you please try to add this without adding #ifdef's but using if (CONFIG_IS_ENABLE(SYS_REDUNDAND_ENVIRONMENT)) instead?

I tried that, too, but it was doomed to fail, because element flags in env_t structure is only defined if redundant environment is enabled.

Even though an "if (CONFIG_IS_ENABLED(...))" would statically evaluate to '0' without active redundancy in environments, the parser sees the syntax error of the non-existing structure element.

So, I don't see a chance for avoiding the #if construct here, too. Let me know if I miss something :-)

Cheers,
Harry

Quoted 162 lines

Thanks,
Stefan

Quoted 146 lines

????? /* Is the sector larger than the env (i.e. embedded) */ ????? if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { ????????? saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE; -??????? saved_offset = env_new_offset + CONFIG_ENV_SIZE; +??????? saved_offset = offset + CONFIG_ENV_SIZE; ????????? saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size); ????????? if (!saved_buffer) {
????????????? ret = -ENOMEM;
????????????? goto done;
????????? }
-??????? ret = spi_flash_read(env_flash, saved_offset, -??????????????????? saved_size, saved_buffer); +??????? ret = spi_flash_read(env_flash, saved_offset, saved_size, +???????????????????? saved_buffer);
????????? if (ret)
????????????? goto done;
????? }

env/sf.c +17 -13
@@ -109,35 +118,39 @@ static int env_sf_save(void)
 ????? sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
 ????? puts("Erasing SPI flash...");
-??? ret = spi_flash_erase(env_flash, env_new_offset,
-??????????????? sector * CONFIG_ENV_SECT_SIZE);
+??? ret = spi_flash_erase(env_flash, offset,
+????????????????? sector * CONFIG_ENV_SECT_SIZE);
 ????? if (ret)
 ????????? goto done;
 ????? puts("Writing to SPI flash...");
-??? ret = spi_flash_write(env_flash, env_new_offset,
-??????? CONFIG_ENV_SIZE, &env_new);
+??? ret = spi_flash_write(env_flash, offset,
+????????????????? CONFIG_ENV_SIZE, &env_new);
 ????? if (ret)
 ????????? goto done;
 ????? if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
-??????? ret = spi_flash_write(env_flash, saved_offset,
-??????????????????? saved_size, saved_buffer);
+??????? ret = spi_flash_write(env_flash, saved_offset, saved_size,
+????????????????????? saved_buffer);
 ????????? if (ret)
 ????????????? goto done;
 ????? }
-??? ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
-??????????????? sizeof(env_new.flags), &flag);
-??? if (ret)
-??????? goto done;
+#if CONFIG_IS_ENABLED(SYS_REDUNDAND_ENVIRONMENT)
+??????? ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
+????????????????????? sizeof(env_new.flags), &flag);
+??????? if (ret)
+??????????? goto done;
-??? puts("done\n");
+??????? puts("done\n");
-??? gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
+??????? gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
-??? printf("Valid environment: %d\n", (int)gd->env_valid);
+??????? printf("Valid environment: %d\n", (int)gd->env_valid);
+#else
+??????? puts("done\n");
+#endif

?? done:
????? if (saved_buffer)

env/sf.c +1 -0
@@ -146,6 +159,7 @@ static int env_sf_save(void)
 ????? return ret;
 ? }
+#if CONFIG_IS_ENABLED(SYS_REDUNDAND_ENVIRONMENT)
 ? static int env_sf_load(void)
 ? {
 ????? int ret;
@@ -183,66 +197,6 @@ out:
 ????? return ret;
 ? }

? #else

env/sf.c +0 -60
-static int env_sf_save(void)
-{
-??? u32??? saved_size, saved_offset, sector;
-??? char??? *saved_buffer = NULL;
-??? int??? ret = 1;
-??? env_t??? env_new;
-
-??? ret = setup_flash_device();
-??? if (ret)
-??????? return ret;
-
-??? /* Is the sector larger than the env (i.e. embedded) */
-??? if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
-??????? saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
-??????? saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
-??????? saved_buffer = malloc(saved_size);
-??????? if (!saved_buffer)
-??????????? goto done;
-
-??????? ret = spi_flash_read(env_flash, saved_offset,
-??????????? saved_size, saved_buffer);
-??????? if (ret)
-??????????? goto done;
-??? }
-
-??? ret = env_export(&env_new);
-??? if (ret)
-??????? goto done;
-
-??? sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
-
-??? puts("Erasing SPI flash...");
-??? ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
-??????? sector * CONFIG_ENV_SECT_SIZE);
-??? if (ret)
-??????? goto done;
-
-??? puts("Writing to SPI flash...");
-??? ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
-??????? CONFIG_ENV_SIZE, &env_new);
-??? if (ret)
-??????? goto done;
-
-??? if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
-??????? ret = spi_flash_write(env_flash, saved_offset,
-??????????? saved_size, saved_buffer);
-??????? if (ret)
-??????????? goto done;
-??? }
-
-??? ret = 0;
-??? puts("done\n");
-
- done:
-??? if (saved_buffer)
-??????? free(saved_buffer);
-
-??? return ret;
-}
-

? static int env_sf_load(void)
? {
????? int ret;

env/sf.c +2 -2
@@ -258,8 +212,8 @@ static int env_sf_load(void)
 ????? if (ret)
 ????????? goto out;
-??? ret = spi_flash_read(env_flash,
-??????? CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
+??? ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE,
+???????????????? buf);
 ????? if (ret) {
 ????????? env_set_default("spi_flash_read() failed", 0);
 ????????? goto err_read;

env/sf.c static int env_sf_init(void) · line 252

Quoted 8 lines
env/sf.c +1 -1
@@ -292,7 +246,7 @@ static int env_sf_init(void)
 ????? env_t *env_ptr = (env_t *)env_sf_get_env_addr();
 ????? if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
-??????? gd->env_addr??? = (ulong)&(env_ptr->data);
+??????? gd->env_addr??? = (ulong)&env_ptr->data;
 ????????? gd->env_valid??? = 1;
 ????? } else {
 ????????? gd->env_addr = (ulong)&default_environment[0];
 

Viele Gr??e,
Stefan

Signature · 4 lines
Harry Waschkeit - Software Engineer
duagon Germany GmbH
Neuwieder Stra?e 1-7 90411 Nuernberg
Geschaeftsfuehrer: Dr. Michael Goldbach - Matthias Kamolz - Kalina Scott - Handelsregister AG Nuernberg HRB 5540
#3 lore raw

PATCH 1/1 env: sf: single function env_sf_save()

Hi Harry,

Quoted 51 lines · Harry Waschkeit

?Hi Stefan,

thanks a lot for your prompt reply :-)

And sorry that I didn't manage to continue on that for such a long time ...

Quoted 34 lines · Stefan Roese

Hi Harry,

Quoted 10 lines · Harry Waschkeit

Instead of implementing redundant environments in two very similar functions env_sf_save(), handle redundancy in one function, placing the few differences in appropriate pre-compiler sections depending on config option CONFIG_ENV_OFFSET_REDUND.

Additionally, several checkpatch complaints were addressed.

This patch is in preparation for adding support for env erase.

I like this idea very much, thanks for working on this. One comment below..

Quoted 17 lines

---
? env/sf.c | 132 ++++++++++++++++++------------------------------------- ? 1 file changed, 43 insertions(+), 89 deletions(-)

env/sf.c +1 -2
index 937778aa37..c60bd1deed 100644
@@ -66,13 +66,16 @@ static int setup_flash_device(void)
 ????? return 0;
 ? }
-#if defined(CONFIG_ENV_OFFSET_REDUND)
 ? static int env_sf_save(void)
 ? {
 ????? env_t??? env_new;
-??? char??? *saved_buffer = NULL, flag = ENV_REDUND_OBSOLETE;
+??? char??? *saved_buffer = NULL;
Quoted 8 lines

env/sf.c static int setup_flash_device(void) · line 77

Quoted 6 lines
env/sf.c +4 -0
 ????? u32??? saved_size, saved_offset, sector;
+??? ulong??? offset;
 ????? int??? ret;
+#if CONFIG_IS_ENABLED(SYS_REDUNDAND_ENVIRONMENT)
+??? char??? flag = ENV_REDUND_OBSOLETE;
+#endif

No more #idef's if possible please. See below...

Quoted 4 lines

As checkpatch.pl throws warnings for each such #if[def] occurrence, I already tried to get rid of them.
But I can't see how this shall be possible in a structure declaration. I'm eager to learn how to do that if possible, though :-)

Ah, right. It's probably not possible for this for a struct declaration. But the variable above can be included always, right?

Quoted 33 lines
Quoted 24 lines
Quoted 24 lines
env/sf.c +9 -8
 ????? ret = setup_flash_device();
 ????? if (ret)
@@ -81,27 +84,33 @@ static int env_sf_save(void)
 ????? ret = env_export(&env_new);
 ????? if (ret)
 ????????? return -EIO;
-??? env_new.flags??? = ENV_REDUND_ACTIVE;
-??? if (gd->env_valid == ENV_VALID) {
-??????? env_new_offset = CONFIG_ENV_OFFSET_REDUND;
-??????? env_offset = CONFIG_ENV_OFFSET;
-??? } else {
-??????? env_new_offset = CONFIG_ENV_OFFSET;
-??????? env_offset = CONFIG_ENV_OFFSET_REDUND;
-??? }
+#if CONFIG_IS_ENABLED(SYS_REDUNDAND_ENVIRONMENT)
+??????? env_new.flags??? = ENV_REDUND_ACTIVE;
+
+??????? if (gd->env_valid == ENV_VALID) {
+??????????? env_new_offset = CONFIG_ENV_OFFSET_REDUND;
+??????????? env_offset = CONFIG_ENV_OFFSET;
+??????? } else {
+??????????? env_new_offset = CONFIG_ENV_OFFSET;
+??????????? env_offset = CONFIG_ENV_OFFSET_REDUND;
Quoted 8 lines

env/sf.c static int env_sf_save(void) · line 100

Quoted 5 lines
env/sf.c +5 -0
+??????? }
+??????? offset = env_new_offset;
+#else
+??????? offset = CONFIG_ENV_OFFSET;
+#endif

Can you please try to add this without adding #ifdef's but using if (CONFIG_IS_ENABLE(SYS_REDUNDAND_ENVIRONMENT)) instead?

Quoted 2 lines

I tried that, too, but it was doomed to fail, because element flags in env_t structure is only defined if redundant environment is enabled.

Too bad we didn't include "flag" for non-redundant env in the beginning. Now its too late.

Quoted 6 lines

Even though an "if (CONFIG_IS_ENABLED(...))" would statically evaluate to '0' without active redundancy in environments, the parser sees the syntax error of the non-existing structure element.

So, I don't see a chance for avoiding the #if construct here, too. Let me know if I miss something :-)

I agree its not easy or perhaps even impossible. One idea would be to introduce a union in the struct with "flags" also defines in the non-redundant case but not being used here. But this would result in very non-intuitive code AFAICT. So better not go this way.

Perhaps somebody else has a quick idea on how to handle this without introduncing #ifdef's here?

Thanks,
Stefan

Quoted 171 lines

Cheers,
Harry

Quoted 166 lines

Thanks,
Stefan

Quoted 150 lines

????? /* Is the sector larger than the env (i.e. embedded) */ ????? if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { ????????? saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE; -??????? saved_offset = env_new_offset + CONFIG_ENV_SIZE; +??????? saved_offset = offset + CONFIG_ENV_SIZE; ????????? saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size); ????????? if (!saved_buffer) {
????????????? ret = -ENOMEM;
????????????? goto done;
????????? }
-??????? ret = spi_flash_read(env_flash, saved_offset, -??????????????????? saved_size, saved_buffer); +??????? ret = spi_flash_read(env_flash, saved_offset, saved_size, +???????????????????? saved_buffer);
????????? if (ret)
????????????? goto done;
????? }

env/sf.c +17 -13
@@ -109,35 +118,39 @@ static int env_sf_save(void)
 ????? sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
 ????? puts("Erasing SPI flash...");
-??? ret = spi_flash_erase(env_flash, env_new_offset,
-??????????????? sector * CONFIG_ENV_SECT_SIZE);
+??? ret = spi_flash_erase(env_flash, offset,
+????????????????? sector * CONFIG_ENV_SECT_SIZE);
 ????? if (ret)
 ????????? goto done;
 ????? puts("Writing to SPI flash...");
-??? ret = spi_flash_write(env_flash, env_new_offset,
-??????? CONFIG_ENV_SIZE, &env_new);
+??? ret = spi_flash_write(env_flash, offset,
+????????????????? CONFIG_ENV_SIZE, &env_new);
 ????? if (ret)
 ????????? goto done;
 ????? if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
-??????? ret = spi_flash_write(env_flash, saved_offset,
-??????????????????? saved_size, saved_buffer);
+??????? ret = spi_flash_write(env_flash, saved_offset, saved_size,
+????????????????????? saved_buffer);
 ????????? if (ret)
 ????????????? goto done;
 ????? }
-??? ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, 
 flags),
-??????????????? sizeof(env_new.flags), &flag);
-??? if (ret)
-??????? goto done;
+#if CONFIG_IS_ENABLED(SYS_REDUNDAND_ENVIRONMENT)
+??????? ret = spi_flash_write(env_flash, env_offset + 
 offsetof(env_t, flags),
+????????????????????? sizeof(env_new.flags), &flag);
+??????? if (ret)
+??????????? goto done;
-??? puts("done\n");
+??????? puts("done\n");
-??? gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : 
 ENV_REDUND;
+??????? gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : 
 ENV_REDUND;
-??? printf("Valid environment: %d\n", (int)gd->env_valid);
+??????? printf("Valid environment: %d\n", (int)gd->env_valid);
+#else
+??????? puts("done\n");
+#endif

?? done:
????? if (saved_buffer)

env/sf.c +1 -0
@@ -146,6 +159,7 @@ static int env_sf_save(void)
 ????? return ret;
 ? }
+#if CONFIG_IS_ENABLED(SYS_REDUNDAND_ENVIRONMENT)
 ? static int env_sf_load(void)
 ? {
 ????? int ret;
@@ -183,66 +197,6 @@ out:
 ????? return ret;
 ? }

? #else

env/sf.c +0 -60
-static int env_sf_save(void)
-{
-??? u32??? saved_size, saved_offset, sector;
-??? char??? *saved_buffer = NULL;
-??? int??? ret = 1;
-??? env_t??? env_new;
-
-??? ret = setup_flash_device();
-??? if (ret)
-??????? return ret;
-
-??? /* Is the sector larger than the env (i.e. embedded) */
-??? if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
-??????? saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
-??????? saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
-??????? saved_buffer = malloc(saved_size);
-??????? if (!saved_buffer)
-??????????? goto done;
-
-??????? ret = spi_flash_read(env_flash, saved_offset,
-??????????? saved_size, saved_buffer);
-??????? if (ret)
-??????????? goto done;
-??? }
-
-??? ret = env_export(&env_new);
-??? if (ret)
-??????? goto done;
-
-??? sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
-
-??? puts("Erasing SPI flash...");
-??? ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
-??????? sector * CONFIG_ENV_SECT_SIZE);
-??? if (ret)
-??????? goto done;
-
-??? puts("Writing to SPI flash...");
-??? ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
-??????? CONFIG_ENV_SIZE, &env_new);
-??? if (ret)
-??????? goto done;
-
-??? if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
-??????? ret = spi_flash_write(env_flash, saved_offset,
-??????????? saved_size, saved_buffer);
-??????? if (ret)
-??????????? goto done;
-??? }
-
-??? ret = 0;
-??? puts("done\n");
-
- done:
-??? if (saved_buffer)
-??????? free(saved_buffer);
-
-??? return ret;
-}
-

? static int env_sf_load(void)
? {
????? int ret;

env/sf.c +2 -2
@@ -258,8 +212,8 @@ static int env_sf_load(void)
 ????? if (ret)
 ????????? goto out;
-??? ret = spi_flash_read(env_flash,
-??????? CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
+??? ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE,
+???????????????? buf);
 ????? if (ret) {
 ????????? env_set_default("spi_flash_read() failed", 0);
 ????????? goto err_read;

env/sf.c static int env_sf_init(void) · line 252

Quoted 8 lines
env/sf.c +1 -1
@@ -292,7 +246,7 @@ static int env_sf_init(void)
 ????? env_t *env_ptr = (env_t *)env_sf_get_env_addr();
 ????? if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
-??????? gd->env_addr??? = (ulong)&(env_ptr->data);
+??????? gd->env_addr??? = (ulong)&env_ptr->data;
 ????????? gd->env_valid??? = 1;
 ????? } else {
 ????????? gd->env_addr = (ulong)&default_environment[0];
 

Viele Gr??e,
Stefan

Viele Gr??e,
Stefan

Signature · 3 lines
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-51 Fax: (+49)-8142-66989-80 Email: sr at denx.de
#4 lore raw

PATCH 1/1 env: sf: single function env_sf_save()

Quoted 54 lines · Stefan Roese

Hi Harry,

Quoted 51 lines · Harry Waschkeit

?Hi Stefan,

thanks a lot for your prompt reply :-)

And sorry that I didn't manage to continue on that for such a long time ...

Quoted 42 lines · Stefan Roese

Hi Harry,

Quoted 10 lines · Harry Waschkeit

Instead of implementing redundant environments in two very similar functions env_sf_save(), handle redundancy in one function, placing the few differences in appropriate pre-compiler sections depending on config option CONFIG_ENV_OFFSET_REDUND.

Additionally, several checkpatch complaints were addressed.

This patch is in preparation for adding support for env erase.

I like this idea very much, thanks for working on this. One comment below..

Quoted 13 lines

---
? env/sf.c | 132 ++++++++++++++++++------------------------------------- ? 1 file changed, 43 insertions(+), 89 deletions(-)

env/sf.c +0 -1
index 937778aa37..c60bd1deed 100644
@@ -66,13 +66,16 @@ static int setup_flash_device(void)
 ????? return 0;
 ? }
-#if defined(CONFIG_ENV_OFFSET_REDUND)
 ? static int env_sf_save(void)

env/sf.c static int setup_flash_device(void) · line 77

Quoted 10 lines
env/sf.c +5 -1
 ? {
 ????? env_t??? env_new;
-??? char??? *saved_buffer = NULL, flag = ENV_REDUND_OBSOLETE;
+??? char??? *saved_buffer = NULL;
 ????? u32??? saved_size, saved_offset, sector;
+??? ulong??? offset;
 ????? int??? ret;
+#if CONFIG_IS_ENABLED(SYS_REDUNDAND_ENVIRONMENT)
+??? char??? flag = ENV_REDUND_OBSOLETE;
+#endif

No more #idef's if possible please. See below...

Quoted 6 lines
Quoted 3 lines

As checkpatch.pl throws warnings for each such #if[def] occurrence, I already tried to get rid of them. But I can't see how this shall be possible in a structure declaration.
I'm eager to learn how to do that if possible, though :-)

Ah, right. It's probably not possible for this for a struct declaration. But the variable above can be included always, right?

Sure it could, but then we would have to live with a compiler warning about an unused variable I think.

Quoted 33 lines
Quoted 33 lines
Quoted 32 lines
Quoted 19 lines
env/sf.c +4 -8
 ????? ret = setup_flash_device();
 ????? if (ret)
@@ -81,27 +84,33 @@ static int env_sf_save(void)
 ????? ret = env_export(&env_new);
 ????? if (ret)
 ????????? return -EIO;
-??? env_new.flags??? = ENV_REDUND_ACTIVE;
-??? if (gd->env_valid == ENV_VALID) {
-??????? env_new_offset = CONFIG_ENV_OFFSET_REDUND;
-??????? env_offset = CONFIG_ENV_OFFSET;
-??? } else {
-??????? env_new_offset = CONFIG_ENV_OFFSET;
-??????? env_offset = CONFIG_ENV_OFFSET_REDUND;
-??? }
+#if CONFIG_IS_ENABLED(SYS_REDUNDAND_ENVIRONMENT)
+??????? env_new.flags??? = ENV_REDUND_ACTIVE;
+
+??????? if (gd->env_valid == ENV_VALID) {

env/sf.c static int env_sf_save(void) · line 100

Quoted 10 lines
env/sf.c +10 -0
+??????????? env_new_offset = CONFIG_ENV_OFFSET_REDUND;
+??????????? env_offset = CONFIG_ENV_OFFSET;
+??????? } else {
+??????????? env_new_offset = CONFIG_ENV_OFFSET;
+??????????? env_offset = CONFIG_ENV_OFFSET_REDUND;
+??????? }
+??????? offset = env_new_offset;
+#else
+??????? offset = CONFIG_ENV_OFFSET;
+#endif

Can you please try to add this without adding #ifdef's but using if (CONFIG_IS_ENABLE(SYS_REDUNDAND_ENVIRONMENT)) instead?

Quoted 4 lines
Quoted 1 line

I tried that, too, but it was doomed to fail, because element flags in env_t structure is only defined if redundant environment is enabled.

Too bad we didn't include "flag" for non-redundant env in the beginning. Now its too late.

That would probably have been the best solution to avoid #if's, at least in that file.

But keep in mind: as a last consequence of this approach you would end up in structures bearing variables for all eventualities, however unlikely these are needed, blowing up the memory footprint for no reason.

In my opinion such things need to be thought through thoroughly :-)

Quoted 9 lines
Quoted 4 lines

Even though an "if (CONFIG_IS_ENABLED(...))" would statically evaluate to '0' without active redundancy in environments, the parser sees the syntax error of the non-existing structure element.

So, I don't see a chance for avoiding the #if construct here, too. Let me know if I miss something :-)

I agree its not easy or perhaps even impossible. One idea would be to introduce a union in the struct with "flags" also defines in the non-redundant case but not being used here. But this would result in very non-intuitive code AFAICT. So better not go this way.

That would feel very strange to me, too.

Quoted 2 lines

Perhaps somebody else has a quick idea on how to handle this without introduncing #ifdef's here?

It would be possible to introduce a macro like env_set_flags(envp, flag) in env.h that only evaluates to something in case of active redundancy, sure.

But that's not even half of a solution, because also variables env_offset and env_new_offset which are set in that section are only defined if necessary, i.e. if CONFIG_SYS_REDUNDAND_ENVIRONMENT is set (btw also protected by #if while not possible otherwise).

The trade-off here is between code duplication - which I removed by combining two very similar separate functions for the two situations w/ and w/o redundancy in one - and in-line pre-compiler protected regions within one function.

While I fully agree that the latter should be avoided as far as possible, it is not avoidable here afaics.

And avoiding code duplication here outweighs the few pre-compiler occurrences in my eyes, but that's just my ? 0,02 :-)

Viele Gr??e,
Harry

Quoted 176 lines

Thanks,
Stefan

Quoted 167 lines

Cheers,
Harry

Quoted 162 lines

Thanks,
Stefan

Quoted 146 lines

????? /* Is the sector larger than the env (i.e. embedded) */ ????? if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { ????????? saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE; -??????? saved_offset = env_new_offset + CONFIG_ENV_SIZE; +??????? saved_offset = offset + CONFIG_ENV_SIZE; ????????? saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size); ????????? if (!saved_buffer) {
????????????? ret = -ENOMEM;
????????????? goto done;
????????? }
-??????? ret = spi_flash_read(env_flash, saved_offset, -??????????????????? saved_size, saved_buffer); +??????? ret = spi_flash_read(env_flash, saved_offset, saved_size, +???????????????????? saved_buffer);
????????? if (ret)
????????????? goto done;
????? }

env/sf.c +17 -13
@@ -109,35 +118,39 @@ static int env_sf_save(void)
 ????? sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
 ????? puts("Erasing SPI flash...");
-??? ret = spi_flash_erase(env_flash, env_new_offset,
-??????????????? sector * CONFIG_ENV_SECT_SIZE);
+??? ret = spi_flash_erase(env_flash, offset,
+????????????????? sector * CONFIG_ENV_SECT_SIZE);
 ????? if (ret)
 ????????? goto done;
 ????? puts("Writing to SPI flash...");
-??? ret = spi_flash_write(env_flash, env_new_offset,
-??????? CONFIG_ENV_SIZE, &env_new);
+??? ret = spi_flash_write(env_flash, offset,
+????????????????? CONFIG_ENV_SIZE, &env_new);
 ????? if (ret)
 ????????? goto done;
 ????? if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
-??????? ret = spi_flash_write(env_flash, saved_offset,
-??????????????????? saved_size, saved_buffer);
+??????? ret = spi_flash_write(env_flash, saved_offset, saved_size,
+????????????????????? saved_buffer);
 ????????? if (ret)
 ????????????? goto done;
 ????? }
-??? ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
-??????????????? sizeof(env_new.flags), &flag);
-??? if (ret)
-??????? goto done;
+#if CONFIG_IS_ENABLED(SYS_REDUNDAND_ENVIRONMENT)
+??????? ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
+????????????????????? sizeof(env_new.flags), &flag);
+??????? if (ret)
+??????????? goto done;
-??? puts("done\n");
+??????? puts("done\n");
-??? gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
+??????? gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
-??? printf("Valid environment: %d\n", (int)gd->env_valid);
+??????? printf("Valid environment: %d\n", (int)gd->env_valid);
+#else
+??????? puts("done\n");
+#endif

?? done:
????? if (saved_buffer)

env/sf.c +1 -0
@@ -146,6 +159,7 @@ static int env_sf_save(void)
 ????? return ret;
 ? }
+#if CONFIG_IS_ENABLED(SYS_REDUNDAND_ENVIRONMENT)
 ? static int env_sf_load(void)
 ? {
 ????? int ret;
@@ -183,66 +197,6 @@ out:
 ????? return ret;
 ? }

? #else

env/sf.c +0 -60
-static int env_sf_save(void)
-{
-??? u32??? saved_size, saved_offset, sector;
-??? char??? *saved_buffer = NULL;
-??? int??? ret = 1;
-??? env_t??? env_new;
-
-??? ret = setup_flash_device();
-??? if (ret)
-??????? return ret;
-
-??? /* Is the sector larger than the env (i.e. embedded) */
-??? if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
-??????? saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
-??????? saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
-??????? saved_buffer = malloc(saved_size);
-??????? if (!saved_buffer)
-??????????? goto done;
-
-??????? ret = spi_flash_read(env_flash, saved_offset,
-??????????? saved_size, saved_buffer);
-??????? if (ret)
-??????????? goto done;
-??? }
-
-??? ret = env_export(&env_new);
-??? if (ret)
-??????? goto done;
-
-??? sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
-
-??? puts("Erasing SPI flash...");
-??? ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
-??????? sector * CONFIG_ENV_SECT_SIZE);
-??? if (ret)
-??????? goto done;
-
-??? puts("Writing to SPI flash...");
-??? ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
-??????? CONFIG_ENV_SIZE, &env_new);
-??? if (ret)
-??????? goto done;
-
-??? if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
-??????? ret = spi_flash_write(env_flash, saved_offset,
-??????????? saved_size, saved_buffer);
-??????? if (ret)
-??????????? goto done;
-??? }
-
-??? ret = 0;
-??? puts("done\n");
-
- done:
-??? if (saved_buffer)
-??????? free(saved_buffer);
-
-??? return ret;
-}
-

? static int env_sf_load(void)
? {
????? int ret;

env/sf.c +2 -2
@@ -258,8 +212,8 @@ static int env_sf_load(void)
 ????? if (ret)
 ????????? goto out;
-??? ret = spi_flash_read(env_flash,
-??????? CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
+??? ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE,
+???????????????? buf);
 ????? if (ret) {
 ????????? env_set_default("spi_flash_read() failed", 0);
 ????????? goto err_read;

env/sf.c static int env_sf_init(void) · line 252

Quoted 8 lines
env/sf.c +1 -1
@@ -292,7 +246,7 @@ static int env_sf_init(void)
 ????? env_t *env_ptr = (env_t *)env_sf_get_env_addr();
 ????? if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
-??????? gd->env_addr??? = (ulong)&(env_ptr->data);
+??????? gd->env_addr??? = (ulong)&env_ptr->data;
 ????????? gd->env_valid??? = 1;
 ????? } else {
 ????????? gd->env_addr = (ulong)&default_environment[0];
 

Viele Gr??e,
Stefan

Viele Gr??e,
Stefan

Signature · 1 line
Harry Waschkeit - Software Engineer
#5 lore raw

PATCH 1/1 env: sf: single function env_sf_save()

On 28.01.21 12:21, Harry Waschkeit wrote:

<snip>

Quoted 31 lines
Quoted 11 lines
Quoted 6 lines

Even though an "if (CONFIG_IS_ENABLED(...))" would statically evaluate to '0' without active redundancy in environments, the parser sees the syntax error of the non-existing structure element.

So, I don't see a chance for avoiding the #if construct here, too. Let me know if I miss something :-)

I agree its not easy or perhaps even impossible. One idea would be to introduce a union in the struct with "flags" also defines in the non-redundant case but not being used here. But this would result in very non-intuitive code AFAICT. So better not go this way.

That would feel very strange to me, too.

Quoted 2 lines

Perhaps somebody else has a quick idea on how to handle this without introduncing #ifdef's here?

It would be possible to introduce a macro like env_set_flags(envp, flag) in env.h that only evaluates to something in case of active redundancy, sure.

But that's not even half of a solution, because also variables env_offset and env_new_offset which are set in that section are only defined if necessary, i.e. if CONFIG_SYS_REDUNDAND_ENVIRONMENT is set (btw also protected by #if while not possible otherwise).

The trade-off here is between code duplication - which I removed by combining two very similar separate functions for the two situations w/ and w/o redundancy in one - and in-line pre-compiler protected regions within one function.

Quoted 5 lines

While I fully agree that the latter should be avoided as far as possible, it is not avoidable here afaics.

And avoiding code duplication here outweighs the few pre-compiler occurrences in my eyes, but that's just my ? 0,02 :-)

I also agree (now). If nobody else comes up with a better idea, then please proceed with the current implementation to remove the code duplication.

Thanks,
Stefan

Quoted 187 lines

Viele Gr??e,
Harry

Quoted 181 lines

Thanks,
Stefan

Quoted 172 lines

Cheers,
Harry

Quoted 167 lines

Thanks,
Stefan

Quoted 151 lines

????? /* Is the sector larger than the env (i.e. embedded) */ ????? if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { ????????? saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE; -??????? saved_offset = env_new_offset + CONFIG_ENV_SIZE; +??????? saved_offset = offset + CONFIG_ENV_SIZE; ????????? saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size); ????????? if (!saved_buffer) {
????????????? ret = -ENOMEM;
????????????? goto done;
????????? }
-??????? ret = spi_flash_read(env_flash, saved_offset, -??????????????????? saved_size, saved_buffer); +??????? ret = spi_flash_read(env_flash, saved_offset, saved_size, +???????????????????? saved_buffer);
????????? if (ret)
????????????? goto done;
????? }

(quoted diff) +17 -13
@@ -109,35 +118,39 @@ static int env_sf_save(void)
 ????? sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
 ????? puts("Erasing SPI flash...");
-??? ret = spi_flash_erase(env_flash, env_new_offset,
-??????????????? sector * CONFIG_ENV_SECT_SIZE);
+??? ret = spi_flash_erase(env_flash, offset,
+????????????????? sector * CONFIG_ENV_SECT_SIZE);
 ????? if (ret)
 ????????? goto done;
 ????? puts("Writing to SPI flash...");
-??? ret = spi_flash_write(env_flash, env_new_offset,
-??????? CONFIG_ENV_SIZE, &env_new);
+??? ret = spi_flash_write(env_flash, offset,
+????????????????? CONFIG_ENV_SIZE, &env_new);
 ????? if (ret)
 ????????? goto done;
 ????? if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
-??????? ret = spi_flash_write(env_flash, saved_offset,
-??????????????????? saved_size, saved_buffer);
+??????? ret = spi_flash_write(env_flash, saved_offset, saved_size,
+????????????????????? saved_buffer);
 ????????? if (ret)
 ????????????? goto done;
 ????? }
-??? ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, 
 flags),
-??????????????? sizeof(env_new.flags), &flag);
-??? if (ret)
-??????? goto done;
+#if CONFIG_IS_ENABLED(SYS_REDUNDAND_ENVIRONMENT)
+??????? ret = spi_flash_write(env_flash, env_offset + 
 offsetof(env_t, flags),
+????????????????????? sizeof(env_new.flags), &flag);
+??????? if (ret)
+??????????? goto done;
-??? puts("done\n");
+??????? puts("done\n");
-??? gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : 
 ENV_REDUND;
+??????? gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : 
 ENV_REDUND;
-??? printf("Valid environment: %d\n", (int)gd->env_valid);
+??????? printf("Valid environment: %d\n", (int)gd->env_valid);
+#else
+??????? puts("done\n");
+#endif

?? done:
????? if (saved_buffer)

(quoted diff) +1 -0
@@ -146,6 +159,7 @@ static int env_sf_save(void)
 ????? return ret;
 ? }
+#if CONFIG_IS_ENABLED(SYS_REDUNDAND_ENVIRONMENT)
 ? static int env_sf_load(void)
 ? {
 ????? int ret;
@@ -183,66 +197,6 @@ out:
 ????? return ret;
 ? }

? #else

(quoted diff) +0 -60
-static int env_sf_save(void)
-{
-??? u32??? saved_size, saved_offset, sector;
-??? char??? *saved_buffer = NULL;
-??? int??? ret = 1;
-??? env_t??? env_new;
-
-??? ret = setup_flash_device();
-??? if (ret)
-??????? return ret;
-
-??? /* Is the sector larger than the env (i.e. embedded) */
-??? if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
-??????? saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
-??????? saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
-??????? saved_buffer = malloc(saved_size);
-??????? if (!saved_buffer)
-??????????? goto done;
-
-??????? ret = spi_flash_read(env_flash, saved_offset,
-??????????? saved_size, saved_buffer);
-??????? if (ret)
-??????????? goto done;
-??? }
-
-??? ret = env_export(&env_new);
-??? if (ret)
-??????? goto done;
-
-??? sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
-
-??? puts("Erasing SPI flash...");
-??? ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
-??????? sector * CONFIG_ENV_SECT_SIZE);
-??? if (ret)
-??????? goto done;
-
-??? puts("Writing to SPI flash...");
-??? ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
-??????? CONFIG_ENV_SIZE, &env_new);
-??? if (ret)
-??????? goto done;
-
-??? if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
-??????? ret = spi_flash_write(env_flash, saved_offset,
-??????????? saved_size, saved_buffer);
-??????? if (ret)
-??????????? goto done;
-??? }
-
-??? ret = 0;
-??? puts("done\n");
-
- done:
-??? if (saved_buffer)
-??????? free(saved_buffer);
-
-??? return ret;
-}
-

? static int env_sf_load(void)
? {
????? int ret;

(quoted diff) +2 -2
@@ -258,8 +212,8 @@ static int env_sf_load(void)
 ????? if (ret)
 ????????? goto out;
-??? ret = spi_flash_read(env_flash,
-??????? CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
+??? ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET, 
 CONFIG_ENV_SIZE,
+???????????????? buf);
 ????? if (ret) {
 ????????? env_set_default("spi_flash_read() failed", 0);
 ????????? goto err_read;

Review static int env_sf_init(void) · line 252

Quoted 8 lines
(quoted diff) +1 -1
@@ -292,7 +246,7 @@ static int env_sf_init(void)
 ????? env_t *env_ptr = (env_t *)env_sf_get_env_addr();
 ????? if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
-??????? gd->env_addr??? = (ulong)&(env_ptr->data);
+??????? gd->env_addr??? = (ulong)&env_ptr->data;
 ????????? gd->env_valid??? = 1;
 ????? } else {
 ????????? gd->env_addr = (ulong)&default_environment[0];
 

Viele Gr??e,
Stefan

Viele Gr??e,
Stefan

Viele Gr??e,
Stefan

Signature · 3 lines
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-51 Fax: (+49)-8142-66989-80 Email: sr at denx.de
#6 lore raw

PATCH 1/1 env: sf: single function env_sf_save()

Hi again Stefan,

Quoted 25 lines · Stefan Roese

On 28.01.21 12:21, Harry Waschkeit wrote:

<snip>

Quoted 21 lines
Quoted 9 lines
Quoted 4 lines

Even though an "if (CONFIG_IS_ENABLED(...))" would statically evaluate to '0' without active redundancy in environments, the parser sees the syntax error of the non-existing structure element.

So, I don't see a chance for avoiding the #if construct here, too. Let me know if I miss something :-)

I agree its not easy or perhaps even impossible. One idea would be to introduce a union in the struct with "flags" also defines in the non-redundant case but not being used here. But this would result in very non-intuitive code AFAICT. So better not go this way.

That would feel very strange to me, too.

Quoted 2 lines

Perhaps somebody else has a quick idea on how to handle this without introduncing #ifdef's here?

It would be possible to introduce a macro like env_set_flags(envp, flag) in env.h that only evaluates to something in case of active redundancy, sure.

But that's not even half of a solution, because also variables env_offset and env_new_offset which are set in that section are only defined if necessary, i.e. if CONFIG_SYS_REDUNDAND_ENVIRONMENT is set (btw also protected by #if while not possible otherwise).

The trade-off here is between code duplication - which I removed by combining two very similar separate functions for the two situations w/ and w/o redundancy in one - and in-line pre-compiler protected regions within one function.

Quoted 7 lines
Quoted 3 lines

While I fully agree that the latter should be avoided as far as possible, it is not avoidable here afaics.

And avoiding code duplication here outweighs the few pre-compiler occurrences in my eyes, but that's just my ? 0,02 :-)

I also agree (now). If nobody else comes up with a better idea, then please proceed with the current implementation to remove the code duplication.

sorry for the newbie question, I'm not familiar (yet) with the normal procedure and I don't want to keep that ball from rolling ;-/

As far as I understood you finally agreed on my patch, but you didn't give it a "reviewed-by" all the same; so do you still expect me to change my patch in some way or are you waiting for a "reviewed-by" from someone else to give that a go?

Why I am also asking: I have another small patch in he pipe that bases on the changed sources and that adds "env erase" support on top of that :-)

Thanks,
Harry

Quoted 191 lines

Thanks,
Stefan

Quoted 182 lines

Viele Gr??e,
Harry

Quoted 176 lines

Thanks,
Stefan

Quoted 167 lines

Cheers,
Harry

Quoted 162 lines

Thanks,
Stefan

Quoted 146 lines

????? /* Is the sector larger than the env (i.e. embedded) */ ????? if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { ????????? saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE; -??????? saved_offset = env_new_offset + CONFIG_ENV_SIZE; +??????? saved_offset = offset + CONFIG_ENV_SIZE; ????????? saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size); ????????? if (!saved_buffer) {
????????????? ret = -ENOMEM;
????????????? goto done;
????????? }
-??????? ret = spi_flash_read(env_flash, saved_offset, -??????????????????? saved_size, saved_buffer); +??????? ret = spi_flash_read(env_flash, saved_offset, saved_size, +???????????????????? saved_buffer);
????????? if (ret)
????????????? goto done;
????? }

(quoted diff) +17 -13
@@ -109,35 +118,39 @@ static int env_sf_save(void)
 ????? sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
 ????? puts("Erasing SPI flash...");
-??? ret = spi_flash_erase(env_flash, env_new_offset,
-??????????????? sector * CONFIG_ENV_SECT_SIZE);
+??? ret = spi_flash_erase(env_flash, offset,
+????????????????? sector * CONFIG_ENV_SECT_SIZE);
 ????? if (ret)
 ????????? goto done;
 ????? puts("Writing to SPI flash...");
-??? ret = spi_flash_write(env_flash, env_new_offset,
-??????? CONFIG_ENV_SIZE, &env_new);
+??? ret = spi_flash_write(env_flash, offset,
+????????????????? CONFIG_ENV_SIZE, &env_new);
 ????? if (ret)
 ????????? goto done;
 ????? if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
-??????? ret = spi_flash_write(env_flash, saved_offset,
-??????????????????? saved_size, saved_buffer);
+??????? ret = spi_flash_write(env_flash, saved_offset, saved_size,
+????????????????????? saved_buffer);
 ????????? if (ret)
 ????????????? goto done;
 ????? }
-??? ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
-??????????????? sizeof(env_new.flags), &flag);
-??? if (ret)
-??????? goto done;
+#if CONFIG_IS_ENABLED(SYS_REDUNDAND_ENVIRONMENT)
+??????? ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
+????????????????????? sizeof(env_new.flags), &flag);
+??????? if (ret)
+??????????? goto done;
-??? puts("done\n");
+??????? puts("done\n");
-??? gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
+??????? gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
-??? printf("Valid environment: %d\n", (int)gd->env_valid);
+??????? printf("Valid environment: %d\n", (int)gd->env_valid);
+#else
+??????? puts("done\n");
+#endif

?? done:
????? if (saved_buffer)

(quoted diff) +1 -0
@@ -146,6 +159,7 @@ static int env_sf_save(void)
 ????? return ret;
 ? }
+#if CONFIG_IS_ENABLED(SYS_REDUNDAND_ENVIRONMENT)
 ? static int env_sf_load(void)
 ? {
 ????? int ret;
@@ -183,66 +197,6 @@ out:
 ????? return ret;
 ? }

? #else

(quoted diff) +0 -60
-static int env_sf_save(void)
-{
-??? u32??? saved_size, saved_offset, sector;
-??? char??? *saved_buffer = NULL;
-??? int??? ret = 1;
-??? env_t??? env_new;
-
-??? ret = setup_flash_device();
-??? if (ret)
-??????? return ret;
-
-??? /* Is the sector larger than the env (i.e. embedded) */
-??? if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
-??????? saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
-??????? saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
-??????? saved_buffer = malloc(saved_size);
-??????? if (!saved_buffer)
-??????????? goto done;
-
-??????? ret = spi_flash_read(env_flash, saved_offset,
-??????????? saved_size, saved_buffer);
-??????? if (ret)
-??????????? goto done;
-??? }
-
-??? ret = env_export(&env_new);
-??? if (ret)
-??????? goto done;
-
-??? sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
-
-??? puts("Erasing SPI flash...");
-??? ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
-??????? sector * CONFIG_ENV_SECT_SIZE);
-??? if (ret)
-??????? goto done;
-
-??? puts("Writing to SPI flash...");
-??? ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
-??????? CONFIG_ENV_SIZE, &env_new);
-??? if (ret)
-??????? goto done;
-
-??? if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
-??????? ret = spi_flash_write(env_flash, saved_offset,
-??????????? saved_size, saved_buffer);
-??????? if (ret)
-??????????? goto done;
-??? }
-
-??? ret = 0;
-??? puts("done\n");
-
- done:
-??? if (saved_buffer)
-??????? free(saved_buffer);
-
-??? return ret;
-}
-

? static int env_sf_load(void)
? {
????? int ret;

(quoted diff) +2 -2
@@ -258,8 +212,8 @@ static int env_sf_load(void)
 ????? if (ret)
 ????????? goto out;
-??? ret = spi_flash_read(env_flash,
-??????? CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
+??? ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE,
+???????????????? buf);
 ????? if (ret) {
 ????????? env_set_default("spi_flash_read() failed", 0);
 ????????? goto err_read;

Review static int env_sf_init(void) · line 252

Quoted 8 lines
(quoted diff) +1 -1
@@ -292,7 +246,7 @@ static int env_sf_init(void)
 ????? env_t *env_ptr = (env_t *)env_sf_get_env_addr();
 ????? if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
-??????? gd->env_addr??? = (ulong)&(env_ptr->data);
+??????? gd->env_addr??? = (ulong)&env_ptr->data;
 ????????? gd->env_valid??? = 1;
 ????? } else {
 ????????? gd->env_addr = (ulong)&default_environment[0];
 

Viele Gr??e,
Stefan

Viele Gr??e,
Stefan

Viele Gr??e,
Stefan

Signature · 1 line
Harry Waschkeit - Software Engineer
#7 lore raw

PATCH 1/1 env: sf: single function env_sf_save()

Hi Harry,

Quoted 44 lines · Harry Waschkeit

Hi again Stefan,

Quoted 41 lines · Stefan Roese

On 28.01.21 12:21, Harry Waschkeit wrote:

<snip>

Quoted 36 lines
Quoted 11 lines
Quoted 6 lines

Even though an "if (CONFIG_IS_ENABLED(...))" would statically evaluate to '0' without active redundancy in environments, the parser sees the syntax error of the non-existing structure element.

So, I don't see a chance for avoiding the #if construct here, too. Let me know if I miss something :-)

I agree its not easy or perhaps even impossible. One idea would be to introduce a union in the struct with "flags" also defines in the non-redundant case but not being used here. But this would result in very non-intuitive code AFAICT. So better not go this way.

That would feel very strange to me, too.

Quoted 2 lines

Perhaps somebody else has a quick idea on how to handle this without introduncing #ifdef's here?

It would be possible to introduce a macro like env_set_flags(envp, flag) in env.h that only evaluates to something in case of active redundancy, sure.

But that's not even half of a solution, because also variables env_offset and env_new_offset which are set in that section are only defined if necessary, i.e. if CONFIG_SYS_REDUNDAND_ENVIRONMENT is set (btw also protected by #if while not possible otherwise).

The trade-off here is between code duplication - which I removed by combining two very similar separate functions for the two situations w/ and w/o redundancy in one - and in-line pre-compiler protected regions within one function.

While I fully agree that the latter should be avoided as far as possible, it is not avoidable here afaics.

And avoiding code duplication here outweighs the few pre-compiler occurrences in my eyes, but that's just my ? 0,02 :-)

Quoted 6 lines
Quoted 3 lines

I also agree (now). If nobody else comes up with a better idea, then please proceed with the current implementation to remove the code duplication.

sorry for the newbie question, I'm not familiar (yet) with the normal procedure and I don't want to keep that ball from rolling ;-/

No Problem. Please double-check if not 100% sure. ;)

Quoted 7 lines

As far as I understood you finally agreed on my patch, but you didn't give it a "reviewed-by" all the same; so do you still expect me to change my patch in some way or are you waiting for a "reviewed-by" from someone else to give that a go?

Why I am also asking: I have another small patch in he pipe that bases on the changed sources and that adds "env erase" support on top of that :-)

I didn't do a thorough review yet. Let me try to do this quickly...

Thanks,
Stefan

Quoted 199 lines

Thanks,
Harry

Quoted 196 lines

Thanks,
Stefan

Quoted 187 lines

Viele Gr??e,
Harry

Quoted 181 lines

Thanks,
Stefan

Quoted 172 lines

Cheers,
Harry

Quoted 167 lines

Thanks,
Stefan

Quoted 151 lines

????? /* Is the sector larger than the env (i.e. embedded) */ ????? if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { ????????? saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE; -??????? saved_offset = env_new_offset + CONFIG_ENV_SIZE; +??????? saved_offset = offset + CONFIG_ENV_SIZE; ????????? saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size); ????????? if (!saved_buffer) {
????????????? ret = -ENOMEM;
????????????? goto done;
????????? }
-??????? ret = spi_flash_read(env_flash, saved_offset, -??????????????????? saved_size, saved_buffer); +??????? ret = spi_flash_read(env_flash, saved_offset, saved_size, +???????????????????? saved_buffer);
????????? if (ret)
????????????? goto done;
????? }

(quoted diff) +17 -13
@@ -109,35 +118,39 @@ static int env_sf_save(void)
 ????? sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
 ????? puts("Erasing SPI flash...");
-??? ret = spi_flash_erase(env_flash, env_new_offset,
-??????????????? sector * CONFIG_ENV_SECT_SIZE);
+??? ret = spi_flash_erase(env_flash, offset,
+????????????????? sector * CONFIG_ENV_SECT_SIZE);
 ????? if (ret)
 ????????? goto done;
 ????? puts("Writing to SPI flash...");
-??? ret = spi_flash_write(env_flash, env_new_offset,
-??????? CONFIG_ENV_SIZE, &env_new);
+??? ret = spi_flash_write(env_flash, offset,
+????????????????? CONFIG_ENV_SIZE, &env_new);
 ????? if (ret)
 ????????? goto done;
 ????? if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
-??????? ret = spi_flash_write(env_flash, saved_offset,
-??????????????????? saved_size, saved_buffer);
+??????? ret = spi_flash_write(env_flash, saved_offset, saved_size,
+????????????????????? saved_buffer);
 ????????? if (ret)
 ????????????? goto done;
 ????? }
-??? ret = spi_flash_write(env_flash, env_offset + 
 offsetof(env_t, flags),
-??????????????? sizeof(env_new.flags), &flag);
-??? if (ret)
-??????? goto done;
+#if CONFIG_IS_ENABLED(SYS_REDUNDAND_ENVIRONMENT)
+??????? ret = spi_flash_write(env_flash, env_offset + 
 offsetof(env_t, flags),
+????????????????????? sizeof(env_new.flags), &flag);
+??????? if (ret)
+??????????? goto done;
-??? puts("done\n");
+??????? puts("done\n");
-??? gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : 
 ENV_REDUND;
+??????? gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID 
 : ENV_REDUND;
-??? printf("Valid environment: %d\n", (int)gd->env_valid);
+??????? printf("Valid environment: %d\n", (int)gd->env_valid);
+#else
+??????? puts("done\n");
+#endif

?? done:
????? if (saved_buffer)

(quoted diff) +1 -0
@@ -146,6 +159,7 @@ static int env_sf_save(void)
 ????? return ret;
 ? }
+#if CONFIG_IS_ENABLED(SYS_REDUNDAND_ENVIRONMENT)
 ? static int env_sf_load(void)
 ? {
 ????? int ret;
@@ -183,66 +197,6 @@ out:
 ????? return ret;
 ? }

? #else

(quoted diff) +0 -60
-static int env_sf_save(void)
-{
-??? u32??? saved_size, saved_offset, sector;
-??? char??? *saved_buffer = NULL;
-??? int??? ret = 1;
-??? env_t??? env_new;
-
-??? ret = setup_flash_device();
-??? if (ret)
-??????? return ret;
-
-??? /* Is the sector larger than the env (i.e. embedded) */
-??? if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
-??????? saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
-??????? saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
-??????? saved_buffer = malloc(saved_size);
-??????? if (!saved_buffer)
-??????????? goto done;
-
-??????? ret = spi_flash_read(env_flash, saved_offset,
-??????????? saved_size, saved_buffer);
-??????? if (ret)
-??????????? goto done;
-??? }
-
-??? ret = env_export(&env_new);
-??? if (ret)
-??????? goto done;
-
-??? sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
-
-??? puts("Erasing SPI flash...");
-??? ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
-??????? sector * CONFIG_ENV_SECT_SIZE);
-??? if (ret)
-??????? goto done;
-
-??? puts("Writing to SPI flash...");
-??? ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
-??????? CONFIG_ENV_SIZE, &env_new);
-??? if (ret)
-??????? goto done;
-
-??? if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
-??????? ret = spi_flash_write(env_flash, saved_offset,
-??????????? saved_size, saved_buffer);
-??????? if (ret)
-??????????? goto done;
-??? }
-
-??? ret = 0;
-??? puts("done\n");
-
- done:
-??? if (saved_buffer)
-??????? free(saved_buffer);
-
-??? return ret;
-}
-

? static int env_sf_load(void)
? {
????? int ret;

(quoted diff) +2 -2
@@ -258,8 +212,8 @@ static int env_sf_load(void)
 ????? if (ret)
 ????????? goto out;
-??? ret = spi_flash_read(env_flash,
-??????? CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
+??? ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET, 
 CONFIG_ENV_SIZE,
+???????????????? buf);
 ????? if (ret) {
 ????????? env_set_default("spi_flash_read() failed", 0);
 ????????? goto err_read;

Review static int env_sf_init(void) · line 252

Quoted 8 lines
(quoted diff) +1 -1
@@ -292,7 +246,7 @@ static int env_sf_init(void)
 ????? env_t *env_ptr = (env_t *)env_sf_get_env_addr();
 ????? if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
-??????? gd->env_addr??? = (ulong)&(env_ptr->data);
+??????? gd->env_addr??? = (ulong)&env_ptr->data;
 ????????? gd->env_valid??? = 1;
 ????? } else {
 ????????? gd->env_addr = (ulong)&default_environment[0];
 

Viele Gr??e,
Stefan

Viele Gr??e,
Stefan

Viele Gr??e,
Stefan

Viele Gr??e,
Stefan

Signature · 3 lines
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-51 Fax: (+49)-8142-66989-80 Email: sr at denx.de
#8 lore raw

PATCH 1/1 env: sf: single function env_sf_save()

In reply to Harry Waschkeit

Hi Harry,

Quoted 5 lines · Harry Waschkeit

Instead of implementing redundant environments in two very similar functions env_sf_save(), handle redundancy in one function, placing the few differences in appropriate pre-compiler sections depending on config option CONFIG_ENV_OFFSET_REDUND.

Quoted 8 lines

Additionally, several checkpatch complaints were addressed.

This patch is in preparation for adding support for env erase.

---
  env/sf.c | 132 ++++++++++++++++++-------------------------------------
  1 file changed, 43 insertions(+), 89 deletions(-)

Nice diffstat. ;)

Quoted 11 lines
env/sf.c +0 -1
index 937778aa37..c60bd1deed 100644
@@ -66,13 +66,16 @@ static int setup_flash_device(void)
  	return 0;
  }
 
-#if defined(CONFIG_ENV_OFFSET_REDUND)
  static int env_sf_save(void)

env/sf.c static int setup_flash_device(void) · line 78

Quoted 10 lines
env/sf.c +5 -1
  {
  	env_t	env_new;
-	char	*saved_buffer = NULL, flag = ENV_REDUND_OBSOLETE;
+	char	*saved_buffer = NULL;
  	u32	saved_size, saved_offset, sector;
+	ulong	offset;
  	int	ret;
+#if CONFIG_IS_ENABLED(SYS_REDUNDAND_ENVIRONMENT)
+	char	flag = ENV_REDUND_OBSOLETE;
+#endif

I would prefer this to the #ifdef here:

	__maybe_unused char flag = ENV_REDUND_OBSOLETE;

Looks good aside from this:

Thanks,
Stefan

Quoted 189 lines
env/sf.c +37 -86
  	ret = setup_flash_device();
  	if (ret)
@@ -81,27 +84,33 @@ static int env_sf_save(void)
  	ret = env_export(&env_new);
  	if (ret)
  		return -EIO;
-	env_new.flags	= ENV_REDUND_ACTIVE;
 
-	if (gd->env_valid == ENV_VALID) {
-		env_new_offset = CONFIG_ENV_OFFSET_REDUND;
-		env_offset = CONFIG_ENV_OFFSET;
-	} else {
-		env_new_offset = CONFIG_ENV_OFFSET;
-		env_offset = CONFIG_ENV_OFFSET_REDUND;
-	}
+#if CONFIG_IS_ENABLED(SYS_REDUNDAND_ENVIRONMENT)
+		env_new.flags	= ENV_REDUND_ACTIVE;
+
+		if (gd->env_valid == ENV_VALID) {
+			env_new_offset = CONFIG_ENV_OFFSET_REDUND;
+			env_offset = CONFIG_ENV_OFFSET;
+		} else {
+			env_new_offset = CONFIG_ENV_OFFSET;
+			env_offset = CONFIG_ENV_OFFSET_REDUND;
+		}
+		offset = env_new_offset;
+#else
+		offset = CONFIG_ENV_OFFSET;
+#endif
 
  	/* Is the sector larger than the env (i.e. embedded) */
  	if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
  		saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
-		saved_offset = env_new_offset + CONFIG_ENV_SIZE;
+		saved_offset = offset + CONFIG_ENV_SIZE;
  		saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
  		if (!saved_buffer) {
  			ret = -ENOMEM;
  			goto done;
  		}
-		ret = spi_flash_read(env_flash, saved_offset,
-					saved_size, saved_buffer);
+		ret = spi_flash_read(env_flash, saved_offset, saved_size,
+				     saved_buffer);
  		if (ret)
  			goto done;
  	}
@@ -109,35 +118,39 @@ static int env_sf_save(void)
  	sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
 
  	puts("Erasing SPI flash...");
-	ret = spi_flash_erase(env_flash, env_new_offset,
-				sector * CONFIG_ENV_SECT_SIZE);
+	ret = spi_flash_erase(env_flash, offset,
+			      sector * CONFIG_ENV_SECT_SIZE);
  	if (ret)
  		goto done;
 
  	puts("Writing to SPI flash...");
 
-	ret = spi_flash_write(env_flash, env_new_offset,
-		CONFIG_ENV_SIZE, &env_new);
+	ret = spi_flash_write(env_flash, offset,
+			      CONFIG_ENV_SIZE, &env_new);
  	if (ret)
  		goto done;
 
  	if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
-		ret = spi_flash_write(env_flash, saved_offset,
-					saved_size, saved_buffer);
+		ret = spi_flash_write(env_flash, saved_offset, saved_size,
+				      saved_buffer);
  		if (ret)
  			goto done;
  	}
 
-	ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
-				sizeof(env_new.flags), &flag);
-	if (ret)
-		goto done;
+#if CONFIG_IS_ENABLED(SYS_REDUNDAND_ENVIRONMENT)
+		ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
+				      sizeof(env_new.flags), &flag);
+		if (ret)
+			goto done;
 
-	puts("done\n");
+		puts("done\n");
 
-	gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
+		gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
 
-	printf("Valid environment: %d\n", (int)gd->env_valid);
+		printf("Valid environment: %d\n", (int)gd->env_valid);
+#else
+		puts("done\n");
+#endif
 
   done:
  	if (saved_buffer)
@@ -146,6 +159,7 @@ static int env_sf_save(void)
  	return ret;
  }
 
+#if CONFIG_IS_ENABLED(SYS_REDUNDAND_ENVIRONMENT)
  static int env_sf_load(void)
  {
  	int ret;
@@ -183,66 +197,6 @@ out:
  	return ret;
  }
  #else
-static int env_sf_save(void)
-{
-	u32	saved_size, saved_offset, sector;
-	char	*saved_buffer = NULL;
-	int	ret = 1;
-	env_t	env_new;
-
-	ret = setup_flash_device();
-	if (ret)
-		return ret;
-
-	/* Is the sector larger than the env (i.e. embedded) */
-	if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
-		saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
-		saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
-		saved_buffer = malloc(saved_size);
-		if (!saved_buffer)
-			goto done;
-
-		ret = spi_flash_read(env_flash, saved_offset,
-			saved_size, saved_buffer);
-		if (ret)
-			goto done;
-	}
-
-	ret = env_export(&env_new);
-	if (ret)
-		goto done;
-
-	sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
-
-	puts("Erasing SPI flash...");
-	ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
-		sector * CONFIG_ENV_SECT_SIZE);
-	if (ret)
-		goto done;
-
-	puts("Writing to SPI flash...");
-	ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
-		CONFIG_ENV_SIZE, &env_new);
-	if (ret)
-		goto done;
-
-	if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
-		ret = spi_flash_write(env_flash, saved_offset,
-			saved_size, saved_buffer);
-		if (ret)
-			goto done;
-	}
-
-	ret = 0;
-	puts("done\n");
-
- done:
-	if (saved_buffer)
-		free(saved_buffer);
-
-	return ret;
-}
-
  static int env_sf_load(void)
  {
  	int ret;
@@ -258,8 +212,8 @@ static int env_sf_load(void)
  	if (ret)
  		goto out;
 
-	ret = spi_flash_read(env_flash,
-		CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
+	ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE,
+			     buf);
  	if (ret) {
  		env_set_default("spi_flash_read() failed", 0);
  		goto err_read;

env/sf.c static int env_sf_init(void) · line 252

Quoted 8 lines
env/sf.c +1 -1
@@ -292,7 +246,7 @@ static int env_sf_init(void)
  	env_t *env_ptr = (env_t *)env_sf_get_env_addr();
 
  	if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
-		gd->env_addr	= (ulong)&(env_ptr->data);
+		gd->env_addr	= (ulong)&env_ptr->data;
  		gd->env_valid	= 1;
  	} else {
  		gd->env_addr = (ulong)&default_environment[0];

Viele Gr??e,
Stefan

Signature · 3 lines
DENX Software Engineering GmbH,      Managing Director: Wolfgang Denk
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-51 Fax: (+49)-8142-66989-80 Email: sr at denx.de
#9 lore raw

PATCH 1/1 env: sf: single function env_sf_save()

?

Quoted 1 line · Stefan Roese

Hi Harry,

Hi Stefan,

Quoted 40 lines · Harry Waschkeit
Quoted 5 lines

Instead of implementing redundant environments in two very similar functions env_sf_save(), handle redundancy in one function, placing the few differences in appropriate pre-compiler sections depending on config option CONFIG_ENV_OFFSET_REDUND.

Quoted 8 lines

Additionally, several checkpatch complaints were addressed.

This patch is in preparation for adding support for env erase.

---
? env/sf.c | 132 ++++++++++++++++++------------------------------------- ? 1 file changed, 43 insertions(+), 89 deletions(-)

Nice diffstat. ;)

Quoted 10 lines
env/sf.c +0 -1
index 937778aa37..c60bd1deed 100644
@@ -66,13 +66,16 @@ static int setup_flash_device(void)
 ????? return 0;
 ? }
-#if defined(CONFIG_ENV_OFFSET_REDUND)
 ? static int env_sf_save(void)

env/sf.c static int setup_flash_device(void) · line 77

Quoted 10 lines
env/sf.c +5 -1
 ? {
 ????? env_t??? env_new;
-??? char??? *saved_buffer = NULL, flag = ENV_REDUND_OBSOLETE;
+??? char??? *saved_buffer = NULL;
 ????? u32??? saved_size, saved_offset, sector;
+??? ulong??? offset;
 ????? int??? ret;
+#if CONFIG_IS_ENABLED(SYS_REDUNDAND_ENVIRONMENT)
+??? char??? flag = ENV_REDUND_OBSOLETE;
+#endif

I would prefer this to the #ifdef here:

Quoted 1 line

????__maybe_unused char flag = ENV_REDUND_OBSOLETE;

Ok, will change that and send a v2.

Quoted 3 lines

Looks good aside from this:

Thanks for your review! :-)

Cheers,
Harry

Quoted 190 lines

Thanks,
Stefan

Quoted 174 lines
env/sf.c +14 -8
 ????? ret = setup_flash_device();
 ????? if (ret)
@@ -81,27 +84,33 @@ static int env_sf_save(void)
 ????? ret = env_export(&env_new);
 ????? if (ret)
 ????????? return -EIO;
-??? env_new.flags??? = ENV_REDUND_ACTIVE;
-??? if (gd->env_valid == ENV_VALID) {
-??????? env_new_offset = CONFIG_ENV_OFFSET_REDUND;
-??????? env_offset = CONFIG_ENV_OFFSET;
-??? } else {
-??????? env_new_offset = CONFIG_ENV_OFFSET;
-??????? env_offset = CONFIG_ENV_OFFSET_REDUND;
-??? }
+#if CONFIG_IS_ENABLED(SYS_REDUNDAND_ENVIRONMENT)
+??????? env_new.flags??? = ENV_REDUND_ACTIVE;
+
+??????? if (gd->env_valid == ENV_VALID) {
+??????????? env_new_offset = CONFIG_ENV_OFFSET_REDUND;
+??????????? env_offset = CONFIG_ENV_OFFSET;
+??????? } else {
+??????????? env_new_offset = CONFIG_ENV_OFFSET;
+??????????? env_offset = CONFIG_ENV_OFFSET_REDUND;
+??????? }
+??????? offset = env_new_offset;
+#else
+??????? offset = CONFIG_ENV_OFFSET;
+#endif

????? /* Is the sector larger than the env (i.e. embedded) */ ????? if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) { ????????? saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE; -??????? saved_offset = env_new_offset + CONFIG_ENV_SIZE; +??????? saved_offset = offset + CONFIG_ENV_SIZE; ????????? saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size); ????????? if (!saved_buffer) {
????????????? ret = -ENOMEM;
????????????? goto done;
????????? }
-??????? ret = spi_flash_read(env_flash, saved_offset, -??????????????????? saved_size, saved_buffer); +??????? ret = spi_flash_read(env_flash, saved_offset, saved_size, +???????????????????? saved_buffer);
????????? if (ret)
????????????? goto done;
????? }

env/sf.c +17 -13
@@ -109,35 +118,39 @@ static int env_sf_save(void)
 ????? sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
 ????? puts("Erasing SPI flash...");
-??? ret = spi_flash_erase(env_flash, env_new_offset,
-??????????????? sector * CONFIG_ENV_SECT_SIZE);
+??? ret = spi_flash_erase(env_flash, offset,
+????????????????? sector * CONFIG_ENV_SECT_SIZE);
 ????? if (ret)
 ????????? goto done;
 ????? puts("Writing to SPI flash...");
-??? ret = spi_flash_write(env_flash, env_new_offset,
-??????? CONFIG_ENV_SIZE, &env_new);
+??? ret = spi_flash_write(env_flash, offset,
+????????????????? CONFIG_ENV_SIZE, &env_new);
 ????? if (ret)
 ????????? goto done;
 ????? if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
-??????? ret = spi_flash_write(env_flash, saved_offset,
-??????????????????? saved_size, saved_buffer);
+??????? ret = spi_flash_write(env_flash, saved_offset, saved_size,
+????????????????????? saved_buffer);
 ????????? if (ret)
 ????????????? goto done;
 ????? }
-??? ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
-??????????????? sizeof(env_new.flags), &flag);
-??? if (ret)
-??????? goto done;
+#if CONFIG_IS_ENABLED(SYS_REDUNDAND_ENVIRONMENT)
+??????? ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
+????????????????????? sizeof(env_new.flags), &flag);
+??????? if (ret)
+??????????? goto done;
-??? puts("done\n");
+??????? puts("done\n");
-??? gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
+??????? gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
-??? printf("Valid environment: %d\n", (int)gd->env_valid);
+??????? printf("Valid environment: %d\n", (int)gd->env_valid);
+#else
+??????? puts("done\n");
+#endif

?? done:
????? if (saved_buffer)

env/sf.c +1 -0
@@ -146,6 +159,7 @@ static int env_sf_save(void)
 ????? return ret;
 ? }
+#if CONFIG_IS_ENABLED(SYS_REDUNDAND_ENVIRONMENT)
 ? static int env_sf_load(void)
 ? {
 ????? int ret;
@@ -183,66 +197,6 @@ out:
 ????? return ret;
 ? }

? #else

env/sf.c +0 -60
-static int env_sf_save(void)
-{
-??? u32??? saved_size, saved_offset, sector;
-??? char??? *saved_buffer = NULL;
-??? int??? ret = 1;
-??? env_t??? env_new;
-
-??? ret = setup_flash_device();
-??? if (ret)
-??????? return ret;
-
-??? /* Is the sector larger than the env (i.e. embedded) */
-??? if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
-??????? saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
-??????? saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
-??????? saved_buffer = malloc(saved_size);
-??????? if (!saved_buffer)
-??????????? goto done;
-
-??????? ret = spi_flash_read(env_flash, saved_offset,
-??????????? saved_size, saved_buffer);
-??????? if (ret)
-??????????? goto done;
-??? }
-
-??? ret = env_export(&env_new);
-??? if (ret)
-??????? goto done;
-
-??? sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, CONFIG_ENV_SECT_SIZE);
-
-??? puts("Erasing SPI flash...");
-??? ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
-??????? sector * CONFIG_ENV_SECT_SIZE);
-??? if (ret)
-??????? goto done;
-
-??? puts("Writing to SPI flash...");
-??? ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
-??????? CONFIG_ENV_SIZE, &env_new);
-??? if (ret)
-??????? goto done;
-
-??? if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
-??????? ret = spi_flash_write(env_flash, saved_offset,
-??????????? saved_size, saved_buffer);
-??????? if (ret)
-??????????? goto done;
-??? }
-
-??? ret = 0;
-??? puts("done\n");
-
- done:
-??? if (saved_buffer)
-??????? free(saved_buffer);
-
-??? return ret;
-}
-

? static int env_sf_load(void)
? {
????? int ret;

env/sf.c +2 -2
@@ -258,8 +212,8 @@ static int env_sf_load(void)
 ????? if (ret)
 ????????? goto out;
-??? ret = spi_flash_read(env_flash,
-??????? CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
+??? ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE,
+???????????????? buf);
 ????? if (ret) {
 ????????? env_set_default("spi_flash_read() failed", 0);
 ????????? goto err_read;

env/sf.c static int env_sf_init(void) · line 252

Quoted 8 lines
env/sf.c +1 -1
@@ -292,7 +246,7 @@ static int env_sf_init(void)
 ????? env_t *env_ptr = (env_t *)env_sf_get_env_addr();
 ????? if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
-??????? gd->env_addr??? = (ulong)&(env_ptr->data);
+??????? gd->env_addr??? = (ulong)&env_ptr->data;
 ????????? gd->env_valid??? = 1;
 ????? } else {
 ????????? gd->env_addr = (ulong)&default_environment[0];
 

Viele Gr??e,
Stefan

Signature · 4 lines
Harry Waschkeit - Software Engineer
duagon Germany GmbH
Neuwieder Stra?e 1-7 90411 Nuernberg
Geschaeftsfuehrer: Dr. Michael Goldbach - Matthias Kamolz - Kalina Scott - Handelsregister AG Nuernberg HRB 5540