Skip to contentSkip to conversation

env: Access Environment in SPI flashes before relocation

PATCH Superseded Heiko Schocher 2 participants 7 messages Nov 15, 2019 – Jan 16, 2020

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v2 cover env: Access Environment in SPI flashes before relocation

Add the possibility to access Environment stored in SPI NOR Flash before relocation. This is used in DM/DTS rework for the aristainetos boards. There mutliple DTS are packed into a FIT image and through a variable in the Environment the correct DTS will be selected. For this we need very early access to the Environment. Also may this is good for selecting the console baudrate again through Environment which I think currently does not work for any board based on DM/DTS and with Environment in SPI NOR flash.

base:
3ff1ff3ff7 - Merge branch '2019-11-12-migrate-SYS_REDUNDAND_ENVIRONMENT'

travis build:
https://travis-ci.org/hsdenx/u-boot-test/builds/612239100
(travis has currently some problems...)

Therefore I started an build on azure, which worked, see:
https://dev.azure.com/hs0298/hs/_build/results?buildId=1&view=results

Changes in v2:
- patch "env: split env_import_redund into 2 functions" is new in version 2. Idea is to not duplicate too much code as Simon suggested.
- env_sf_init_early() always return 0 now. If we do not return 0 in this function, env_set_inited() never get called, which has the consequence that env_load/save/erase never work, because they check if the init bit is set.

Heiko Schocher (2):
env: split env_import_redund() into 2 functions
env: Access Environment in SPI flashes before relocation

 env/Kconfig   |  8 +++++
 env/common.c  | 40 +++++++++++++++++++------
 env/sf.c      | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++
 include/env.h | 18 ++++++++++++
 4 files changed, 138 insertions(+), 9 deletions(-)
Signature · 1 line
2.21.0
#1 lore raw

PATCH v2 1/2 env: split env_import_redund() into 2 functions

split from env_import_redund() the part which checks which Environment is valid into a separate function called env_check_redund() and call it from env_import_redund().

So env_check_redund() can be used from places which also need to do this checks.

---
The return values from env_check_redund() for the cases only one read buffer is OK are not perfect, may we should find better return codes?

Changes in v2:
- patch "env: split env_import_redund into 2 functions" is new in version 2. Idea is to not duplicate too much code as Simon suggested.

 env/common.c  | 40 +++++++++++++++++++++++++++++++---------
 include/env.h | 18 ++++++++++++++++++
 2 files changed, 49 insertions(+), 9 deletions(-)
env/common.c +31 -9
index 4daaa6faea..790829b71f 100644
@@ -136,11 +136,11 @@ int env_import(const char *buf, int check)
 #ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
 static unsigned char env_flags;
 
-int env_import_redund(const char *buf1, int buf1_read_fail,
-		      const char *buf2, int buf2_read_fail)
+int env_check_redund(const char *buf1, int buf1_read_fail,
+		     const char *buf2, int buf2_read_fail)
 {
 	int crc1_ok, crc2_ok;
-	env_t *ep, *tmp_env1, *tmp_env2;
+	env_t *tmp_env1, *tmp_env2;
 
 	tmp_env1 = (env_t *)buf1;
 	tmp_env2 = (env_t *)buf2;
@@ -153,14 +153,13 @@ int env_import_redund(const char *buf1, int buf1_read_fail,
 	}
 
 	if (buf1_read_fail && buf2_read_fail) {
-		env_set_default("bad env area", 0);
 		return -EIO;
 	} else if (!buf1_read_fail && buf2_read_fail) {
 		gd->env_valid = ENV_VALID;
-		return env_import((char *)tmp_env1, 1);
+		return -EINVAL;
 	} else if (buf1_read_fail && !buf2_read_fail) {
 		gd->env_valid = ENV_REDUND;
-		return env_import((char *)tmp_env2, 1);
+		return -ENOENT;
 	}
 
 	crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) ==
