aboutsummaryrefslogtreecommitdiff
path: root/common/env_sf.c
diff options
context:
space:
mode:
authorWolfgang Denk <wd@denx.de>2010-06-20 23:33:59 +0200
committerWolfgang Denk <wd@denx.de>2010-09-19 19:29:48 +0200
commitea882baf9c17cd142c99e3ff640d3ab01daa5cec (patch)
treeedf6a32f670aa9a54572e142f22070ef6d636e4f /common/env_sf.c
parent91a76751a090bf43c166fda0815c9b5b2bfccbe9 (diff)
downloadu-boot-ea882baf9c17cd142c99e3ff640d3ab01daa5cec.zip
u-boot-ea882baf9c17cd142c99e3ff640d3ab01daa5cec.tar.gz
u-boot-ea882baf9c17cd142c99e3ff640d3ab01daa5cec.tar.bz2
New implementation for internal handling of environment variables.
Motivation: * Old environment code used a pessimizing implementation: - variable lookup used linear search => slow - changed/added variables were added at the end, i. e. most frequently used variables had the slowest access times => slow - each setenv() would calculate the CRC32 checksum over the whole environment block => slow * "redundant" envrionment was locked down to two copies * No easy way to implement features like "reset to factory defaults", or to select one out of several pre-defined (previously saved) sets of environment settings ("profiles") * No easy way to import or export environment settings ====================================================================== API Changes: - Variable names starting with '#' are no longer allowed I didn't find any such variable names being used; it is highly recommended to follow standard conventions and start variable names with an alphanumeric character - "printenv" will now print a backslash at the end of all but the last lines of a multi-line variable value. Multi-line variables have never been formally defined, allthough there is no reason not to use them. Now we define rules how to deal with them, allowing for import and export. - Function forceenv() and the related code in saveenv() was removed. At the moment this is causing build problems for the only user of this code (schmoogie - which has no entry in MAINTAINERS); may be fixed later by implementing the "env set -f" feature. Inconsistencies: - "printenv" will '\\'-escape the '\n' in multi-line variables, while "printenv var" will not do that. ====================================================================== Advantages: - "printenv" output much better readable (sorted) - faster! - extendable (additional variable properties can be added) - new, powerful features like "factory reset" or easy switching between several different environment settings ("profiles") Disadvantages: - Image size grows by typically 5...7 KiB (might shrink a bit again on systems with redundant environment with a following patch series) ====================================================================== Implemented: - env command with subcommands: - env print [arg ...] same as "printenv": print environment - env set [-f] name [arg ...] same as "setenv": set (and delete) environment variables ["-f" - force setting even for read-only variables - not implemented yet.] - end delete [-f] name not implemented yet ["-f" - force delete even for read-only variables] - env save same as "saveenv": save environment - env export [-t | -b | -c] addr [size] export internal representation (hash table) in formats usable for persistent storage or processing: -t: export as text format; if size is given, data will be padded with '\0' bytes; if not, one terminating '\0' will be added (which is included in the "filesize" setting so you can for exmple copy this to flash and keep the termination). -b: export as binary format (name=value pairs separated by '\0', list end marked by double "\0\0") -c: export as checksum protected environment format as used for example by "saveenv" command addr: memory address where environment gets stored size: size of output buffer With "-c" and size is NOT given, then the export command will format the data as currently used for the persistent storage, i. e. it will use CONFIG_ENV_SECT_SIZE as output block size and prepend a valid CRC32 checksum and, in case of resundant environment, a "current" redundancy flag. If size is given, this value will be used instead of CONFIG_ENV_SECT_SIZE; again, CRC32 checksum and redundancy flag will be inserted. With "-b" and "-t", always only the real data (including a terminating '\0' byte) will be written; here the optional size argument will be used to make sure not to overflow the user provided buffer; the command will abort if the size is not sufficient. Any remainign space will be '\0' padded. On successful return, the variable "filesize" will be set. Note that filesize includes the trailing/terminating '\0' byte(s). Usage szenario: create a text snapshot/backup of the current settings: => env export -t 100000 => era ${backup_addr} +${filesize} => cp.b 100000 ${backup_addr} ${filesize} Re-import this snapshot, deleting all other settings: => env import -d -t ${backup_addr} - env import [-d] [-t | -b | -c] addr [size] import external format (text or binary) into hash table, optionally deleting existing values: -d: delete existing environment before importing; otherwise overwrite / append to existion definitions -t: assume text format; either "size" must be given or the text data must be '\0' terminated -b: assume binary format ('\0' separated, "\0\0" terminated) -c: assume checksum protected environment format addr: memory address to read from size: length of input data; if missing, proper '\0' termination is mandatory - env default -f reset default environment: drop all environment settings and load default environment - env ask name [message] [size] same as "askenv": ask for environment variable - env edit name same as "editenv": edit environment variable - env run same as "run": run commands in an environment variable ====================================================================== TODO: - drop default env as implemented now; provide a text file based initialization instead (eventually using several text files to incrementally build it from common blocks) and a tool to convert it into a binary blob / object file. - It would be nice if we could add wildcard support for environment variables; this is needed for variable name auto-completion, but it would also be nice to be able to say "printenv ip*" or "printenv *addr*" - Some boards don't link any more due to the grown code size: DU405, canyonlands, sequoia, socrates. => cc: Matthias Fuchs <matthias.fuchs@esd-electronics.com>, Stefan Roese <sr@denx.de>, Heiko Schocher <hs@denx.de> - Dropping forceenv() causes build problems on schmoogie => cc: Sergey Kubushyn <ksi@koi8.net> - Build tested on PPC and ARM only; runtime tested with NOR and NAND flash only => needs testing!! Signed-off-by: Wolfgang Denk <wd@denx.de> Cc: Matthias Fuchs <matthias.fuchs@esd-electronics.com>, Cc: Stefan Roese <sr@denx.de>, Cc: Heiko Schocher <hs@denx.de> Cc: Sergey Kubushyn <ksi@koi8.net>
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