]> git.sur5r.net Git - u-boot/blobdiff - common/env_nand.c
mod_i2c_mem() bugfix
[u-boot] / common / env_nand.c
index 89540177048a54e930c98390b1e4e940f6efe192..a8f0de7ae24b29a835771e3fb25fb8c7141a2ebc 100644 (file)
@@ -95,8 +95,8 @@ uchar env_get_char_spec (int index)
 /* this is called before nand_init()
  * so we can't read Nand to validate env data.
  * Mark it OK for now. env_relocate() in env_common.c
- * will call our relocate function which will does
- * the real validation.
+ * will call our relocate function which 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
@@ -159,22 +159,23 @@ int writeenv(size_t offset, u_char *buf)
 {
        size_t end = offset + CFG_ENV_RANGE;
        size_t amount_saved = 0;
-       size_t blocksize;
+       size_t blocksize, len;
 
        u_char *char_ptr;
 
        blocksize = nand_info[0].erasesize;
+       len = min(blocksize, CFG_ENV_SIZE);
 
        while (amount_saved < CFG_ENV_SIZE && offset < end) {
                if (nand_block_isbad(&nand_info[0], offset)) {
                        offset += blocksize;
                } else {
                        char_ptr = &buf[amount_saved];
-                       if (nand_write(&nand_info[0], offset, &blocksize,
+                       if (nand_write(&nand_info[0], offset, &len,
                                        char_ptr))
                                return 1;
                        offset += blocksize;
-                       amount_saved += blocksize;
+                       amount_saved += len;
                }
        }
        if (amount_saved != CFG_ENV_SIZE)
@@ -246,7 +247,7 @@ int saveenv(void)
 
        puts ("Writing to Nand... ");
        total = CFG_ENV_SIZE;
-       if (writeenv(CFG_ENV_OFFSET, env_ptr)) {
+       if (writeenv(CFG_ENV_OFFSET, (u_char *) env_ptr)) {
                puts("FAILED!\n");
                return 1;
        }
@@ -261,21 +262,22 @@ int readenv (size_t offset, u_char * buf)
 {
        size_t end = offset + CFG_ENV_RANGE;
        size_t amount_loaded = 0;
-       size_t blocksize;
+       size_t blocksize, len;
 
        u_char *char_ptr;
 
        blocksize = nand_info[0].erasesize;
+       len = min(blocksize, CFG_ENV_SIZE);
 
        while (amount_loaded < CFG_ENV_SIZE && offset < end) {
                if (nand_block_isbad(&nand_info[0], offset)) {
                        offset += blocksize;
                } else {
                        char_ptr = &buf[amount_loaded];
-                       if (nand_read(&nand_info[0], offset, &blocksize, char_ptr))
+                       if (nand_read(&nand_info[0], offset, &len, char_ptr))
                                return 1;
                        offset += blocksize;
-                       amount_loaded += blocksize;
+                       amount_loaded += len;
                }
        }
        if (amount_loaded != CFG_ENV_SIZE)
@@ -345,12 +347,10 @@ void env_relocate_spec (void)
 void env_relocate_spec (void)
 {
 #if !defined(ENV_IS_EMBEDDED)
-       size_t total;
        int ret;
 
-       total = CFG_ENV_SIZE;
-       ret = readenv(CFG_ENV_OFFSET, env_ptr);
-       if (ret || total != CFG_ENV_SIZE)
+       ret = readenv(CFG_ENV_OFFSET, (u_char *) env_ptr);
+       if (ret)
                return use_default();
 
        if (crc32(0, env_ptr->data, ENV_SIZE) != env_ptr->crc)
@@ -363,19 +363,7 @@ void env_relocate_spec (void)
 static void use_default()
 {
        puts ("*** Warning - bad CRC or NAND, using default environment\n\n");
-
-       if (default_environment_size > CFG_ENV_SIZE){
-               puts ("*** Error - default environment is too large\n\n");
-               return;
-       }
-
-       memset (env_ptr, 0, sizeof(env_t));
-       memcpy (env_ptr->data,
-                       default_environment,
-                       default_environment_size);
-       env_ptr->crc = crc32(0, env_ptr->data, ENV_SIZE);
-       gd->env_valid = 1;
-
+       set_default_env();
 }
 #endif