@@ -169,7 +168,6 @@ int env_import_redund(const char *buf1, int buf1_read_fail,
 			tmp_env2->crc;
 
 	if (!crc1_ok && !crc2_ok) {
-		env_set_default("bad CRC", 0);
 		return -ENOMSG; /* needed for env_load() */
 	} else if (crc1_ok && !crc2_ok) {
 		gd->env_valid = ENV_VALID;
@@ -189,12 +187,36 @@ int env_import_redund(const char *buf1, int buf1_read_fail,
 			gd->env_valid = ENV_VALID;
 	}
 
+	return 0;
+}
+
+int env_import_redund(const char *buf1, int buf1_read_fail,
+		      const char *buf2, int buf2_read_fail)
+{
+	env_t *ep;
+	int ret;
+
+	ret = env_check_redund(buf1, buf1_read_fail, buf2, buf2_read_fail);
+
+	if (ret == -EIO) {
+		env_set_default("bad env area", 0);
+		return -EIO;
+	} else if (ret == -EINVAL) {
+		return env_import((char *)buf1, 1);
+	} else if (ret == -ENOENT) {
+		return env_import((char *)buf2, 1);
+	} else if (ret == -ENOMSG) {
+		env_set_default("bad CRC", 0);
+		return -ENOMSG;
+	}
+
 	if (gd->env_valid == ENV_VALID)
-		ep = tmp_env1;
+		ep = (env_t *)buf1;
 	else
-		ep = tmp_env2;
+		ep = (env_t *)buf2;
 
 	env_flags = ep->flags;
+
 	return env_import((char *)ep, 0);
 }
 #endif /* CONFIG_SYS_REDUNDAND_ENVIRONMENT */
include/env.h +18 -0
index b72239f6a5..4616551afb 100644
@@ -293,6 +293,24 @@ int env_import(const char *buf, int check);
  */
 int env_export(struct environment_s *env_out);
 
+/**
+ * env_check_redund() - check the two redundant environments
+ *   and find out, which is the valid one.
+ *
+ * @buf1: First environment (struct environemnt_s *)
+ * @buf1_read_fail: 0 if buf1 is valid, non-zero if invalid
+ * @buf2: Second environment (struct environemnt_s *)
+ * @buf2_read_fail: 0 if buf2 is valid, non-zero if invalid
+ * @return 0 if OK,
+ *	-EIO if no environment is valid,
+ *	-EINVAL if read of second entry is good
+ *	-ENOENT if read of first entry is good
+ *	-ENOMSG if the CRC was bad
+ */
+
+int env_check_redund(const char *buf1, int buf1_read_fail,
+		     const char *buf2, int buf2_read_fail);
+
 /**
  * env_import_redund() - Select and import one of two redundant environments
  *
Signature · 1 line
2.21.0
#2 lore raw

PATCH v2 2/2 env: Access Environment in SPI flashes before relocation

In reply to Heiko Schocher

Enable the new Kconfig option ENV_SPI_EARLY if you want to use Environment in SPI flash before relocation. Call env_init() and than you can use env_get_f() for accessing Environment variables.

---

Changes in v2:
- env_sf_init_early() always return 0 now. If we do not return 0 in this function, env_set_inited() never get called, which has the consequence that env_load/save/erase never work, because they check if the init bit is set.

 env/Kconfig |  8 ++++++
 env/sf.c    | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 89 insertions(+)
env/Kconfig +8 -0
index 090cc795f9..76e4f30839 100644
@@ -370,6 +370,14 @@ config ENV_SPI_MODE
 	  Value of the SPI work mode for environment.
 	  See include/spi.h for value.
 
+config ENV_SPI_EARLY
+	bool "Access Environment in SPI flashes before relocation"
+	depends on ENV_IS_IN_SPI_FLASH
+	help
+	  Enable this if you want to use Environment in SPI flash
+	  before relocation. Call env_init() and than you can use
+	  env_get_f() for accessing Environment variables.
+
 config ENV_IS_IN_UBI
 	bool "Environment in a UBI volume"
 	depends on !CHAIN_OF_TRUST
env/sf.c +81 -0

92 lines not on this page · raw message

Signature · 1 line
2.21.0
#3 lore raw

PATCH v2 2/2 env: Access Environment in SPI flashes before relocation

Hi Heiko,

Quoted 44 lines · Heiko Schocher

Enable the new Kconfig option ENV_SPI_EARLY if you want to use Environment in SPI flash before relocation. Call env_init() and than you can use env_get_f() for accessing Environment variables.

---

Changes in v2:
- env_sf_init_early() always return 0 now. If we do not return 0 in this function, env_set_inited() never get called, which has the consequence that env_load/save/erase never work, because they check if the init bit is set.

 env/Kconfig |  8 ++++++
 env/sf.c    | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 89 insertions(+)
env/Kconfig +8 -0
index 090cc795f9..76e4f30839 100644
@@ -370,6 +370,14 @@ config ENV_SPI_MODE
          Value of the SPI work mode for environment.
          See include/spi.h for value.
 
+config ENV_SPI_EARLY
+       bool "Access Environment in SPI flashes before relocation"
+       depends on ENV_IS_IN_SPI_FLASH
+       help
+         Enable this if you want to use Environment in SPI flash
+         before relocation. Call env_init() and than you can use
+         env_get_f() for accessing Environment variables.
+
 config ENV_IS_IN_UBI
        bool "Environment in a UBI volume"
        depends on !CHAIN_OF_TRUST
env/sf.c +0 -0
index 590d0cedd8..be2e7fc01c 100644

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

Quoted 5 lines
env/sf.c +2 -0
@@ -308,6 +308,85 @@ static int env_sf_init(void)
 }
 #endif
 
+#if defined(CONFIG_ENV_SPI_EARLY)
+static int env_sf_init_early(void)

Function comment to explain what it does?

Quoted 12 lines
env/sf.c +12 -0
+{
+       int ret;
+       int read1_fail;
+#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
+       int read2_fail;
+#else
+       int crc1_ok;
+#endif
+
+       /*
+        * if malloc is not ready yet, we cannot use
+        * this part yet.

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

Quoted 10 lines
env/sf.c +10 -0
+        */
+       if (!gd->malloc_limit)
+               return -ENOENT;
+
+       env_t *tmp_env2 = NULL;
+       env_t *tmp_env1;
+
+       tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
+                       CONFIG_ENV_SIZE);
+#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT

