2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2008-05-16 09:10:35 +00:00
|
|
|
/*
|
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>
2010-06-20 21:33:59 +00:00
|
|
|
* (C) Copyright 2000-2010
|
2008-05-16 09:10:35 +00:00
|
|
|
* Wolfgang Denk, DENX Software Engineering, wd@denx.de.
|
|
|
|
*
|
|
|
|
* (C) Copyright 2001 Sysgo Real-Time Solutions, GmbH <www.elinos.com>
|
|
|
|
* Andreas Heppel <aheppel@sysgo.de>
|
|
|
|
*
|
|
|
|
* (C) Copyright 2008 Atmel Corporation
|
|
|
|
*/
|
|
|
|
#include <common.h>
|
2017-05-17 23:18:03 +00:00
|
|
|
#include <dm.h>
|
2019-08-01 15:47:00 +00:00
|
|
|
#include <env.h>
|
2019-08-02 15:44:25 +00:00
|
|
|
#include <env_internal.h>
|
2020-05-10 17:39:54 +00:00
|
|
|
#include <flash.h>
|
2008-12-11 11:23:37 +00:00
|
|
|
#include <malloc.h>
|
2014-10-14 05:41:55 +00:00
|
|
|
#include <spi.h>
|
2008-05-16 09:10:35 +00:00
|
|
|
#include <spi_flash.h>
|
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>
2010-06-20 21:33:59 +00:00
|
|
|
#include <search.h>
|
|
|
|
#include <errno.h>
|
2020-05-10 17:39:54 +00:00
|
|
|
#include <uuid.h>
|
2020-05-10 17:39:56 +00:00
|
|
|
#include <asm/cache.h>
|
2020-10-31 03:38:53 +00:00
|
|
|
#include <asm/global_data.h>
|
2016-01-26 07:06:42 +00:00
|
|
|
#include <dm/device-internal.h>
|
2019-11-14 19:57:16 +00:00
|
|
|
#include <u-boot/crc.h>
|
2008-05-16 09:10:35 +00:00
|
|
|
|
2021-02-09 10:48:52 +00:00
|
|
|
#define OFFSET_INVALID (~(u32)0)
|
|
|
|
|
2010-04-23 15:22:55 +00:00
|
|
|
#ifdef CONFIG_ENV_OFFSET_REDUND
|
2021-02-09 10:48:52 +00:00
|
|
|
#define ENV_OFFSET_REDUND CONFIG_ENV_OFFSET_REDUND
|
|
|
|
|
2011-11-07 01:14:08 +00:00
|
|
|
static ulong env_offset = CONFIG_ENV_OFFSET;
|
|
|
|
static ulong env_new_offset = CONFIG_ENV_OFFSET_REDUND;
|
2021-02-09 10:48:52 +00:00
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#define ENV_OFFSET_REDUND OFFSET_INVALID
|
|
|
|
|
2010-10-27 09:06:20 +00:00
|
|
|
#endif /* CONFIG_ENV_OFFSET_REDUND */
|
2010-04-23 15:22:55 +00:00
|
|
|
|
2008-05-16 09:10:35 +00:00
|
|
|
DECLARE_GLOBAL_DATA_PTR;
|
|
|
|
|
2021-02-24 10:52:36 +00:00
|
|
|
static int setup_flash_device(struct spi_flash **env_flash)
|
2010-04-23 15:22:55 +00:00
|
|
|
{
|
2020-06-04 15:11:53 +00:00
|
|
|
#if CONFIG_IS_ENABLED(DM_SPI_FLASH)
|
2016-01-26 07:06:42 +00:00
|
|
|
struct udevice *new;
|
2017-04-08 09:59:31 +00:00
|
|
|
int ret;
|
2016-01-26 07:06:42 +00:00
|
|
|
|
2016-07-06 04:34:28 +00:00
|
|
|
/* speed and mode will be read from DT */
|
2016-01-26 07:06:42 +00:00
|
|
|
ret = spi_flash_probe_bus_cs(CONFIG_ENV_SPI_BUS, CONFIG_ENV_SPI_CS,
|
2018-08-29 13:34:52 +00:00
|
|
|
&new);
|
2016-01-26 07:06:42 +00:00
|
|
|
if (ret) {
|
2019-08-01 15:47:00 +00:00
|
|
|
env_set_default("spi_flash_probe_bus_cs() failed", 0);
|
2017-08-03 18:22:17 +00:00
|
|
|
return ret;
|
2016-01-26 07:06:42 +00:00
|
|
|
}
|
|
|
|
|
2021-02-24 10:52:36 +00:00
|
|
|
*env_flash = dev_get_uclass_priv(new);
|
2016-01-26 07:06:42 +00:00
|
|
|
#else
|
2021-02-24 10:52:36 +00:00
|
|
|
*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) {
|
2020-05-13 11:02:42 +00:00
|
|
|
env_set_default("spi_flash_probe() failed", 0);
|
|
|
|
return -EIO;
|
2010-04-23 15:22:55 +00:00
|
|
|
}
|
2016-01-26 07:06:42 +00:00
|
|
|
#endif
|
2017-04-08 09:59:31 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(CONFIG_ENV_OFFSET_REDUND)
|
2017-08-03 18:22:01 +00:00
|
|
|
static int env_sf_save(void)
|
2017-04-08 09:59:31 +00:00
|
|
|
{
|
|
|
|
env_t env_new;
|
2019-08-01 15:47:08 +00:00
|
|
|
char *saved_buffer = NULL, flag = ENV_REDUND_OBSOLETE;
|
env: add CONFIG_ENV_SECT_SIZE_AUTO
This is roughly the U-Boot side equivalent to commit
e282c422e0 (tools: fw_env: use erasesize from MEMGETINFO ioctl). The
motivation is the case where one has a board with several revisions,
where the SPI flashes have different erase sizes.
In our case, we have an 8K environment, and the flashes have erase
sizes of 4K (newer boards) and 64K (older boards). Currently, we must
set CONFIG_ENV_SECT_SIZE to 64K to make the code work on the older
boards, but for the newer ones, that ends up wasting quite a bit of
time reading/erasing/restoring the last 56K.
At first, I wanted to allow setting CONFIG_ENV_SECT_SIZE to 0 to mean
"use the erase size the chip reports", but that config
option is used in a number of preprocessor conditionals, and shared
between ENV_IS_IN_FLASH and ENV_IS_IN_SPI_FLASH.
So instead, introduce a new boolean config option, which for now can
only be used with ENV_IS_IN_SPI_FLASH. If left off, there's no change
in behaviour.
The only slightly annoying detail is that, when selected, the compiler
is apparently not smart enough to see that the the saved_size and
saved_offset variables are only used under the same "if (sect_size >
CONFIG_ENV_SIZE)" condition as where they are computed, so we need to
initialize them to 0 to avoid "may be used uninitialized" warnings.
On our newer boards with the 4K erase size, saving the environment now
takes 0.080 seconds instead of 0.53 seconds, which directly translates
to that much faster boot time since our logic always causes the
environment to be written during boot.
Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
2021-04-14 18:51:43 +00:00
|
|
|
u32 saved_size = 0, saved_offset = 0, sector;
|
2021-04-14 18:51:42 +00:00
|
|
|
u32 sect_size = CONFIG_ENV_SECT_SIZE;
|
2017-04-08 09:59:31 +00:00
|
|
|
int ret;
|
2021-02-24 10:52:36 +00:00
|
|
|
struct spi_flash *env_flash;
|
2017-04-08 09:59:31 +00:00
|
|
|
|
2021-02-24 10:52:36 +00:00
|
|
|
ret = setup_flash_device(&env_flash);
|
2017-04-08 09:59:31 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2010-04-23 15:22:55 +00:00
|
|
|
|
env: add CONFIG_ENV_SECT_SIZE_AUTO
This is roughly the U-Boot side equivalent to commit
e282c422e0 (tools: fw_env: use erasesize from MEMGETINFO ioctl). The
motivation is the case where one has a board with several revisions,
where the SPI flashes have different erase sizes.
In our case, we have an 8K environment, and the flashes have erase
sizes of 4K (newer boards) and 64K (older boards). Currently, we must
set CONFIG_ENV_SECT_SIZE to 64K to make the code work on the older
boards, but for the newer ones, that ends up wasting quite a bit of
time reading/erasing/restoring the last 56K.
At first, I wanted to allow setting CONFIG_ENV_SECT_SIZE to 0 to mean
"use the erase size the chip reports", but that config
option is used in a number of preprocessor conditionals, and shared
between ENV_IS_IN_FLASH and ENV_IS_IN_SPI_FLASH.
So instead, introduce a new boolean config option, which for now can
only be used with ENV_IS_IN_SPI_FLASH. If left off, there's no change
in behaviour.
The only slightly annoying detail is that, when selected, the compiler
is apparently not smart enough to see that the the saved_size and
saved_offset variables are only used under the same "if (sect_size >
CONFIG_ENV_SIZE)" condition as where they are computed, so we need to
initialize them to 0 to avoid "may be used uninitialized" warnings.
On our newer boards with the 4K erase size, saving the environment now
takes 0.080 seconds instead of 0.53 seconds, which directly translates
to that much faster boot time since our logic always causes the
environment to be written during boot.
Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
2021-04-14 18:51:43 +00:00
|
|
|
if (IS_ENABLED(CONFIG_ENV_SECT_SIZE_AUTO))
|
|
|
|
sect_size = env_flash->mtd.erasesize;
|
|
|
|
|
2014-03-05 18:59:50 +00:00
|
|
|
ret = env_export(&env_new);
|
|
|
|
if (ret)
|
2017-08-03 18:22:17 +00:00
|
|
|
return -EIO;
|
2019-08-01 15:47:08 +00:00
|
|
|
env_new.flags = ENV_REDUND_ACTIVE;
|
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>
2010-06-20 21:33:59 +00:00
|
|
|
|
2017-08-03 18:21:56 +00:00
|
|
|
if (gd->env_valid == ENV_VALID) {
|
2010-10-27 09:06:20 +00:00
|
|
|
env_new_offset = CONFIG_ENV_OFFSET_REDUND;
|
|
|
|
env_offset = CONFIG_ENV_OFFSET;
|
|
|
|
} else {
|
|
|
|
env_new_offset = CONFIG_ENV_OFFSET;
|
|
|
|
env_offset = CONFIG_ENV_OFFSET_REDUND;
|
|
|
|
}
|
|
|
|
|
2010-04-23 15:22:55 +00:00
|
|
|
/* Is the sector larger than the env (i.e. embedded) */
|
2021-04-14 18:51:42 +00:00
|
|
|
if (sect_size > CONFIG_ENV_SIZE) {
|
|
|
|
saved_size = sect_size - CONFIG_ENV_SIZE;
|
2010-04-23 15:22:55 +00:00
|
|
|
saved_offset = env_new_offset + CONFIG_ENV_SIZE;
|
2015-08-17 07:59:49 +00:00
|
|
|
saved_buffer = memalign(ARCH_DMA_MINALIGN, saved_size);
|
2010-04-23 15:22:55 +00:00
|
|
|
if (!saved_buffer) {
|
2017-08-03 18:22:17 +00:00
|
|
|
ret = -ENOMEM;
|
2010-04-23 15:22:55 +00:00
|
|
|
goto done;
|
|
|
|
}
|
2019-03-13 11:15:45 +00:00
|
|
|
ret = spi_flash_read(env_flash, saved_offset,
|
|
|
|
saved_size, saved_buffer);
|
2010-04-23 15:22:55 +00:00
|
|
|
if (ret)
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2021-04-14 18:51:42 +00:00
|
|
|
sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, sect_size);
|
2010-04-23 15:22:55 +00:00
|
|
|
|
|
|
|
puts("Erasing SPI flash...");
|
|
|
|
ret = spi_flash_erase(env_flash, env_new_offset,
|
2021-04-14 18:51:42 +00:00
|
|
|
sector * sect_size);
|
2010-04-23 15:22:55 +00:00
|
|
|
if (ret)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
puts("Writing to SPI flash...");
|
|
|
|
|
2010-10-27 09:06:20 +00:00
|
|
|
ret = spi_flash_write(env_flash, env_new_offset,
|
2013-04-05 18:55:21 +00:00
|
|
|
CONFIG_ENV_SIZE, &env_new);
|
2010-04-23 15:22:55 +00:00
|
|
|
if (ret)
|
|
|
|
goto done;
|
|
|
|
|
2021-04-14 18:51:42 +00:00
|
|
|
if (sect_size > CONFIG_ENV_SIZE) {
|
2010-04-23 15:22:55 +00:00
|
|
|
ret = spi_flash_write(env_flash, saved_offset,
|
|
|
|
saved_size, saved_buffer);
|
|
|
|
if (ret)
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2011-11-07 01:14:08 +00:00
|
|
|
ret = spi_flash_write(env_flash, env_offset + offsetof(env_t, flags),
|
2013-04-05 18:55:21 +00:00
|
|
|
sizeof(env_new.flags), &flag);
|
2010-10-27 09:06:20 +00:00
|
|
|
if (ret)
|
|
|
|
goto done;
|
2010-04-23 15:22:55 +00:00
|
|
|
|
|
|
|
puts("done\n");
|
|
|
|
|
2017-08-03 18:21:56 +00:00
|
|
|
gd->env_valid = gd->env_valid == ENV_REDUND ? ENV_VALID : ENV_REDUND;
|
2010-10-27 09:06:20 +00:00
|
|
|
|
2011-03-29 02:35:14 +00:00
|
|
|
printf("Valid environment: %d\n", (int)gd->env_valid);
|
2010-10-27 09:06:20 +00:00
|
|
|
|
2021-01-04 13:41:31 +00:00
|
|
|
done:
|
2021-02-24 10:52:35 +00:00
|
|
|
spi_flash_free(env_flash);
|
|
|
|
|
2010-04-23 15:22:55 +00:00
|
|
|
if (saved_buffer)
|
|
|
|
free(saved_buffer);
|
2011-11-07 01:14:08 +00:00
|
|
|
|
2010-04-23 15:22:55 +00:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-08-03 18:22:17 +00:00
|
|
|
static int env_sf_load(void)
|
2010-04-23 15:22:55 +00:00
|
|
|
{
|
|
|
|
int ret;
|
2018-01-31 13:47:13 +00:00
|
|
|
int read1_fail, read2_fail;
|
|
|
|
env_t *tmp_env1, *tmp_env2;
|
2021-02-24 10:52:36 +00:00
|
|
|
struct spi_flash *env_flash;
|
2010-04-23 15:22:55 +00:00
|
|
|
|
2015-08-17 07:59:49 +00:00
|
|
|
tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
|
|
|
|
CONFIG_ENV_SIZE);
|
|
|
|
tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
|
|
|
|
CONFIG_ENV_SIZE);
|
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>
2010-06-20 21:33:59 +00:00
|
|
|
if (!tmp_env1 || !tmp_env2) {
|
2019-08-01 15:47:00 +00:00
|
|
|
env_set_default("malloc() failed", 0);
|
2017-08-03 18:22:17 +00:00
|
|
|
ret = -EIO;
|
2011-03-29 02:35:14 +00:00
|
|
|
goto out;
|
2010-04-23 15:22:55 +00:00
|
|
|
}
|
|
|
|
|
2021-02-24 10:52:36 +00:00
|
|
|
ret = setup_flash_device(&env_flash);
|
2017-04-08 09:59:32 +00:00
|
|
|
if (ret)
|
2011-03-29 02:35:14 +00:00
|
|
|
goto out;
|
2010-04-23 15:22:55 +00:00
|
|
|
|
2019-03-13 11:15:45 +00:00
|
|
|
read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
|
|
|
|
CONFIG_ENV_SIZE, tmp_env1);
|
|
|
|
read2_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET_REDUND,
|
|
|
|
CONFIG_ENV_SIZE, tmp_env2);
|
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>
2010-06-20 21:33:59 +00:00
|
|
|
|
2018-01-31 13:47:13 +00:00
|
|
|
ret = env_import_redund((char *)tmp_env1, read1_fail, (char *)tmp_env2,
|
2020-07-07 18:51:35 +00:00
|
|
|
read2_fail, H_EXTERNAL);
|
2010-04-23 15:22:55 +00:00
|
|
|
|
|
|
|
spi_flash_free(env_flash);
|
|
|
|
out:
|
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>
2010-06-20 21:33:59 +00:00
|
|
|
free(tmp_env1);
|
|
|
|
free(tmp_env2);
|
2017-08-03 18:22:17 +00:00
|
|
|
|
|
|
|
return ret;
|
2010-04-23 15:22:55 +00:00
|
|
|
}
|
|
|
|
#else
|
2017-08-03 18:22:01 +00:00
|
|
|
static int env_sf_save(void)
|
2008-05-16 09:10:35 +00:00
|
|
|
{
|
env: add CONFIG_ENV_SECT_SIZE_AUTO
This is roughly the U-Boot side equivalent to commit
e282c422e0 (tools: fw_env: use erasesize from MEMGETINFO ioctl). The
motivation is the case where one has a board with several revisions,
where the SPI flashes have different erase sizes.
In our case, we have an 8K environment, and the flashes have erase
sizes of 4K (newer boards) and 64K (older boards). Currently, we must
set CONFIG_ENV_SECT_SIZE to 64K to make the code work on the older
boards, but for the newer ones, that ends up wasting quite a bit of
time reading/erasing/restoring the last 56K.
At first, I wanted to allow setting CONFIG_ENV_SECT_SIZE to 0 to mean
"use the erase size the chip reports", but that config
option is used in a number of preprocessor conditionals, and shared
between ENV_IS_IN_FLASH and ENV_IS_IN_SPI_FLASH.
So instead, introduce a new boolean config option, which for now can
only be used with ENV_IS_IN_SPI_FLASH. If left off, there's no change
in behaviour.
The only slightly annoying detail is that, when selected, the compiler
is apparently not smart enough to see that the the saved_size and
saved_offset variables are only used under the same "if (sect_size >
CONFIG_ENV_SIZE)" condition as where they are computed, so we need to
initialize them to 0 to avoid "may be used uninitialized" warnings.
On our newer boards with the 4K erase size, saving the environment now
takes 0.080 seconds instead of 0.53 seconds, which directly translates
to that much faster boot time since our logic always causes the
environment to be written during boot.
Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
2021-04-14 18:51:43 +00:00
|
|
|
u32 saved_size = 0, saved_offset = 0, sector;
|
2021-04-14 18:51:42 +00:00
|
|
|
u32 sect_size = CONFIG_ENV_SECT_SIZE;
|
2014-03-05 18:59:50 +00:00
|
|
|
char *saved_buffer = NULL;
|
2011-11-07 01:14:08 +00:00
|
|
|
int ret = 1;
|
2013-04-05 18:55:21 +00:00
|
|
|
env_t env_new;
|
2021-02-24 10:52:36 +00:00
|
|
|
struct spi_flash *env_flash;
|
2016-01-26 07:06:42 +00:00
|
|
|
|
2021-02-24 10:52:36 +00:00
|
|
|
ret = setup_flash_device(&env_flash);
|
2017-04-08 09:59:31 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
2008-05-16 09:10:35 +00:00
|
|
|
|
env: add CONFIG_ENV_SECT_SIZE_AUTO
This is roughly the U-Boot side equivalent to commit
e282c422e0 (tools: fw_env: use erasesize from MEMGETINFO ioctl). The
motivation is the case where one has a board with several revisions,
where the SPI flashes have different erase sizes.
In our case, we have an 8K environment, and the flashes have erase
sizes of 4K (newer boards) and 64K (older boards). Currently, we must
set CONFIG_ENV_SECT_SIZE to 64K to make the code work on the older
boards, but for the newer ones, that ends up wasting quite a bit of
time reading/erasing/restoring the last 56K.
At first, I wanted to allow setting CONFIG_ENV_SECT_SIZE to 0 to mean
"use the erase size the chip reports", but that config
option is used in a number of preprocessor conditionals, and shared
between ENV_IS_IN_FLASH and ENV_IS_IN_SPI_FLASH.
So instead, introduce a new boolean config option, which for now can
only be used with ENV_IS_IN_SPI_FLASH. If left off, there's no change
in behaviour.
The only slightly annoying detail is that, when selected, the compiler
is apparently not smart enough to see that the the saved_size and
saved_offset variables are only used under the same "if (sect_size >
CONFIG_ENV_SIZE)" condition as where they are computed, so we need to
initialize them to 0 to avoid "may be used uninitialized" warnings.
On our newer boards with the 4K erase size, saving the environment now
takes 0.080 seconds instead of 0.53 seconds, which directly translates
to that much faster boot time since our logic always causes the
environment to be written during boot.
Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
2021-04-14 18:51:43 +00:00
|
|
|
if (IS_ENABLED(CONFIG_ENV_SECT_SIZE_AUTO))
|
|
|
|
sect_size = env_flash->mtd.erasesize;
|
|
|
|
|
2008-12-11 11:23:37 +00:00
|
|
|
/* Is the sector larger than the env (i.e. embedded) */
|
2021-04-14 18:51:42 +00:00
|
|
|
if (sect_size > CONFIG_ENV_SIZE) {
|
|
|
|
saved_size = sect_size - CONFIG_ENV_SIZE;
|
2008-12-11 11:23:37 +00:00
|
|
|
saved_offset = CONFIG_ENV_OFFSET + CONFIG_ENV_SIZE;
|
|
|
|
saved_buffer = malloc(saved_size);
|
2011-11-07 01:14:08 +00:00
|
|
|
if (!saved_buffer)
|
2008-12-11 11:23:37 +00:00
|
|
|
goto done;
|
2011-11-07 01:14:08 +00:00
|
|
|
|
2019-03-13 11:15:45 +00:00
|
|
|
ret = spi_flash_read(env_flash, saved_offset,
|
|
|
|
saved_size, saved_buffer);
|
2008-12-11 11:23:37 +00:00
|
|
|
if (ret)
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
2014-03-05 18:59:50 +00:00
|
|
|
ret = env_export(&env_new);
|
|
|
|
if (ret)
|
2010-10-27 09:06:20 +00:00
|
|
|
goto done;
|
|
|
|
|
2021-04-14 18:51:42 +00:00
|
|
|
sector = DIV_ROUND_UP(CONFIG_ENV_SIZE, sect_size);
|
2017-04-08 09:59:34 +00:00
|
|
|
|
2008-05-16 09:10:35 +00:00
|
|
|
puts("Erasing SPI flash...");
|
2010-10-27 09:06:20 +00:00
|
|
|
ret = spi_flash_erase(env_flash, CONFIG_ENV_OFFSET,
|
2021-04-14 18:51:42 +00:00
|
|
|
sector * sect_size);
|
2008-12-11 11:23:37 +00:00
|
|
|
if (ret)
|
|
|
|
goto done;
|
2008-05-16 09:10:35 +00:00
|
|
|
|
|
|
|
puts("Writing to SPI flash...");
|
2010-10-27 09:06:20 +00:00
|
|
|
ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET,
|
2013-04-05 18:55:21 +00:00
|
|
|
CONFIG_ENV_SIZE, &env_new);
|
2008-12-11 11:23:37 +00:00
|
|
|
if (ret)
|
|
|
|
goto done;
|
2008-05-16 09:10:35 +00:00
|
|
|
|
2021-04-14 18:51:42 +00:00
|
|
|
if (sect_size > CONFIG_ENV_SIZE) {
|
2010-10-27 09:06:20 +00:00
|
|
|
ret = spi_flash_write(env_flash, saved_offset,
|
|
|
|
saved_size, saved_buffer);
|
2008-12-11 11:23:37 +00:00
|
|
|
if (ret)
|
|
|
|
goto done;
|
|
|
|
}
|
|
|
|
|
|
|
|
ret = 0;
|
2008-05-16 09:10:35 +00:00
|
|
|
puts("done\n");
|
2008-12-11 11:23:37 +00:00
|
|
|
|
2021-01-04 13:41:31 +00:00
|
|
|
done:
|
2021-02-24 10:52:35 +00:00
|
|
|
spi_flash_free(env_flash);
|
|
|
|
|
2008-12-11 11:23:37 +00:00
|
|
|
if (saved_buffer)
|
|
|
|
free(saved_buffer);
|
2011-11-07 01:14:08 +00:00
|
|
|
|
2008-12-11 11:23:37 +00:00
|
|
|
return ret;
|
2008-05-16 09:10:35 +00:00
|
|
|
}
|
|
|
|
|
2017-08-03 18:22:17 +00:00
|
|
|
static int env_sf_load(void)
|
2008-05-16 09:10:35 +00:00
|
|
|
{
|
|
|
|
int ret;
|
2014-01-24 07:50:07 +00:00
|
|
|
char *buf = NULL;
|
2021-02-24 10:52:36 +00:00
|
|
|
struct spi_flash *env_flash;
|
2008-05-16 09:10:35 +00:00
|
|
|
|
2015-08-17 07:59:49 +00:00
|
|
|
buf = (char *)memalign(ARCH_DMA_MINALIGN, CONFIG_ENV_SIZE);
|
2017-04-08 09:59:33 +00:00
|
|
|
if (!buf) {
|
2019-08-01 15:47:00 +00:00
|
|
|
env_set_default("malloc() failed", 0);
|
2017-08-03 18:22:17 +00:00
|
|
|
return -EIO;
|
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>
2010-06-20 21:33:59 +00:00
|
|
|
}
|
2008-05-16 09:10:35 +00:00
|
|
|
|
2021-02-24 10:52:36 +00:00
|
|
|
ret = setup_flash_device(&env_flash);
|
2017-04-08 09:59:33 +00:00
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
|
2019-03-13 11:15:45 +00:00
|
|
|
ret = spi_flash_read(env_flash,
|
|
|
|
CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, buf);
|
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>
2010-06-20 21:33:59 +00:00
|
|
|
if (ret) {
|
2019-08-01 15:47:00 +00:00
|
|
|
env_set_default("spi_flash_read() failed", 0);
|
2017-04-08 09:59:33 +00:00
|
|
|
goto err_read;
|
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>
2010-06-20 21:33:59 +00:00
|
|
|
}
|
2008-05-16 09:10:35 +00:00
|
|
|
|
2020-07-07 18:51:35 +00:00
|
|
|
ret = env_import(buf, 1, H_EXTERNAL);
|
2018-01-31 13:47:10 +00:00
|
|
|
if (!ret)
|
2017-08-03 18:21:56 +00:00
|
|
|
gd->env_valid = ENV_VALID;
|
2017-04-08 09:59:33 +00:00
|
|
|
|
|
|
|
err_read:
|
2008-05-16 09:10:35 +00:00
|
|
|
spi_flash_free(env_flash);
|
2017-04-08 09:59:33 +00:00
|
|
|
out:
|
|
|
|
free(buf);
|
2017-08-03 18:22:17 +00:00
|
|
|
|
|
|
|
return ret;
|
2008-05-16 09:10:35 +00:00
|
|
|
}
|
2010-04-23 15:22:55 +00:00
|
|
|
#endif
|
2008-05-16 09:10:35 +00:00
|
|
|
|
2021-02-09 10:48:52 +00:00
|
|
|
static int env_sf_erase(void)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
env_t env;
|
2021-02-24 10:52:36 +00:00
|
|
|
struct spi_flash *env_flash;
|
2021-02-09 10:48:52 +00:00
|
|
|
|
2021-02-24 10:52:36 +00:00
|
|
|
ret = setup_flash_device(&env_flash);
|
2021-02-09 10:48:52 +00:00
|
|
|
if (ret)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
memset(&env, 0, sizeof(env_t));
|
|
|
|
ret = spi_flash_write(env_flash, CONFIG_ENV_OFFSET, CONFIG_ENV_SIZE, &env);
|
|
|
|
if (ret)
|
|
|
|
goto done;
|
|
|
|
|
|
|
|
if (ENV_OFFSET_REDUND != OFFSET_INVALID)
|
|
|
|
ret = spi_flash_write(env_flash, ENV_OFFSET_REDUND, CONFIG_ENV_SIZE, &env);
|
|
|
|
|
|
|
|
done:
|
|
|
|
spi_flash_free(env_flash);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-11-05 18:01:15 +00:00
|
|
|
__weak void *env_sf_get_env_addr(void)
|
|
|
|
{
|
2022-03-02 11:47:51 +00:00
|
|
|
#ifndef CONFIG_SPL_BUILD
|
2018-11-05 18:01:15 +00:00
|
|
|
return (void *)CONFIG_ENV_ADDR;
|
2022-03-02 11:47:51 +00:00
|
|
|
#else
|
|
|
|
return NULL;
|
2018-11-05 18:01:15 +00:00
|
|
|
#endif
|
2022-03-02 11:47:51 +00:00
|
|
|
}
|
2018-11-05 18:01:15 +00:00
|
|
|
|
2020-10-10 08:28:05 +00:00
|
|
|
/*
|
|
|
|
* check if Environment on CONFIG_ENV_ADDR is valid.
|
|
|
|
*/
|
|
|
|
static int env_sf_init_addr(void)
|
2017-12-14 12:07:08 +00:00
|
|
|
{
|
2018-11-05 18:01:15 +00:00
|
|
|
env_t *env_ptr = (env_t *)env_sf_get_env_addr();
|
2017-12-14 12:07:08 +00:00
|
|
|
|
2022-03-02 11:47:51 +00:00
|
|
|
if (!env_ptr)
|
|
|
|
return -ENOENT;
|
|
|
|
|
2017-12-14 12:07:08 +00:00
|
|
|
if (crc32(0, env_ptr->data, ENV_SIZE) == env_ptr->crc) {
|
2021-10-22 13:47:12 +00:00
|
|
|
gd->env_addr = (ulong)&(env_ptr->data);
|
2021-10-22 13:47:13 +00:00
|
|
|
gd->env_valid = ENV_VALID;
|
2017-12-14 12:07:08 +00:00
|
|
|
} else {
|
2021-10-22 13:47:14 +00:00
|
|
|
gd->env_valid = ENV_INVALID;
|
2017-12-14 12:07:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-10-10 08:28:05 +00:00
|
|
|
#if defined(CONFIG_ENV_SPI_EARLY)
|
|
|
|
/*
|
|
|
|
* early load environment from SPI flash (before relocation)
|
|
|
|
* and check if it is valid.
|
|
|
|
*/
|
|
|
|
static int env_sf_init_early(void)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
int read1_fail;
|
|
|
|
int read2_fail;
|
|
|
|
int crc1_ok;
|
|
|
|
env_t *tmp_env2 = NULL;
|
|
|
|
env_t *tmp_env1;
|
2021-02-24 10:52:36 +00:00
|
|
|
struct spi_flash *env_flash;
|
2020-10-10 08:28:05 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* if malloc is not ready yet, we cannot use
|
|
|
|
* this part yet.
|
|
|
|
*/
|
|
|
|
if (!gd->malloc_limit)
|
|
|
|
return -ENOENT;
|
|
|
|
|
|
|
|
tmp_env1 = (env_t *)memalign(ARCH_DMA_MINALIGN,
|
|
|
|
CONFIG_ENV_SIZE);
|
|
|
|
if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT))
|
|
|
|
tmp_env2 = (env_t *)memalign(ARCH_DMA_MINALIGN,
|
|
|
|
CONFIG_ENV_SIZE);
|
|
|
|
|
|
|
|
if (!tmp_env1 || !tmp_env2)
|
|
|
|
goto out;
|
|
|
|
|
2021-02-24 10:52:36 +00:00
|
|
|
ret = setup_flash_device(&env_flash);
|
2020-10-10 08:28:05 +00:00
|
|
|
if (ret)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
read1_fail = spi_flash_read(env_flash, CONFIG_ENV_OFFSET,
|
|
|
|
CONFIG_ENV_SIZE, tmp_env1);
|
|
|
|
|
|
|
|
if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT)) {
|
|
|
|
read2_fail = spi_flash_read(env_flash,
|
|
|
|
CONFIG_ENV_OFFSET_REDUND,
|
|
|
|
CONFIG_ENV_SIZE, tmp_env2);
|
|
|
|
ret = env_check_redund((char *)tmp_env1, read1_fail,
|
|
|
|
(char *)tmp_env2, read2_fail);
|
|
|
|
|
2020-12-17 23:19:18 +00:00
|
|
|
if (ret < 0)
|
2020-10-10 08:28:05 +00:00
|
|
|
goto err_read;
|
|
|
|
|
|
|
|
if (gd->env_valid == ENV_VALID)
|
|
|
|
gd->env_addr = (unsigned long)&tmp_env1->data;
|
|
|
|
else
|
|
|
|
gd->env_addr = (unsigned long)&tmp_env2->data;
|
|
|
|
} else {
|
|
|
|
if (read1_fail)
|
|
|
|
goto err_read;
|
|
|
|
|
|
|
|
crc1_ok = crc32(0, tmp_env1->data, ENV_SIZE) ==
|
|
|
|
tmp_env1->crc;
|
|
|
|
if (!crc1_ok)
|
|
|
|
goto err_read;
|
|
|
|
|
|
|
|
/* if valid -> this is our env */
|
|
|
|
gd->env_valid = ENV_VALID;
|
|
|
|
gd->env_addr = (unsigned long)&tmp_env1->data;
|
|
|
|
}
|
|
|
|
|
2021-02-24 10:52:35 +00:00
|
|
|
spi_flash_free(env_flash);
|
|
|
|
|
2020-10-10 08:28:05 +00:00
|
|
|
return 0;
|
|
|
|
err_read:
|
|
|
|
spi_flash_free(env_flash);
|
2021-02-24 10:52:36 +00:00
|
|
|
|
2020-10-10 08:28:05 +00:00
|
|
|
free(tmp_env1);
|
|
|
|
if (IS_ENABLED(CONFIG_SYS_REDUNDAND_ENVIRONMENT))
|
|
|
|
free(tmp_env2);
|
|
|
|
out:
|
|
|
|
/* env is not valid. always return 0 */
|
|
|
|
gd->env_valid = ENV_INVALID;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static int env_sf_init(void)
|
|
|
|
{
|
2022-03-02 11:47:51 +00:00
|
|
|
int ret = env_sf_init_addr();
|
|
|
|
if (ret != -ENOENT)
|
|
|
|
return ret;
|
|
|
|
#ifdef CONFIG_ENV_SPI_EARLY
|
2020-10-10 08:28:05 +00:00
|
|
|
return env_sf_init_early();
|
|
|
|
#endif
|
|
|
|
/*
|
2020-11-03 14:22:36 +00:00
|
|
|
* return here -ENOENT, so env_init()
|
|
|
|
* can set the init bit and later if no
|
|
|
|
* other Environment storage is defined
|
|
|
|
* can set the default environment
|
2020-10-10 08:28:05 +00:00
|
|
|
*/
|
2020-11-03 14:22:36 +00:00
|
|
|
return -ENOENT;
|
2020-10-10 08:28:05 +00:00
|
|
|
}
|
|
|
|
|
2017-08-03 18:21:58 +00:00
|
|
|
U_BOOT_ENV_LOCATION(sf) = {
|
|
|
|
.location = ENVL_SPI_FLASH,
|
2020-07-28 09:51:16 +00:00
|
|
|
ENV_NAME("SPIFlash")
|
2017-08-03 18:22:01 +00:00
|
|
|
.load = env_sf_load,
|
2021-02-09 10:48:51 +00:00
|
|
|
.save = ENV_SAVE_PTR(env_sf_save),
|
2021-02-09 10:48:52 +00:00
|
|
|
.erase = ENV_ERASE_PTR(env_sf_erase),
|
2017-12-14 12:07:08 +00:00
|
|
|
.init = env_sf_init,
|
2017-08-03 18:21:58 +00:00
|
|
|
};
|