[PATCH v4] env: spi: Fix gd->env_valid for the first write
Michal Simek
michal.simek at amd.com
Thu Oct 2 11:34:35 CEST 2025
Update the environment location selection logic to perform round-robin
selection of the environment location based on whether the last write from
a call to saveenv was to the primary or redundant location. Ensure the
initial call to saveenv correctly sets ENV_VALID so the subsequent calls to
saveenv follow the round-robin pattern as expected. This keeps
gd->env_valid consistent with the actual storage location and fixes the
sequence from the very first write.
Prior to this fix the round-robin assignment of locations would begin only
after the second call to saveenv.
With this change, the "Valid environment" printout correctly reflects
the active location after each save, and round-robin switching between
primary and redundant locations works as intended from the start.
This corrects the existing behavior that when both SPI environment
locations are invalid (gd->env_valid == ENV_INVALID), updating
gd->env_valid is incorrectly assigned as ENV_REDUND and allowed the
subsequent call to saveenv to target the same location as the first call.
Note that data write operations are not duplicated or striped between
locations for any single call to saveenv. The location switching is
being done between completed saveenv operations.
Signed-off-by: Michal Simek <michal.simek at amd.com>
---
Changes in v4:
- Polish commit message again
- Marek: I have removed your tag because description has changed. Feel free
to send it again on this version.
Changes in v3:
- Polish commit message
Changes in v2:
- Simplify commit message
v1: https://lore.kernel.org/r/03aa59f72f47fe0ecafd1e1ca52cbb8a5a8f0446.1755613540.git.michal.simek@amd.com
v2: https://lore.kernel.org/r/7a6ffee423f45507af680c63a86cc50fb9e3a5df.1757489188.git.michal.simek@amd.com
v3: https://lore.kernel.org/r/7a6ffee423f45507af680c63a86cc50fb9e3a5df.1757489188.git.michal.simek@amd.com
---
env/sf.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/env/sf.c b/env/sf.c
index 0b70e18b9afa..0e27a020643e 100644
--- a/env/sf.c
+++ b/env/sf.c
@@ -148,7 +148,7 @@ static int env_sf_save(void)
puts("done\n");
- gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
+ gd->env_valid = gd->env_valid == ENV_VALID ? ENV_REDUND : ENV_VALID;
printf("Valid environment: %d\n", (int)gd->env_valid);
--
2.43.0
base-commit: cf8e61f78690f9081f335180607eabdca9970a4f
branch: debian-sent3
More information about the U-Boot
mailing list