Can the #ifdefs in this function be if(IS_ENABLED...) instead?

Quoted 9 lines
env/sf.c +9 -0
+       tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
+                       CONFIG_ENV_SIZE);
+#endif
+       if (!tmp_env1 || !tmp_env2)
+               goto out;
+
+       ret = setup_flash_device();
+       if (ret)
+               goto out;

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

Quoted 10 lines
env/sf.c +10 -0
+
+       read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
+                                   CONFIG_ENV_SIZE, tmp_env1);
+#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
+       read2_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
+                                   CONFIG_ENV_SIZE, tmp_env2);
+       ret = env_check_redund((char *)tmp_env1, read1_fail,
+                              (char *)tmp_env2, read2_fail);
+
+       if ((ret == -EIO) || (ret == -ENOMSG))

Can drop extra brackets

Quoted 17 lines
env/sf.c +17 -0
+               goto err_read;
+
+       if (gd->env_valid == ENV_VALID)
+               gd->env_addr = (unsigned long)&tmp_env1->data;
+       else
+               gd->env_addr = (unsigned long)&tmp_env2->data;
+
+#else
+       if (read1_fail)
+               goto err_read;
+
+       crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) ==
+                       tmp_env1->crc;
+       if (!crc1_ok)
+               goto err_read;
+
+       /* if valid -> this is our env */

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

Quoted 10 lines
env/sf.c +10 -0
+       gd->env_valid = ENV_VALID;
+       gd->env_addr = (unsigned long)&tmp_env1->data;
+#endif
+
+       return 0;
+err_read:
+       spi_flash_free(env_flash);
+       env_flash = NULL;
+       free(tmp_env1);
+#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT

Unrelated to your patch, but should that be REDUNDANT?

