[U-Boot] [PATCH] AT91: Defer Dataflash access to env_relocate_spec

Hong Xu hong.xu at atmel.com
Wed Aug 3 09:01:36 CEST 2011


When env_init is called, the SPI is not actually initialized in U-Boot.
So that we can not read Dataflash for its content.
We simply mark it OK for now, and defer the real work to
`env_relocate_spec'. (Idealy from env_nand.c)

Signed-off-by: Hong Xu <hong.xu at atmel.com>
---
 common/env_dataflash.c |   83 ++++++++++++++++++++++++++----------------------
 1 files changed, 45 insertions(+), 38 deletions(-)

diff --git a/common/env_dataflash.c b/common/env_dataflash.c
index 1d57079..55534a5 100644
--- a/common/env_dataflash.c
+++ b/common/env_dataflash.c
@@ -21,6 +21,7 @@
 #include <command.h>
 #include <environment.h>
 #include <linux/stddef.h>
+#include <malloc.h>
 #include <dataflash.h>
 #include <search.h>
 #include <errno.h>
@@ -50,11 +51,46 @@ uchar env_get_char_spec(int index)
 
 void env_relocate_spec(void)
 {
-	char buf[CONFIG_ENV_SIZE];
+	ulong old_crc, new_crc = 0;
+	char *buf;
 
-	read_dataflash(CONFIG_ENV_ADDR, CONFIG_ENV_SIZE, buf);
+	gd->env_valid = 0;
 
-	env_import(buf, 1);
+	buf = (char *)malloc(CONFIG_ENV_SIZE);
+	if (buf == NULL) {
+		error("Can not allocate memory for env.\n");
+		goto err_mem;
+	}
+
+	AT91F_DataflashInit();
+
+	if (read_dataflash(CONFIG_ENV_ADDR + offsetof(env_t, crc),
+		sizeof(ulong), (char *)&old_crc) != DATAFLASH_OK) {
+		error("Dataflash: Failed to read original 4-bytes CRC\n");
+		goto err;
+	}
+
+	if (read_dataflash(CONFIG_ENV_ADDR + offsetof(env_t, data),
+		ENV_SIZE, buf) != DATAFLASH_OK) {
+		error("Dataflash: Failed to read env string.\n");
+		goto err;
+	}
+
+	new_crc = crc32(new_crc, (uchar *)buf, ENV_SIZE);
+
+	if (old_crc == new_crc) {
+		gd->env_addr  = offsetof(env_t, data);
+		gd->env_valid = 1;
+		env_import(buf, 0);
+		goto out;
+	}
+
+err:
+	set_default_env("!bad CRC");
+out:
+	free(buf);
+err_mem:
+	return;
 }
 
 #ifdef CONFIG_ENV_OFFSET_REDUND
@@ -83,44 +119,15 @@ int saveenv(void)
 /*
  * Initialize environment use
  *
- * We are still running from ROM, so data use is limited.
- * Use a (moderately small) buffer on the stack
+ * When env_init is called, the SPI is not actually initialized in U-Boot.
+ * So that we can not read Dataflash for its content.
+ * We simply mark it OK for now, and defer the real work to
+ * `env_relocate_spec'. (Idealy from env_nand.c)
  */
 int env_init(void)
 {
-	ulong crc, len, new;
-	unsigned off;
-	uchar buf[64];
-
-	if (gd->env_valid)
-		return 0;
-
-	AT91F_DataflashInit();	/* prepare for DATAFLASH read/write */
-
-	/* read old CRC */
-	read_dataflash(CONFIG_ENV_ADDR + offsetof(env_t, crc),
-		sizeof(ulong), (char *)&crc);
-
-	new = 0;
-	len = ENV_SIZE;
-	off = offsetof(env_t,data);
-	while (len > 0) {
-		int n = (len > sizeof(buf)) ? sizeof(buf) : len;
-
-		read_dataflash(CONFIG_ENV_ADDR + off, n, (char *)buf);
-
-		new = crc32 (new, buf, n);
-		len -= n;
-		off += n;
-	}
-
-	if (crc == new) {
-		gd->env_addr  = offsetof(env_t,data);
-		gd->env_valid = 1;
-	} else {
-		gd->env_addr  = (ulong)&default_environment[0];
-		gd->env_valid = 0;
-	}
+	gd->env_addr  = (ulong)&default_environment[0];
+	gd->env_valid = 1;
 
 	return 0;
 }
-- 
1.7.3.3



More information about the U-Boot mailing list