]> git.sur5r.net Git - u-boot/blobdiff - common/env_nand.c
Sequential accesses to non-existent memory must be synchronized,
[u-boot] / common / env_nand.c
index 2e1bfa666596b2531852ee46cc1ac69712404f2e..67c4a4e011761b92b92f291714cf9af4181696d3 100644 (file)
@@ -2,7 +2,7 @@
  * (C) Copyright 2004
  * Jian Zhang, Texas Instruments, jzhang@ti.com.
 
- * (C) Copyright 2000-2004
+ * (C) Copyright 2000-2006
  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
  *
  * (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
@@ -76,13 +76,14 @@ env_t *env_ptr = 0;
 
 
 /* local functions */
+#if !defined(ENV_IS_EMBEDDED)
 static void use_default(void);
+#endif
 
+DECLARE_GLOBAL_DATA_PTR;
 
 uchar env_get_char_spec (int index)
 {
-       DECLARE_GLOBAL_DATA_PTR;
-
        return ( *((uchar *)(gd->env_addr + index)) );
 }
 
@@ -92,13 +93,55 @@ uchar env_get_char_spec (int index)
  * Mark it OK for now. env_relocate() in env_common.c
  * will call our relocate function which will does
  * the real validation.
+ *
+ * When using a NAND boot image (like sequoia_nand), the environment
+ * can be embedded or attached to the U-Boot image in NAND flash. This way
+ * the SPL loads not only the U-Boot image from NAND but also the
+ * environment.
  */
 int env_init(void)
 {
-       DECLARE_GLOBAL_DATA_PTR;
+#if defined(ENV_IS_EMBEDDED)
+       ulong total;
+       int crc1_ok = 0, crc2_ok = 0;
+       env_t *tmp_env1, *tmp_env2;
+
+       total = CFG_ENV_SIZE;
+
+       tmp_env1 = env_ptr;
+       tmp_env2 = (env_t *)((ulong)env_ptr + CFG_ENV_SIZE);
+
+       crc1_ok = (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc);
+       crc2_ok = (crc32(0, tmp_env2->data, ENV_SIZE) == tmp_env2->crc);
+
+       if (!crc1_ok && !crc2_ok)
+               gd->env_valid = 0;
+       else if(crc1_ok && !crc2_ok)
+               gd->env_valid = 1;
+       else if(!crc1_ok && crc2_ok)
+               gd->env_valid = 2;
+       else {
+               /* both ok - check serial */
+               if(tmp_env1->flags == 255 && tmp_env2->flags == 0)
+                       gd->env_valid = 2;
+               else if(tmp_env2->flags == 255 && tmp_env1->flags == 0)
+                       gd->env_valid = 1;
+               else if(tmp_env1->flags > tmp_env2->flags)
+                       gd->env_valid = 1;
+               else if(tmp_env2->flags > tmp_env1->flags)
+                       gd->env_valid = 2;
+               else /* flags are equal - almost impossible */
+                       gd->env_valid = 1;
+       }
 
+       if (gd->env_valid == 1)
+               env_ptr = tmp_env1;
+       else if (gd->env_valid == 2)
+               env_ptr = tmp_env2;
+#else /* ENV_IS_EMBEDDED */
        gd->env_addr  = (ulong)&default_environment[0];
        gd->env_valid = 1;
+#endif /* ENV_IS_EMBEDDED */
 
        return (0);
 }
@@ -111,9 +154,8 @@ int env_init(void)
 #ifdef CFG_ENV_OFFSET_REDUND
 int saveenv(void)
 {
-       int total, ret = 0;
-
-       DECLARE_GLOBAL_DATA_PTR;
+       ulong total;
+       int ret = 0;
 
        env_ptr->flags++;
        total = CFG_ENV_SIZE;
@@ -146,7 +188,8 @@ int saveenv(void)
 #else /* ! CFG_ENV_OFFSET_REDUND */
 int saveenv(void)
 {
-       int total, ret = 0;
+       ulong total;
+       int ret = 0;
 
        puts ("Erasing Nand...");
        if (nand_erase(&nand_info[0], CFG_ENV_OFFSET, CFG_ENV_SIZE))
@@ -154,8 +197,7 @@ int saveenv(void)
 
        puts ("Writing to Nand... ");
        total = CFG_ENV_SIZE;
-       ret = nand_write(&nand_info[0], CFG_ENV_OFFSET, &total,
-                       (u_char*) env_ptr);
+       ret = nand_write(&nand_info[0], CFG_ENV_OFFSET, &total, (u_char*)env_ptr);
        if (ret || total != CFG_ENV_SIZE)
                return 1;
 
@@ -169,11 +211,10 @@ int saveenv(void)
 void env_relocate_spec (void)
 {
 #if !defined(ENV_IS_EMBEDDED)
-       int crc1_ok = 0, crc2_ok = 0, total;
+       ulong total;
+       int crc1_ok = 0, crc2_ok = 0;
        env_t *tmp_env1, *tmp_env2;
 
-       DECLARE_GLOBAL_DATA_PTR;
-
        total = CFG_ENV_SIZE;
 
        tmp_env1 = (env_t *) malloc(CFG_ENV_SIZE);
@@ -227,12 +268,12 @@ void env_relocate_spec (void)
 void env_relocate_spec (void)
 {
 #if !defined(ENV_IS_EMBEDDED)
-       int ret, total;
+       ulong total;
+       int ret;
 
        total = CFG_ENV_SIZE;
-       ret = nand_read(&nand_info[0], CFG_ENV_OFFSET, &total,
-                       (u_char*) env_ptr);
-       if (ret || total != CFG_ENV_SIZE)
+       ret = nand_read(&nand_info[0], CFG_ENV_OFFSET, &total, (u_char*)env_ptr);
+       if (ret || total != CFG_ENV_SIZE)
                return use_default();
 
        if (crc32(0, env_ptr->data, ENV_SIZE) != env_ptr->crc)
@@ -241,10 +282,9 @@ void env_relocate_spec (void)
 }
 #endif /* CFG_ENV_OFFSET_REDUND */
 
+#if !defined(ENV_IS_EMBEDDED)
 static void use_default()
 {
-       DECLARE_GLOBAL_DATA_PTR;
-
        puts ("*** Warning - bad CRC or NAND, using default environment\n\n");
 
        if (default_environment_size > CFG_ENV_SIZE){
@@ -260,5 +300,6 @@ static void use_default()
        gd->env_valid = 1;
 
 }
+#endif
 
 #endif /* CFG_ENV_IS_IN_NAND */