Quoted 13 lines
env/sf.c +9 -0
+       free(tmp_env2);
+#endif
+out:
+       /* env is not valid. always return 0 */
+       gd->env_valid = ENV_INVALID;
+       return 0;
+}
+#endif
+
 U_BOOT_ENV_LOCATION(sf) = {
        .location       = ENVL_SPI_FLASH,
        ENV_NAME("SPI Flash")

env/sf.c U_BOOT_ENV_LOCATION(sf) = { · line 401

Quoted 6 lines
env/sf.c +2 -0
@@ -317,5 +396,7 @@ U_BOOT_ENV_LOCATION(sf) = {
 #endif
 #if defined(INITENV) && defined(CONFIG_ENV_ADDR)
        .init           = env_sf_init,
+#elif defined(CONFIG_ENV_SPI_EARLY)
+       .init           = env_sf_init_early,
 #endif

It seems like we should have two drivers here, only one of which is enabled. Perhaps for testing sandbox would want to enable both?

Alternatively I think env_sf_init() should decide which init function to call.

Regards,
Simon

#4 lore raw

PATCH v2 2/2 env: Access Environment in SPI flashes before relocation

Hello Simon,

Am 10.12.2019 um 13:39 schrieb Simon Glass:

Quoted 50 lines

Hi Heiko,

Quoted 47 lines · Heiko Schocher

Enable the new Kconfig option ENV_SPI_EARLY if you want to use Environment in SPI flash before relocation. Call env_init() and than you can use env_get_f() for accessing Environment variables.

---

Changes in v2:
- env_sf_init_early() always return 0 now. If we do not return 0 in this function, env_set_inited() never get called, which has the consequence that env_load/save/erase never work, because they check if the init bit is set.

  env/Kconfig |  8 ++++++
  env/sf.c    | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++
  2 files changed, 89 insertions(+)
env/Kconfig +8 -0
index 090cc795f9..76e4f30839 100644
@@ -370,6 +370,14 @@ config ENV_SPI_MODE
           Value of the SPI work mode for environment.
           See include/spi.h for value.
 
+config ENV_SPI_EARLY
+       bool "Access Environment in SPI flashes before relocation"
+       depends on ENV_IS_IN_SPI_FLASH
+       help
+         Enable this if you want to use Environment in SPI flash
+         before relocation. Call env_init() and than you can use
+         env_get_f() for accessing Environment variables.
+
  config ENV_IS_IN_UBI
         bool "Environment in a UBI volume"
         depends on !CHAIN_OF_TRUST
env/sf.c +0 -0
index 590d0cedd8..be2e7fc01c 100644
@@ -308,6 +308,85 @@ static int env_sf_init(void)
  }
  #endif
 
Quoted 4 lines

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

Quoted 2 lines
env/sf.c +2 -0
+#if defined(CONFIG_ENV_SPI_EARLY)
+static int env_sf_init_early(void)

Function comment to explain what it does?

Ok, added.

Quoted 16 lines
Quoted 16 lines
env/sf.c +16 -0
+{
+       int ret;
+       int read1_fail;
+#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
+       int read2_fail;
+#else
+       int crc1_ok;
+#endif
+
+       /*
+        * if malloc is not ready yet, we cannot use
+        * this part yet.
+        */
+       if (!gd->malloc_limit)
+               return -ENOENT;
+
Quoted 8 lines

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

Quoted 6 lines
env/sf.c +6 -0
+       env_t *tmp_env2 = NULL;
+       env_t *tmp_env1;
+
+       tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
+                       CONFIG_ENV_SIZE);
+#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT

Can the #ifdefs in this function be if(IS_ENABLED...) instead?

I think yes.

Quoted 7 lines
Quoted 7 lines
env/sf.c +7 -0
+       tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
+                       CONFIG_ENV_SIZE);
+#endif
+       if (!tmp_env1 || !tmp_env2)
+               goto out;
+
+       ret = setup_flash_device();
Quoted 8 lines

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

Quoted 8 lines
env/sf.c +8 -0
+       if (ret)
+               goto out;
+
+       read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
+                                   CONFIG_ENV_SIZE, tmp_env1);
+#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
+       read2_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
+                                   CONFIG_ENV_SIZE, tmp_env2);

Hmm... if porting to "if(IS_ENABLED...)" may here is CONFIG_ENV_OFFSET_REDUND not defined ... I will see on world build ...

Quoted 6 lines

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

Quoted 4 lines
env/sf.c +4 -0
+       ret = env_check_redund((char *)tmp_env1, read1_fail,
+                              (char *)tmp_env2, read2_fail);
+
+       if ((ret == -EIO) || (ret == -ENOMSG))

Can drop extra brackets

Without this bracket I get:

/home/hs/data/Entwicklung/abb/uboot-rework/u-boot/env/sf.c: In function ‘env_sf_init_early’:
/home/hs/data/Entwicklung/abb/uboot-rework/u-boot/env/sf.c:355:20: error: expected expression before ‘||’ token

    if (ret == -EIO) || (ret == -ENOMSG)
                     ^~
