summaryrefslogtreecommitdiff
path: root/common/env_sf.c
diff options
context:
space:
mode:
Diffstat (limited to 'common/env_sf.c')
-rw-r--r--common/env_sf.c132
1 files changed, 73 insertions, 59 deletions
diff --git a/common/env_sf.c b/common/env_sf.c
index 4391d61..fb0c39b 100644
--- a/common/env_sf.c
+++ b/common/env_sf.c
@@ -1,5 +1,5 @@
/*
- * (C) Copyright 2000-2002
+ * (C) Copyright 2000-2010
* Wolfgang Denk, DENX Software Engineering, wd@denx.de.
*
* (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
@@ -29,6 +29,8 @@
#include <environment.h>
#include <malloc.h>
#include <spi_flash.h>
+#include <search.h>
+#include <errno.h>
#ifndef CONFIG_ENV_SPI_BUS
# define CONFIG_ENV_SPI_BUS 0
@@ -77,17 +79,29 @@ void swap_env(void)
int saveenv(void)
{
- u32 saved_size, saved_offset;
- char *saved_buffer = NULL;
- u32 sector = 1;
- int ret;
- char flag = OBSOLETE_FLAG, new_flag = ACTIVE_FLAG;
+ env_t env_new;
+ ssize_t len;
+ char *res;
+ u32 saved_size, saved_offset;
+ char *saved_buffer = NULL;
+ u32 sector = 1;
+ int ret;
+ char flag = OBSOLETE_FLAG, new_flag = ACTIVE_FLAG;
if (!env_flash) {
puts("Environment SPI flash not initialized\n");
return 1;
}
+ res = (char *)&env_new.data;
+ len = hexport('\0', &res, ENV_SIZE);
+ if (len < 0) {
+ error("Cannot export environment: errno = %d\n", errno);
+ return 1;
+ }
+ env_new.crc = crc32(0, env_new.data, ENV_SIZE);
+ env_new.flags = ACTIVE_FLAG;
+
/* Is the sector larger than the env (i.e. embedded) */
if (CONFIG_ENV_SECT_SIZE > CONFIG_ENV_SIZE) {
saved_size = CONFIG_ENV_SECT_SIZE - CONFIG_ENV_SIZE;
@@ -118,25 +132,25 @@ int saveenv(void)
puts("Writing to SPI flash...");
ret = spi_flash_write(env_flash,
env_new_offset + offsetof(env_t, data),
- sizeof(env_ptr->data), env_ptr->data);
+ sizeof(env_new.data), env_new.data);
if (ret)
goto done;
ret = spi_flash_write(env_flash,
env_new_offset + offsetof(env_t, crc),
- sizeof(env_ptr->crc), &env_ptr->crc);
+ sizeof(env_new.crc), &env_new.crc);
if (ret)
goto done;
ret = spi_flash_write(env_flash,
env_offset + offsetof(env_t, flags),
- sizeof(env_ptr->flags), &flag);
+ sizeof(env_new.flags), &flag);
if (ret)
goto done;
ret = spi_flash_write(env_flash,
env_new_offset + offsetof(env_t, flags),
- sizeof(env_ptr->flags), &new_flag);
+ sizeof(env_new.flags), &new_flag);
if (ret)
goto done;
@@ -164,33 +178,34 @@ void env_relocate_spec(void)
int crc1_ok = 0, crc2_ok = 0;
env_t *tmp_env1 = NULL;
env_t *tmp_env2 = NULL;
+ env_t ep;
uchar flag1, flag2;
/* current_env is set only in case both areas are valid! */
int current_env = 0;
tmp_env1 = (env_t *)malloc(CONFIG_ENV_SIZE);
- if (!tmp_env1) {
- puts("*** Warning: could not init environment,"
- " using defaults\n\n");
- goto out;
- }
-
tmp_env2 = (env_t *)malloc(CONFIG_ENV_SIZE);
- if (!tmp_env2) {
- puts("*** Warning: could not init environment,"
- " using defaults\n\n");
- goto out;
+
+ if (!tmp_env1 || !tmp_env2) {
+ free(tmp_env1);
+ free(tmp_env2);
+ set_default_env("!malloc() failed");
+ return;
}
env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
- if (!env_flash)
- goto err_probe;
+ if (!env_flash) {
+ set_default_env("!spi_flash_probe() failed");
+ return;
+ }
ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
CONFIG_ENV_SIZE, tmp_env1);
- if (ret)
+ if (ret) {
+ set_default_env("!spi_flash_read() failed");
goto err_read;
+ }
if (crc32(0, tmp_env1->data, ENV_SIZE) == tmp_env1->crc)
crc1_ok = 1;
@@ -208,25 +223,25 @@ void env_relocate_spec(void)
goto err_crc;
else if (crc1_ok && !crc2_ok) {
gd->env_valid = 1;
- memcpy(env_ptr, tmp_env1, CONFIG_ENV_SIZE);
+ ep = tmp_env1;
} else if (!crc1_ok && crc2_ok) {
gd->env_valid = 1;
- memcpy(env_ptr, tmp_env2, CONFIG_ENV_SIZE);
+ ep = tmp_env2;
swap_env();
} else if (flag1 == ACTIVE_FLAG && flag2 == OBSOLETE_FLAG) {
gd->env_valid = 1;
- memcpy(env_ptr, tmp_env1, CONFIG_ENV_SIZE);
+ ep = tmp_env1;
} else if (flag1 == OBSOLETE_FLAG && flag2 == ACTIVE_FLAG) {
gd->env_valid = 1;
- memcpy(env_ptr, tmp_env2, CONFIG_ENV_SIZE);
+ ep = tmp_env2;
swap_env();
} else if (flag1 == flag2) {
gd->env_valid = 2;
- memcpy(env_ptr, tmp_env1, CONFIG_ENV_SIZE);
+ ep = tmp_env1;
current_env = 1;
} else if (flag1 == 0xFF) {
gd->env_valid = 2;
- memcpy(env_ptr, tmp_env1, CONFIG_ENV_SIZE);
+ ep = tmp_env1;
current_env = 1;
} else {
/*
@@ -234,35 +249,42 @@ void env_relocate_spec(void)
* default path is desirable.
*/
gd->env_valid = 2;
- memcpy(env_ptr, tmp_env2, CONFIG_ENV_SIZE);
+ ep = tmp_env2;
swap_env();
current_env = 2;
}
+
+ rc = env_import((char *)ep, 0);
+ if (!rc) {
+ error("Cannot import environment: errno = %d\n", errno);
+ goto out;
+ }
+
if (current_env == 1) {
if (flag2 != OBSOLETE_FLAG) {
flag2 = OBSOLETE_FLAG;
spi_flash_write(env_flash,
env_new_offset + offsetof(env_t, flags),
- sizeof(env_ptr->flags), &flag2);
+ sizeof(env_new.flags), &flag2);
}
if (flag1 != ACTIVE_FLAG) {
flag1 = ACTIVE_FLAG;
spi_flash_write(env_flash,
env_offset + offsetof(env_t, flags),
- sizeof(env_ptr->flags), &flag1);
+ sizeof(env_new.flags), &flag1);
}
} else if (current_env == 2) {
if (flag1 != OBSOLETE_FLAG) {
flag1 = OBSOLETE_FLAG;
spi_flash_write(env_flash,
env_new_offset + offsetof(env_t, flags),
- sizeof(env_ptr->flags), &flag1);
+ sizeof(env_new.flags), &flag1);
}
if (flag2 != ACTIVE_FLAG) {
flag2 = ACTIVE_FLAG;
spi_flash_write(env_flash,
env_offset + offsetof(env_t, flags),
- sizeof(env_ptr->flags), &flag2);
+ sizeof(env_new.flags), &flag2);
}
}
if (gd->env_valid == 2) {
@@ -278,15 +300,9 @@ void env_relocate_spec(void)
err_read:
spi_flash_free(env_flash);
env_flash = NULL;
-err_probe:
-err_crc:
- puts("*** Warning - bad CRC, using default environment\n\n");
out:
- if (tmp_env1)
- free(tmp_env1);
- if (tmp_env2)
- free(tmp_env2);
- set_default_env();
+ free(tmp_env1);
+ free(tmp_env2);
}
#else
int saveenv(void)
@@ -348,32 +364,30 @@ int saveenv(void)
void env_relocate_spec(void)
{
+ char buf[CONFIG_ENV_SIZE];
int ret;
env_flash = spi_flash_probe(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
CONFIG_ENV_SPI_MAX_HZ, CONFIG_ENV_SPI_MODE);
- if (!env_flash)
- goto err_probe;
-
- ret = spi_flash_read(env_flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, env_ptr);
- if (ret)
- goto err_read;
-
- if (crc32(0, env_ptr->data, ENV_SIZE) != env_ptr->crc)
- goto err_crc;
+ if (!env_flash) {
+ set_default_env("!spi_flash_probe() failed");
+ return;
+ }
- gd->env_valid = 1;
+ ret = spi_flash_read(env_flash,
+ CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
+ if (ret) {
+ set_default_env("!spi_flash_read() failed");
+ goto out;
+ }
- return;
+ ret = env_import(buf, 1);
-err_read:
+ if (ret)
+ gd->env_valid = 1;
+out:
spi_flash_free(env_flash);
env_flash = NULL;
-err_probe:
-err_crc:
- puts("*** Warning - bad CRC, using default environment\n\n");
-
- set_default_env();
}
#endif