]> git.ipfire.org Git - thirdparty/u-boot.git/commitdiff
env: spi: Fix gd->env_valid for the first write
authorMichal Simek <michal.simek@amd.com>
Mon, 22 Sep 2025 16:03:39 +0000 (18:03 +0200)
committerTom Rini <trini@konsulko.com>
Thu, 2 Oct 2025 17:32:34 +0000 (11:32 -0600)
When both SPI environment locations are invalid (gd->env_valid ==
ENV_INVALID), the first call to saveenv writes to the primary location and
sets the active flag. However, the logic for updating gd->env_valid
incorrectly sets it to ENV_REDUND, which does not match the actual location
written. This causes the first two writes to target the same location, and
alternation only begins after the second write.

Update the logic to alternate gd->env_valid based on whether the last write
was to the primary or redundant location, ensuring the first write sets
ENV_VALID and subsequent writes alternate as expected. This aligns
env_valid with the actual storage location and fixes the alternation
sequence from the first write.

With this change, the "Valid environment" printout correctly reflects the
active location after each save, and the alternation between primary and
redundant locations works as intended from the start.

Signed-off-by: Michal Simek <michal.simek@amd.com>
Reviewed-by: Marek Vasut <marek.vasut@mailbox.org>
Acked-by: E Shattow <e@freeshell.de>
env/sf.c

index 0b70e18b9afafb79702aa50692b8563789aa00f9..0e27a020643edc2475bc920d3f5784070a9c8aba 100644 (file)
--- 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);