Quoted 21 lines
Quoted 21 lines
env/sf.c +21 -0
+               goto err_read;
+
+       if (gd->env_valid == ENV_VALID)
+               gd->env_addr = (unsigned long)&tmp_env1->data;
+       else
+               gd->env_addr = (unsigned long)&tmp_env2->data;
+
+#else
+       if (read1_fail)
+               goto err_read;
+
+       crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) ==
+                       tmp_env1->crc;
+       if (!crc1_ok)
+               goto err_read;
+
+       /* if valid -> this is our env */
+       gd->env_valid = ENV_VALID;
+       gd->env_addr = (unsigned long)&tmp_env1->data;
+#endif
+
Quoted 8 lines

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

Quoted 6 lines
env/sf.c +6 -0
+       return 0;
+err_read:
+       spi_flash_free(env_flash);
+       env_flash = NULL;
+       free(tmp_env1);
+#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT

Unrelated to your patch, but should that be REDUNDANT?

Hmm.. currently the define is REDUNDAND ... but you are correct ...

Quoted 23 lines
Quoted 13 lines
env/sf.c +9 -0
+       free(tmp_env2);
+#endif
+out:
+       /* env is not valid. always return 0 */
+       gd->env_valid = ENV_INVALID;
+       return 0;
+}
+#endif
+
  U_BOOT_ENV_LOCATION(sf) = {
         .location       = ENVL_SPI_FLASH,
         ENV_NAME("SPI Flash")

env/sf.c U_BOOT_ENV_LOCATION(sf) = { · line 401

Quoted 6 lines
env/sf.c +2 -0
@@ -317,5 +396,7 @@ U_BOOT_ENV_LOCATION(sf) = {
  #endif
  #if defined(INITENV) && defined(CONFIG_ENV_ADDR)
         .init           = env_sf_init,
+#elif defined(CONFIG_ENV_SPI_EARLY)
+       .init           = env_sf_init_early,
  #endif

It seems like we should have two drivers here, only one of which is enabled. Perhaps for testing sandbox would want to enable both?

Quoted 1 line

Alternatively I think env_sf_init() should decide which init function to call.

Ok, I can change this, but this leads into change, that we now always call ".init" ...

in env/env.c env_init():

         for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) {
                 if (!drv->init || !(ret = drv->init()))
                         env_set_inited(drv->location);

So, if env_sf_init() return 0 in case nothing ToDo, it is OK to change this. I added a comment in env_sf_init()

bye,
Heiko

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-52   Fax: +49-8142-66989-80   Email: hs at denx.de
#5 lore raw

PATCH v2 2/2 env: Access Environment in SPI flashes before relocation

Hi Heiko,

Quoted 27 lines · Heiko Schocher

Hello Simon,

Am 10.12.2019 um 13:39 schrieb Simon Glass:

Quoted 23 lines

Hi Heiko,

Quoted 20 lines · Heiko Schocher

Enable the new Kconfig option ENV_SPI_EARLY if you want to use Environment in SPI flash before relocation. Call env_init() and than you can use env_get_f() for accessing Environment variables.

---

Changes in v2:
- env_sf_init_early() always return 0 now. If we do not return 0 in this function, env_set_inited() never get called, which has the consequence that env_load/save/erase never work, because they check if the init bit is set.

  env/Kconfig |  8 ++++++
  env/sf.c    | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++
  2 files changed, 89 insertions(+)

[..]

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
env/sf.c +10 -0
+       tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
+                       CONFIG_ENV_SIZE);
+#endif
+       if (!tmp_env1 || !tmp_env2)
+               goto out;
+
+       ret = setup_flash_device();
+       if (ret)
+               goto out;
+
Quoted 8 lines
Quoted 5 lines

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

Quoted 5 lines
env/sf.c +5 -0
+       read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
+                                   CONFIG_ENV_SIZE, tmp_env1);
+#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
+       read2_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
+                                   CONFIG_ENV_SIZE, tmp_env2);

Hmm... if porting to "if(IS_ENABLED...)" may here is CONFIG_ENV_OFFSET_REDUND not defined ... I will see on world build ...

OK maybe it is not in Kconfig yet?

Quoted 8 lines
Quoted 7 lines

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

Quoted 5 lines
env/sf.c +4 -0
+       ret = env_check_redund((char *)tmp_env1, read1_fail,
+                              (char *)tmp_env2, read2_fail);
+
+       if ((ret == -EIO) || (ret == -ENOMSG))

Can drop extra brackets

Quoted 7 lines

Without this bracket I get:

/home/hs/data/Entwicklung/abb/uboot-rework/u-boot/env/sf.c: In function ‘env_sf_init_early’:
/home/hs/data/Entwicklung/abb/uboot-rework/u-boot/env/sf.c:355:20: error: expected expression before ‘||’ token

    if (ret == -EIO) || (ret == -ENOMSG)
                     ^~

No, I mean:

   if (ret == -EIO || ret == -ENOMSG)
Quoted 61 lines
Quoted 22 lines
Quoted 22 lines
env/sf.c +21 -0
+               goto err_read;
+
+       if (gd->env_valid == ENV_VALID)
+               gd->env_addr = (unsigned long)&tmp_env1->data;
+       else
+               gd->env_addr = (unsigned long)&tmp_env2->data;
+
+#else
+       if (read1_fail)
+               goto err_read;
+
+       crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) ==
+                       tmp_env1->crc;
+       if (!crc1_ok)
+               goto err_read;
+
+       /* if valid -> this is our env */
+       gd->env_valid = ENV_VALID;
+       gd->env_addr = (unsigned long)&tmp_env1->data;
+#endif
+
Quoted 8 lines

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

Quoted 6 lines
env/sf.c +6 -0
+       return 0;
+err_read:
+       spi_flash_free(env_flash);
+       env_flash = NULL;
+       free(tmp_env1);
+#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT

Unrelated to your patch, but should that be REDUNDANT?

Hmm.. currently the define is REDUNDAND ... but you are correct ...

Quoted 23 lines
Quoted 13 lines
env/sf.c +9 -0
+       free(tmp_env2);
+#endif
+out:
+       /* env is not valid. always return 0 */
+       gd->env_valid = ENV_INVALID;
+       return 0;
+}
+#endif
+
  U_BOOT_ENV_LOCATION(sf) = {
         .location       = ENVL_SPI_FLASH,
         ENV_NAME("SPI Flash")

env/sf.c U_BOOT_ENV_LOCATION(sf) = { · line 401

Quoted 6 lines
env/sf.c +2 -0
@@ -317,5 +396,7 @@ U_BOOT_ENV_LOCATION(sf) = {
  #endif
  #if defined(INITENV) && defined(CONFIG_ENV_ADDR)
         .init           = env_sf_init,
+#elif defined(CONFIG_ENV_SPI_EARLY)
+       .init           = env_sf_init_early,
  #endif

It seems like we should have two drivers here, only one of which is enabled. Perhaps for testing sandbox would want to enable both?

Quoted 1 line

Alternatively I think env_sf_init() should decide which init function to call.

Ok, I can change this, but this leads into change, that we now always call ".init" ...

Quoted 8 lines

in env/env.c env_init():

         for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) {
                 if (!drv->init || !(ret = drv->init()))
                         env_set_inited(drv->location);

So, if env_sf_init() return 0 in case nothing ToDo, it is OK to change this. I added a comment in env_sf_init()

Yes I think it is better to have the function decide is anything is needed.

Regards,
Simon

#6 lore raw

PATCH v2 2/2 env: Access Environment in SPI flashes before relocation

Hello Simon,

Am 16.01.2020 um 01:10 schrieb Simon Glass:

Quoted 52 lines

Hi Heiko,

Quoted 27 lines · Heiko Schocher

Hello Simon,

Am 10.12.2019 um 13:39 schrieb Simon Glass:

Quoted 23 lines

Hi Heiko,

Quoted 20 lines · Heiko Schocher

Enable the new Kconfig option ENV_SPI_EARLY if you want to use Environment in SPI flash before relocation. Call env_init() and than you can use env_get_f() for accessing Environment variables.

---

Changes in v2:
- env_sf_init_early() always return 0 now. If we do not return 0 in this function, env_set_inited() never get called, which has the consequence that env_load/save/erase never work, because they check if the init bit is set.

   env/Kconfig |  8 ++++++
   env/sf.c    | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++
   2 files changed, 89 insertions(+)

[..]

Quoted 10 lines
Quoted 10 lines
Quoted 10 lines
env/sf.c +10 -0
+       tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
+                       CONFIG_ENV_SIZE);
+#endif
+       if (!tmp_env1 || !tmp_env2)
+               goto out;
+
+       ret = setup_flash_device();
+       if (ret)
+               goto out;
+
Quoted 8 lines
Quoted 5 lines

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

Quoted 5 lines
env/sf.c +5 -0
+       read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
+                                   CONFIG_ENV_SIZE, tmp_env1);
+#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT
+       read2_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
+                                   CONFIG_ENV_SIZE, tmp_env2);

Hmm... if porting to "if(IS_ENABLED...)" may here is CONFIG_ENV_OFFSET_REDUND not defined ... I will see on world build ...

Quoted 1 line

OK maybe it is not in Kconfig yet?

No, both are in Kconfig, and ENV_OFFSET_REDUND depends on SYS_REDUNDAND_ENVIRONMENT so all should be fine.

Quoted 15 lines
Quoted 7 lines
Quoted 6 lines

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

Quoted 4 lines
env/sf.c +4 -0
+       ret = env_check_redund((char *)tmp_env1, read1_fail,
+                              (char *)tmp_env2, read2_fail);
+
+       if ((ret == -EIO) || (ret == -ENOMSG))

Can drop extra brackets

Quoted 7 lines

Without this bracket I get:

/home/hs/data/Entwicklung/abb/uboot-rework/u-boot/env/sf.c: In function ‘env_sf_init_early’:
/home/hs/data/Entwicklung/abb/uboot-rework/u-boot/env/sf.c:355:20: error: expected expression before ‘||’ token

     if (ret == -EIO) || (ret == -ENOMSG)
                      ^~
Quoted 3 lines

No, I mean:

    if (ret == -EIO || ret == -ENOMSG)

of course, sorry for being so stupid!

Quoted 62 lines
Quoted 62 lines
Quoted 21 lines
Quoted 21 lines
env/sf.c +21 -0
+               goto err_read;
+
+       if (gd->env_valid == ENV_VALID)
+               gd->env_addr = (unsigned long)&tmp_env1->data;
+       else
+               gd->env_addr = (unsigned long)&tmp_env2->data;
+
+#else
+       if (read1_fail)
+               goto err_read;
+
+       crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) ==
+                       tmp_env1->crc;
+       if (!crc1_ok)
+               goto err_read;
+
+       /* if valid -> this is our env */
+       gd->env_valid = ENV_VALID;
+       gd->env_addr = (unsigned long)&tmp_env1->data;
+#endif
+
Quoted 8 lines

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

Quoted 6 lines
env/sf.c +6 -0
+       return 0;
+err_read:
+       spi_flash_free(env_flash);
+       env_flash = NULL;
+       free(tmp_env1);
+#ifdef CONFIG_SYS_REDUNDAND_ENVIRONMENT

Unrelated to your patch, but should that be REDUNDANT?

Hmm.. currently the define is REDUNDAND ... but you are correct ...

Quoted 23 lines
Quoted 13 lines
env/sf.c +9 -0
+       free(tmp_env2);
+#endif
+out:
+       /* env is not valid. always return 0 */
+       gd->env_valid = ENV_INVALID;
+       return 0;
+}
+#endif
+
   U_BOOT_ENV_LOCATION(sf) = {
          .location       = ENVL_SPI_FLASH,
          ENV_NAME("SPI Flash")

env/sf.c U_BOOT_ENV_LOCATION(sf) = { · line 401

Quoted 6 lines
env/sf.c +2 -0
@@ -317,5 +396,7 @@ U_BOOT_ENV_LOCATION(sf) = {
   #endif
   #if defined(INITENV) && defined(CONFIG_ENV_ADDR)
          .init           = env_sf_init,
+#elif defined(CONFIG_ENV_SPI_EARLY)
+       .init           = env_sf_init_early,
   #endif

It seems like we should have two drivers here, only one of which is enabled. Perhaps for testing sandbox would want to enable both?

Quoted 1 line

Alternatively I think env_sf_init() should decide which init function to call.

Ok, I can change this, but this leads into change, that we now always call ".init" ...

in env/env.c env_init():

Quoted 8 lines
Quoted 6 lines
          for (prio = 0; (drv = env_driver_lookup(ENVOP_INIT, prio)); prio++) {
                  if (!drv->init || !(ret = drv->init()))
                          env_set_inited(drv->location);

So, if env_sf_init() return 0 in case nothing ToDo, it is OK to change this. I added a comment in env_sf_init()

Yes I think it is better to have the function decide is anything is needed.

Ok, I rework it.

Thanks for your comments!

bye,
Heiko

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-52   Fax: +49-8142-66989-80   Email: hs at denx.de