History log of /u-boot/lib/hashtable.c
Revision Date Author Comments
(<<< Hide modified files)
(Show modified files >>>)
# 467382ca 14-Dec-2023 Tom Rini <trini@konsulko.com>

lib: Remove <common.h> inclusion from these files

After some header file cleanups to add missing include files, remove
common.h from all files in the lib directory. This primarily means just
dropping the line but in a few cases we need to add in other header
files now.

Reviewed-by: Simon Glass <sjg@chromium.org>
Signed-off-by: Tom Rini <trini@konsulko.com>

# 28de1e06 05-Feb-2023 Simon Glass <sjg@chromium.org>

Correct SPL use of ENV_WRITEABLE_LIST

This converts 1 usage of this option to the non-SPL form, since there is
no SPL_ENV_WRITEABLE_LIST defined in Kconfig

Signed-off-by: Simon Glass <sjg@chromium.org>

# 82e0b51c 12-Jun-2022 Tom Rini <trini@konsulko.com>

Convert CONFIG_ENV_MIN_ENTRIES et al to Kconfig

This converts the following to Kconfig:
CONFIG_ENV_MIN_ENTRIES
CONFIG_ENV_MAX_ENTRIES

Cc: Michal Simek <michal.simek@amd.com>
Signed-off-by: Tom Rini <trini@konsulko.com>

# 96434a76 05-Nov-2020 Simon Glass <sjg@chromium.org>

env: Allow returning errors from hdelete_r()

At present this function returns 1 on success and 0 on failure. But in
the latter case it provides no indication of what went wrong.

If an attempt is made to delete a non-existent variable, the caller may
want to ignore this error. This happens when setting a non-existent
variable to "", for example.

Update the function to return 0 on success and a useful error code on
failure. Add a function comment too.

Make sure that env_set() does not return an error if it is deleting a
variable that doesn't exist. We could update env_set() to return useful
error numbers also, but that is beyond the scope of this change.

Signed-off-by: Simon Glass <sjg@chromium.org>

wip

# 34ca77c1 20-Aug-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

lib/hashtable: remove superfluous check

We assign first_deleted = 0. There is no need to check its value without
any further assignment in between.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# d045cbac 07-Jul-2020 Marek Vasut <marex@denx.de>

env: Add support for explicit write access list

This option marks any U-Boot variable which does not have explicit 'w'
writeable flag set as read-only. This way the environment can be locked
down and only variables explicitly configured to be writeable can ever
be changed by either 'env import', 'env set' or loading user environment
from environment storage.

Signed-off-by: Marek Vasut <marex@denx.de>
Reviewed-by: Tom Rini <trini@konsulko.com>

# 47f3b1f2 07-Jul-2020 Marek Vasut <marex@denx.de>

env: Add option to only ever append environment

Add configuration option which prevents the environment hash table to be
ever cleared and reloaded with different content. This is useful in case
the first environment loaded into the hash table contains e.g. sensitive
content which must not be dropped or reloaded.

Signed-off-by: Marek Vasut <marex@denx.de>
Reviewed-by: Tom Rini <trini@konsulko.com>

# 60ffcf21 24-Jun-2020 Sean Anderson <seanga2@gmail.com>

lib: Always set errno in hcreate_r

This could give a confusing error message if it failed and didn't set
errno.

Signed-off-by: Sean Anderson <seanga2@gmail.com>
Reviewed-by: Bin Meng <bmeng.cn@gmail.com>

# f7ae49fc 10-May-2020 Simon Glass <sjg@chromium.org>

common: Drop log.h from common header

Move this header out of the common header.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 09140113 10-May-2020 Simon Glass <sjg@chromium.org>

command: Remove the cmd_tbl_t typedef

We should not use typedefs in U-Boot. They cannot be used as forward
declarations which means that header files must include the full header to
access them.

Drop the typedef and rename the struct to remove the _s suffix which is
now not useful.

This requires quite a few header-file additions.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 080019b8 27-Feb-2020 Rasmus Villemoes <rasmus.villemoes@prevas.dk>

make env_entry::callback conditional on !CONFIG_SPL_BUILD

The callback member of struct env_entry is always NULL for an SPL
build. Removing it thus saves a bit of run-time memory in the
SPL (when CONFIG_SPL_ENV_SUPPORT=y) since struct env_entry is embedded
in struct env_entry_node - i.e. about 2KB for the normal case of
512+change hash table entries.

Two small fixups are needed for this, all other references to the
callback member are already under !CONFIG_SPL_BUILD: Don't initialize
.callback in set_flags() - hsearch_r doesn't use that value
anyway. And make env_callback_init() initialize ->callback to NULL for
a new entry instead of relying on an unused or deleted entry having
NULL in ->callback.

Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 34284970 27-Feb-2020 Rasmus Villemoes <rasmus.villemoes@prevas.dk>

lib/hashtable.c: don't test ->callback in SPL

In SPL, environment callbacks are not supported, so e->callback is
always NULL. Removing this makes the SPL a little smaller (about 400
bytes in my ppc build) with no functional change.

Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 7f529f65 27-Feb-2020 Rasmus Villemoes <rasmus.villemoes@prevas.dk>

lib/hashtable.c: create helper for calling env_entry::callback

This is preparation for compiling out the "call the callback" code and
associated error handling for SPL, where ->callback is always NULL.

Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 8bef79bf 14-Nov-2019 Simon Glass <sjg@chromium.org>

common: Move sorting functions to their own header file

These don't need to be in common.h so move them out into a new header.
Also add some missing comments.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@konsulko.com>

# 3f0d6807 01-Aug-2019 Simon Glass <sjg@chromium.org>

env: Drop the ACTION typedef

Avoid using a typedef here which is unnecessary. Add an 'env_' prefix to
both the enum and its members to make it clear that these are related to
the environment.

Add an ENV prefix to these two flags so that it is clear what they relate
to. Also move them to env.h since they are part of the public API. Use an
enum rather than a #define to tie them together.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 25e51e90 02-Aug-2019 Simon Glass <sjg@chromium.org>

env: Drop _ENTRY

This typedef does not need to be defined in the search.h header since it
is only used in one file (hashtable.c). Remove it from the header and
change it to a struct.

Signed-off-by: Simon Glass <sjg@chromium.org>

# dd2408ca 02-Aug-2019 Simon Glass <sjg@chromium.org>

env: Drop the ENTRY typedef

U-Boot is not supposed to use typedef for structs anymore. Also this name
is the same as the ENTRY() macro used in assembler files, and 'entry'
itself is widely used in U-Boot (>8k matches).

Drop the typedef and rename the struct to env_entry to reduce confusion.

Signed-off-by: Simon Glass <sjg@chromium.org>
Acked-by: Joe Hershberger <joe.hershberger@ni.com>

# 9dfdbd9f 30-Jan-2019 Roman Kapl <rka@sysgo.com>

hashtable: fix environment variable corruption

Only first previously deleted entry was recognized, leading hsearch_r
to think that there was no previously deleted entry. It then conluded
that a free entry was found, even if there were no free entries and it
overwrote a random entry.

This patch makes sure all deleted or free entries are always found and
also introduces constants for the 0 and -1 numbers. Unit tests to excersise a
simple hash table usage and catch the corruption were added.

To trash your environment, simply run this loop:

setenv i 0
while true; do
setenv v_$i $i
setenv v_$i
setexpr i $i + 1
done

Signed-off-by: Roman Kapl <rka@sysgo.com>

# 320194ae 23-Jan-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

hashtable: remove caps buffer

slre_match() checks if caps == NULL. In this case it does not try to
update it. So there is no need to create a buffer caps which we do not
evaluate.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 4bca3249 14-Dec-2018 AKASHI Takahiro <takahiro.akashi@linaro.org>

hashtable: fix length calculation in hexport_r

The commit below incorrectly fixed hexport_r();
> size = totlen + 1;
One extra byte is necessary to NULL-terminate a whole buffer, "resp."

Fixes: f1b20acb4a03 ("hashtable: Fix length calculation in hexport_r")
Signed-off-by: AKASHI Takahiro <takahiro.akashi@linaro.org>

# f1b20acb 17-Jul-2018 Zubair Lutfullah Kakakhel <zubair@resin.io>

hashtable: Fix length calculation in hexport_r

The length returned by hexport_r has a few redundant characters.
This appears as NULL characters at the end so seems harmless.

Remove the surplus counts in two places

totlen += strlen(ep->key) + 2;
I'm guessing the +2 here is for = and sep char. But there is another
totlen += 2; line that does that.

size = totletn + 1;
Doesn't make sense and isn't justified with any comment.

Signed-off-by: Zubair Lutfullah Kakakhel <zubair@resin.io>

# d9fc9077 09-Jul-2018 Quentin Schulz <quentin.schulz@bootlin.com>

hashtable: do not recreate whole hash table if vars are passed to himport_r

When vars are passed to the himport_r function with H_NOCLEAR flag,
those vars will be overridden in the current environment and if one of
those vars is not in the imported environment, it'll be deleted in the
current environment whatever the flag passed to himport_r.

The H_NOCLEAR flag is used to clear the whole environment whether vars
are passed to the function or not.

This leads to incoherent behaviour. If one passes vars to himport_r
with the H_NOCLEAR flag, if a var in vars is not in the imported env,
that var will be removed from the current env.

If one passes vars to himport_r without the H_NOCLEAR flag, the whole
environment will be removed and vars will be imported from the
environment in RAM.

It makes more sense to keep the variable that is in the current
environment but not in the imported environment if the H_NOCLEAR flag is
set and remove only that variable if the H_NOCLEAR flag is not set.

Let's clear the whole environment only if H_NOCLEAR and vars are not
passed to himport_r.

Let's remove variables that are in the current environment but not in
the imported env only if the H_NOCLEAR flag is not passed.

Suggested-by: Wolfgang Denk <wd@denx.de>
Signed-off-by: Quentin Schulz <quentin.schulz@bootlin.com>

# 83d290c5 06-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert all of our single license tags to Linux Kernel style

When U-Boot started using SPDX tags we were among the early adopters and
there weren't a lot of other examples to borrow from. So we picked the
area of the file that usually had a full license text and replaced it
with an appropriate SPDX-License-Identifier: entry. Since then, the
Linux Kernel has adopted SPDX tags and they place it as the very first
line in a file (except where shebangs are used, then it's second line)
and with slightly different comment styles than us.

In part due to community overlap, in part due to better tag visibility
and in part for other minor reasons, switch over to that style.

This commit changes all instances where we have a single declared
license in the tag as both the before and after are identical in tag
contents. There's also a few places where I found we did not have a tag
and have introduced one.

Signed-off-by: Tom Rini <trini@konsulko.com>

# d2d9bdfc 28-Sep-2016 B, Ravi <ravibabu@ti.com>

spl: saveenv: adding saveenv support in SPL

By default saveenv option is not supported for SPL. This patch
enable the support for save environment variable for SPL build.

Enable save environment support in SPL after setenv. By default
the saveenv option is not provided in SPL, but some boards need
this support in 'Falcon' boot, where SPL need to boot from
different images based on environment variable set by OS. For
example OS may set "reboot_image" environment variable to
"recovery" inorder to boot recovery image by SPL. The SPL read
"reboot_image" and act accordingly and change the reboot_image
to default mode using setenv and save the environemnt.

Signed-off-by: Ravi Babu <ravibabu@ti.com>
Reviewed-by: Simon Glass <sig@chromium.org>

change in v1:
- dropped SUPPORT, use CONFIG_SPL_SAVEENV
- updates the comments in mmc_private.h

# fc0b5948 07-Sep-2016 Robert P. J. Day <rpjday@crashcourse.ca>

Various, accumulated typos collected from around the tree.

Fix various misspellings of:

* deprecated
* partition
* preceding,preceded
* preparation
* its versus it's
* export
* existing
* scenario
* redundant
* remaining
* value
* architecture

Signed-off-by: Robert P. J. Day <rpjday@crashcourse.ca>
Reviewed-by: Jagan Teki <jteki@openedev.com>
Reviewed-by: Stefan Roese <sr@denx.de>

# c55d02b2 22-Jul-2016 Simon Glass <sjg@chromium.org>

hashtable: Fix compiler warning on 32-bit sandbox

This fixes a mismatch between the %zu format and the type used on sandbox.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@konsulko.com>

# 62a3b7dd 15-Jul-2016 Robert P. J. Day <rpjday@crashcourse.ca>

Various, unrelated tree-wide typo fixes.

Fix a number of typos, including:

* "compatble" -> "compatible"
* "eanbeld" -> "enabled"
* "envrionment" -> "environment"
* "FTD" -> "FDT" (for "flattened device tree")
* "ommitted" -> "omitted"
* "overriden" -> "overridden"
* "partiton" -> "partition"
* "propogate" -> "propagate"
* "resourse" -> "resource"
* "rest in piece" -> "rest in peace"
* "suport" -> "support"
* "varible" -> "variable"

Signed-off-by: Robert P. J. Day <rpjday@crashcourse.ca>

# 0226d878 13-Sep-2015 Lukasz Majewski <l.majewski@majess.pl>

env: import: hashtable: Free memory allocated before exiting from himport_r()

ithout this patch memory is not released on early exit.

Signed-off-by: Lukasz Majewski <l.majewski@majess.pl>

# 817e48d8 13-Sep-2015 Lukasz Majewski <l.majewski@majess.pl>

env: import: hashtable: Prevent buffer overrun when importing environment from file

Lets consider following scenario:
- One uses echo -n "key=value" to define environment variable in a file (single variable)
- The file content is "key=value" without any terminating byte (e.g. 0x0a or
0x0d).
- The file is loaded to u-boot non zero'ed RAM buffer (with load command).
- Then "env import -t -r $loadaddr $filesize" is executed.
- Due to lack of proper termination byte we have classical example of buffer
overrun.

This patch prevents from this by allocating one extra byte than size and
explicitly null terminate it.

There should be no change for normal env import operation after applying
this patch.

Signed-off-by: Lukasz Majewski <l.majewski@majess.pl>

# ecd1446f 14-Jul-2014 Alexander Holler <holler@ahsoftware.de>

Add option -r to env import to allow import of text files with CRLF as line endings

When this option is enabled, CRLF is treated like LF when importing environments
from text files, which means CRs ('\r') in front of LFs ('\n') are just ignored.

Drawback of enabling this option is that (maybe exported) variables which have
a trailing CR in their content will get imported without that CR. But this
drawback is very unlikely and the big advantage of letting Windows user create
a *working* uEnv.txt too is likely more welcome.

Signed-off-by: Alexander Holler <holler@ahsoftware.de>

# 9a832331 08-Oct-2013 Pierre Aubert <p.aubert@staubli.com>

env: fix the env export varname

The env export command doesn't export the first variable of the list
since commit 5a31ea04c9ee5544fbb70ad7597ea4b294840eab
"env grep" - reimplement command using hexport_r()

Signed-off-by: Pierre Aubert <p.aubert@staubli.com>

# 1bce2aeb 16-Sep-2013 Robert P. J. Day <rpjday@crashcourse.ca>

Cosmetic: Fix a number of typos, no functional changes.

Fix various misspellings of things like "environment", "kernel",
"default" and "volatile", and throw in a couple grammar fixes.

Signed-off-by: Robert P. J. Day <rpjday@crashcourse.ca>

# eee479cf 08-Jul-2013 Wolfgang Denk <wd@denx.de>

Add LGPL-2.1+ SPDX-License-Identifier to source files

Signed-off-by: Wolfgang Denk <wd@denx.de>

# e4fdcadd 28-Apr-2013 Lucian Cojocar <cojocar@gmail.com>

env: throw an error when an empty key is used

If the environment contains an entry like "=value" "\0" we should throw
an error when parsing the environment. Otherwise, U-Boot will enter in
an infinite loop.

Signed-off-by: Lucian Cojocar <cojocar@gmail.com>

# be29df6a 23-Mar-2013 Wolfgang Denk <wd@denx.de>

"env grep" - add support for regular expression matches

When CONFIG_REGEX is enabled, the new option "-e" becomes available
which causes regular expression matches to be used. This allows for
example things like these:

- print all MAC addresses:

=> env grep -e eth.*addr
eth1addr=00:10:ec:80:c5:15
ethaddr=00:10:ec:00:c5:15

- print all variables that have at least 2 colons in their value:

=> env grep -v -e :.*:
addip=setenv bootargs ${bootargs} ip=${ipaddr}:${serverip}:${gatewayip}:${netmask}:${hostname}:${netdev}:off
panic=1
eth1addr=00:10:ec:80:c5:15
ethaddr=00:10:ec:00:c5:15
ver=U-Boot 2013.04-rc1-00289-g497746b-dirty (Mar 22 2013 - 12:50:25)

etc.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# 5a31ea04 23-Mar-2013 Wolfgang Denk <wd@denx.de>

"env grep" - reimplement command using hexport_r()

Also drop hstrstr_r() which is not needed any more.
The new code is way more flexible.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# ea009d47 23-Mar-2013 Wolfgang Denk <wd@denx.de>

hashtable: preparations to use hexport_r() for "env grep"

The output of "env grep" is unsorted, and printing is done by a
private implementation to parse the hash table. We have all the
needed code in place in hexport_r() alsready, so let's use this
instead. Here we prepare the code for this, without any functional
changes yet.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# 2598090b 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Add environment variable flags

Currently just validates variable types as decimal, hexidecimal,
boolean, ip address, and mac address.

If the entry is not found in the env ".flags", then look in the static
one. This allows the env to override the static definitions, but prevents
the need to have every definition in the environment distracting you.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# 170ab110 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Add support for callbacks to environment vars

Add support for per-variable callbacks to the "hashtable" functions.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

!!!fix comment in callback

# be11235a 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Hide '.' variables in env print by default

When printing all variables with env print, don't print variables that
begin with '.'. If env print is called with a '-a' switch, then
include variables that begin with '.' (just like the ls command).

Variables printed explicitly will be printed even without the -a.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# 7afcf3a5 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Refactor apply into change_ok

Move the read of the old value to inside the check function. In some
cases it can be avoided all together and at the least the code is only
called from one place.

Also name the function and the callback to more clearly describe what
it does.

Pass the ENTRY instead of just the name for direct access to the whole
data structure.

Pass an enum to the callback that specifies the operation being approved.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# 3d3b52f2 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Consolidate common code in hsearch_r()

The same chunk of code was replicated in two places and the following
changes will make that chunk grow a bit, so combine into a static func.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# c4e0057f 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Refactor do_apply to a flag

Use a flag in hsearch_r for insert mode passed from import to allow the
behavior be different based on use.

Now that "do_check" is called for all imports, ensure console init is
complete before updating the console on relocation import

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# 7ac2fe2d 17-Sep-2012 Ilya Yanok <ilya.yanok@cogentembedded.com>

OMAP: networking support for SPL

This patch adds support for networking in SPL. Some devices are
capable of loading SPL via network so it makes sense to load the
main U-Boot binary via network too. This patch tries to use
existing network code as much as possible. Unfortunately, it depends
on environment which in turn depends on other code so SPL size
is increased significantly. No effort was done to decouple network
code and environment so far.

Signed-off-by: Ilya Yanok <ilya.yanok@cogentembedded.com>
Acked-by: Joe Hershberger <joe.hershberger@ni.com>
Signed-off-by: Tom Rini <trini@ti.com>

# d5370feb 26-Aug-2012 Gerlando Falauto <gerlando.falauto@keymile.com>

env: delete selected vars not present in imported env

When variables explicitly specified on the command line are not present
in the imported env, delete them from the running env.
If the variable is also missing from the running env, issue a warning.

Signed-off-by: Gerlando Falauto <gerlando.falauto@keymile.com>
Reviewed-by: Marek Vasut <marex@denx.de>

# 152874b6 23-Aug-2012 Gerlando Falauto <gerlando.falauto@keymile.com>

env: check and apply changes on delete/destroy

Signed-off-by: Gerlando Falauto <gerlando.falauto@keymile.com>
Reviewed-by: Marek Vasut <marex@denx.de>

# c5983592 23-Aug-2012 Gerlando Falauto <gerlando.falauto@keymile.com>

env: add check/apply logic to himport_r()

Change hashtable so that a callback function will decide whether a
variable can be overwritten, and possibly apply the changes.

So add a new field to struct hsearch_data:

o "apply" callback function to check whether a variable can be
overwritten, and possibly immediately apply the changes;
when NULL, no check is performed.

And a new argument to himport_r():
o "do_apply": whether to call the apply callback function

NOTE: This patch does not change the current behavior.

Signed-off-by: Gerlando Falauto <gerlando.falauto@keymile.com>
Reviewed-by: Marek Vasut <marex@denx.de>

# 348b1f1c 23-Aug-2012 Gerlando Falauto <gerlando.falauto@keymile.com>

env: make himport_r() selective on variables

Add 2 new arguments to himport_r():

o "nvars", "vars": number and list of variables to take into account
(0 means ALL)

NOTE: This patch does not change the current behaviour.

Signed-off-by: Gerlando Falauto <gerlando.falauto@keymile.com>
Reviewed-by: Marek Vasut <marex@denx.de>

# ff856286 21-Nov-2011 Simon Glass <sjg@chromium.org>

sandbox: Fix warnings in hashtable.c

This fixes a few printf() strings for size_t which are missing the 'z'
modifier.

Signed-off-by: Simon Glass <sjg@chromium.org>
Acked-by: Mike Frysinger <vapier@gentoo.org>

# 37f2fe74 06-Nov-2011 Wolfgang Denk <wd@denx.de>

env: allow to export only selected variables

New syntax:
env export [-t | -b | -c] [-s size] addr [var ...]

With this change it is possible to provide a list of variables names
that shall be exported. Whenno arguments are given, the whole
environment gets exported.

NOTE: The new handling of the "size" argument means a change to the
user API.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# 4d91a6ec 23-Aug-2011 Jason Hobbs <jason.hobbs@calxeda.com>

Replace space and tab checks with isblank

These are various places I found that checked for conditions equivalent
to isblank.

Signed-off-by: Jason Hobbs <jason.hobbs@calxeda.com>

# 84b5e802 29-Jul-2011 Wolfgang Denk <wd@denx.de>

Constify getenv(), setenv() and hash code functions

This is needed to get rid of build warnings like

main.c:311: warning: passing argument 2 of 'setenv' discards qualifiers from pointer target type

which result from commit 09c2e90 "unify version_string".

Signed-off-by: Wolfgang Denk <wd@denx.de>
Cc: Andreas Bie�mann <andreas.devel@googlemail.com>

# a000b795 05-Apr-2011 Kim Phillips <kim.phillips@freescale.com>

common: add a grepenv command

u-boot environments, esp. when boards are shared across multiple
users, can get pretty large and time consuming to visually parse.
The grepenv command this patch adds can be used in lieu of printenv
to facilitate searching. grepenv works like printenv but limits
its output only to environment strings (variable name and value
pairs) that match the user specified substring.

the following examples are on a board with a 5313 byte environment
that spans multiple screen pages:

Example 1: summarize ethernet configuration:

=> grepenv eth TSEC
etact=FM1@DTSEC2
eth=FM1@DTSEC4
ethact=FM1@DTSEC2
eth1addr=00:E0:0C:00:8b:01
eth2addr=00:E0:0C:00:8b:02
eth3addr=00:E0:0C:00:8b:03
eth4addr=00:E0:0C:00:8b:04
eth5addr=00:E0:0C:00:8b:05
eth6addr=00:E0:0C:00:8b:06
eth7addr=00:E0:0C:00:8b:07
eth8addr=00:E0:0C:00:8b:08
eth9addr=00:E0:0C:00:8b:09
ethaddr=00:E0:0C:00:8b:00
netdev=eth0
uprcw=setenv ethact $eth;setenv filename p4080ds/R_PPSXX_0xe/rcw_0xe_2sgmii_rev2_high.bin;setenv start 0xe8000000;protect off all;run upimage;protect on all
upuboot=setenv ethact $eth;setenv filename u-boot.bin;setenv start eff80000;protect off all;run upimage;protect on all
upucode=setenv ethact $eth;setenv filename fsl_fman_ucode_P4080_101_6.bin;setenv start 0xef000000;protect off all;run upimage;protect on all
usdboot=setenv ethact $eth;tftp 1000000 $dir/$bootfile;tftp 2000000 $dir/initramfs.cpio.gz.uboot;tftp c00000 $dir/p4080ds-usdpaa.dtb;setenv bootargs root=/dev/ram rw console=ttyS0,115200 $othbootargs;bootm 1000000 2000000 c00000;
=>

Example 2: detect unused env vars:

=> grepenv etact
etact=FM1@DTSEC2
=>

Example 3: reveal hardcoded variables; e.g., for fdtaddr:

=> grepenv fdtaddr
fdtaddr=c00000
nfsboot=setenv bootargs root=/dev/nfs rw nfsroot=$serverip:$rootpath ip=$ipaddr:$serverip:$gatewayip:$netmask:$hostname:$netdev:off console=$consoledev,$baudrate $othbootargs;tftp $loadaddr $bootfile;tftp $fdtaddr $fdtfile;bootm $loadaddr - $fdtaddr
ramboot=setenv bootargs root=/dev/ram rw console=$consoledev,$baudrate $othbootargs;tftp $ramdiskaddr $ramdiskfile;tftp $loadaddr $bootfile;tftp $fdtaddr $fdtfile;bootm $loadaddr $ramdiskaddr $fdtaddr
=> grep $fdtaddr
fdtaddr=c00000
my_boot=bootm 0x40000000 0x41000000 0x00c00000
my_dtb=tftp 0x00c00000 $prefix/p4080ds.dtb
nohvboot=tftp 1000000 $dir/$bootfile;tftp 2000000 $dir/$ramdiskfile;tftp c00000 $dir/$fdtfile;setenv bootargs root=/dev/ram rw ramdisk_size=0x10000000 console=ttyS0,115200;bootm 1000000 2000000 c00000;
=>

This patch also enables the grepenv command by default on
corenet_ds based boards (and repositions the DHCP command
entry to keep the list sorted).

Signed-off-by: Kim Phillips <kim.phillips@freescale.com>
Cc: Kumar Gala <kumar.gala@freescale.com>
Cc: Andy Fleming <afleming@freescale.com>

# af4d9074 04-Apr-2011 Kim Phillips <kim.phillips@freescale.com>

env: fix env var autocompletion

commit 560d424b6d7cd4205b062ad95f1b104bd4f8bcc3 "env: re-add
support for auto-completion" fell short of its description -
the 'used' logic in hmatch_r was reversed - 'used' is 0 if
the hash table entry is not used, or -1 if deleted. This
patch makes hmatch_r actually match on valid ('used') entries,
instead of skipping them and failing to match anything.

typing 'printenv tft' and hitting 'tab' now displays valid
choices for variable names.

Signed-off-by: Kim Phillips <kim.phillips@freescale.com>
Cc: Mike Frysinger <vapier@gentoo.org>
Tested-by: Mike Frysinger <vapier@gentoo.org>

# c81c1222 21-Mar-2011 Peter Barada <peter.barada@logicpd.com>

Fix hash table deletion to prevent lost entries

Use negative used value to mark deleted entry. Search keeps probing
past deleted entries. Adding an entry uses first deleted entry when
it hits end of probe chain.

Initially found that "ramdiskimage" and "preboot" collide modulus 347,
causing "preboot" to be inserted at idx 190, "ramdiskimage" at idx 191.
Previous to this fix when "preboot" is deleted, "ramdiskimage" is
orphaned.

Signed-off-by: Peter Barada <peter.barada@logicpd.com>
Tested-by: Wolfgang Denk <wd@denx.de>

# 560d424b 17-Dec-2010 Mike Frysinger <vapier@gentoo.org>

env: re-add support for auto-completion

Currently, only basic completion is supported (no globs), but this is
what we had previously.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 2eb1573f 08-Dec-2010 Mike Frysinger <vapier@gentoo.org>

hashtable: drop all non-reentrant versions

The non-reentrant versions of the hashtable functions operate on a single
shared hashtable. So if two different people try using these funcs for
two different purposes, they'll cause problems for the other.

Avoid this by converting all existing hashtable consumers over to the
reentrant versions and then punting the non-reentrant ones.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 071bc923 27-Oct-2010 Wolfgang Denk <wd@denx.de>

Coding Style cleanup

Signed-off-by: Wolfgang Denk <wd@denx.de>

# fc5fc76b 01-Oct-2010 Andreas Bießmann <andreas.devel@googlemail.com>

lib/hashtable.c: add CONFIG_ENV_MIN_ENTRIES

This patch adds a new config parameter for adjusting the calculation of
hash table size when importing a buffer.

When importing a extremely small buffer (e.g. the default_environment)
the old calculation generated a hash table which could hold at most the
buffer content but no more entires.

The new calculation add a fixed number of entries to the result to fit
better for small import buffers. This amount may be configured by the
user in board file to adjust the behaviour.

Signed-off-by: Andreas Bie�mann <andreas.devel@googlemail.com>

# ea882baf 20-Jun-2010 Wolfgang Denk <wd@denx.de>

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>

# a6826fbc 20-Jun-2010 Wolfgang Denk <wd@denx.de>

Add hash table support as base for new environment code

This implementation is based on code from uClibc-0.9.30.3 but was
modified and extended for use within U-Boot.

Major modifications and extensions:

* hsearch() [modified / extended]:
- While the standard version does not make any assumptions about
the type of the stored data objects at all, this implementation
works with NUL terminated strings only.
- Instead of storing just pointers to the original objects, we
create local copies so the caller does not need to care about the
data any more.
- The standard implementation does not provide a way to update an
existing entry. This version will create a new entry or update an
existing one when both "action == ENTER" and "item.data != NULL".
- hsearch_r(): Instead of returning 1 on success, we return the
index into the internal hash table, which is also guaranteed to be
positive. This allows us direct access to the found hash table
slot for example for functions like hdelete().
* hdelete() [added]:
- The standard implementation of hsearch(3) does not provide any way
to delete any entries from the hash table. We extend the code to
do that.
* hexport() [added]:
- Export the data stored in the hash table in linearized form:
Entries are exported as "name=value" strings, separated by an
arbitrary (non-NUL, of course) separator character. This allows to
use this function both when formatting the U-Boot environment for
external storage (using '\0' as separator), but also when using it
for the "printenv" command to print all variables, simply by using
as '\n" as separator. This can also be used for new features like
exporting the environment data as text file, including the option
for later re-import.
- The entries in the result list will be sorted by ascending key
values.
* himport() [added]:
- Import linearized data into hash table. This is the inverse
function to hexport(): it takes a linear list of "name=value"
pairs and creates hash table entries from it.
- Entries without "value", i. e. consisting of only "name" or
"name=", will cause this entry to be deleted from the hash table.
- The "flag" argument can be used to control the behaviour: when
the H_NOCLEAR bit is set, then an existing hash table will kept,
i. e. new data will be added to an existing hash table;
otherwise, old data will be discarded and a new hash table will
be created.
- The separator character for the "name=value" pairs can be
selected, so we both support importing from externally stored
environment data (separated by NUL characters) and from plain text
files (entries separated by newline characters).
- To allow for nicely formatted text input, leading white space
(sequences of SPACE and TAB chars) is ignored, and entries
starting (after removal of any leading white space) with a '#'
character are considered comments and ignored.
- NOTE: this means that a variable name cannot start with a '#'
character.
- When using a non-NUL separator character, backslash is used as
escape character in the value part, allowing for example fo
multi-line values.
- In theory, arbitrary separator characters can be used, but only
'\0' and '\n' have really been tested.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# 28de1e06 05-Feb-2023 Simon Glass <sjg@chromium.org>

Correct SPL use of ENV_WRITEABLE_LIST

This converts 1 usage of this option to the non-SPL form, since there is
no SPL_ENV_WRITEABLE_LIST defined in Kconfig

Signed-off-by: Simon Glass <sjg@chromium.org>

# 82e0b51c 12-Jun-2022 Tom Rini <trini@konsulko.com>

Convert CONFIG_ENV_MIN_ENTRIES et al to Kconfig

This converts the following to Kconfig:
CONFIG_ENV_MIN_ENTRIES
CONFIG_ENV_MAX_ENTRIES

Cc: Michal Simek <michal.simek@amd.com>
Signed-off-by: Tom Rini <trini@konsulko.com>

# 96434a76 05-Nov-2020 Simon Glass <sjg@chromium.org>

env: Allow returning errors from hdelete_r()

At present this function returns 1 on success and 0 on failure. But in
the latter case it provides no indication of what went wrong.

If an attempt is made to delete a non-existent variable, the caller may
want to ignore this error. This happens when setting a non-existent
variable to "", for example.

Update the function to return 0 on success and a useful error code on
failure. Add a function comment too.

Make sure that env_set() does not return an error if it is deleting a
variable that doesn't exist. We could update env_set() to return useful
error numbers also, but that is beyond the scope of this change.

Signed-off-by: Simon Glass <sjg@chromium.org>

wip

# 34ca77c1 20-Aug-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

lib/hashtable: remove superfluous check

We assign first_deleted = 0. There is no need to check its value without
any further assignment in between.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# d045cbac 07-Jul-2020 Marek Vasut <marex@denx.de>

env: Add support for explicit write access list

This option marks any U-Boot variable which does not have explicit 'w'
writeable flag set as read-only. This way the environment can be locked
down and only variables explicitly configured to be writeable can ever
be changed by either 'env import', 'env set' or loading user environment
from environment storage.

Signed-off-by: Marek Vasut <marex@denx.de>
Reviewed-by: Tom Rini <trini@konsulko.com>

# 47f3b1f2 07-Jul-2020 Marek Vasut <marex@denx.de>

env: Add option to only ever append environment

Add configuration option which prevents the environment hash table to be
ever cleared and reloaded with different content. This is useful in case
the first environment loaded into the hash table contains e.g. sensitive
content which must not be dropped or reloaded.

Signed-off-by: Marek Vasut <marex@denx.de>
Reviewed-by: Tom Rini <trini@konsulko.com>

# 60ffcf21 24-Jun-2020 Sean Anderson <seanga2@gmail.com>

lib: Always set errno in hcreate_r

This could give a confusing error message if it failed and didn't set
errno.

Signed-off-by: Sean Anderson <seanga2@gmail.com>
Reviewed-by: Bin Meng <bmeng.cn@gmail.com>

# f7ae49fc 10-May-2020 Simon Glass <sjg@chromium.org>

common: Drop log.h from common header

Move this header out of the common header.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 09140113 10-May-2020 Simon Glass <sjg@chromium.org>

command: Remove the cmd_tbl_t typedef

We should not use typedefs in U-Boot. They cannot be used as forward
declarations which means that header files must include the full header to
access them.

Drop the typedef and rename the struct to remove the _s suffix which is
now not useful.

This requires quite a few header-file additions.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 080019b8 27-Feb-2020 Rasmus Villemoes <rasmus.villemoes@prevas.dk>

make env_entry::callback conditional on !CONFIG_SPL_BUILD

The callback member of struct env_entry is always NULL for an SPL
build. Removing it thus saves a bit of run-time memory in the
SPL (when CONFIG_SPL_ENV_SUPPORT=y) since struct env_entry is embedded
in struct env_entry_node - i.e. about 2KB for the normal case of
512+change hash table entries.

Two small fixups are needed for this, all other references to the
callback member are already under !CONFIG_SPL_BUILD: Don't initialize
.callback in set_flags() - hsearch_r doesn't use that value
anyway. And make env_callback_init() initialize ->callback to NULL for
a new entry instead of relying on an unused or deleted entry having
NULL in ->callback.

Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 34284970 27-Feb-2020 Rasmus Villemoes <rasmus.villemoes@prevas.dk>

lib/hashtable.c: don't test ->callback in SPL

In SPL, environment callbacks are not supported, so e->callback is
always NULL. Removing this makes the SPL a little smaller (about 400
bytes in my ppc build) with no functional change.

Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 7f529f65 27-Feb-2020 Rasmus Villemoes <rasmus.villemoes@prevas.dk>

lib/hashtable.c: create helper for calling env_entry::callback

This is preparation for compiling out the "call the callback" code and
associated error handling for SPL, where ->callback is always NULL.

Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 8bef79bf 14-Nov-2019 Simon Glass <sjg@chromium.org>

common: Move sorting functions to their own header file

These don't need to be in common.h so move them out into a new header.
Also add some missing comments.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@konsulko.com>

# 3f0d6807 01-Aug-2019 Simon Glass <sjg@chromium.org>

env: Drop the ACTION typedef

Avoid using a typedef here which is unnecessary. Add an 'env_' prefix to
both the enum and its members to make it clear that these are related to
the environment.

Add an ENV prefix to these two flags so that it is clear what they relate
to. Also move them to env.h since they are part of the public API. Use an
enum rather than a #define to tie them together.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 25e51e90 02-Aug-2019 Simon Glass <sjg@chromium.org>

env: Drop _ENTRY

This typedef does not need to be defined in the search.h header since it
is only used in one file (hashtable.c). Remove it from the header and
change it to a struct.

Signed-off-by: Simon Glass <sjg@chromium.org>

# dd2408ca 02-Aug-2019 Simon Glass <sjg@chromium.org>

env: Drop the ENTRY typedef

U-Boot is not supposed to use typedef for structs anymore. Also this name
is the same as the ENTRY() macro used in assembler files, and 'entry'
itself is widely used in U-Boot (>8k matches).

Drop the typedef and rename the struct to env_entry to reduce confusion.

Signed-off-by: Simon Glass <sjg@chromium.org>
Acked-by: Joe Hershberger <joe.hershberger@ni.com>

# 9dfdbd9f 30-Jan-2019 Roman Kapl <rka@sysgo.com>

hashtable: fix environment variable corruption

Only first previously deleted entry was recognized, leading hsearch_r
to think that there was no previously deleted entry. It then conluded
that a free entry was found, even if there were no free entries and it
overwrote a random entry.

This patch makes sure all deleted or free entries are always found and
also introduces constants for the 0 and -1 numbers. Unit tests to excersise a
simple hash table usage and catch the corruption were added.

To trash your environment, simply run this loop:

setenv i 0
while true; do
setenv v_$i $i
setenv v_$i
setexpr i $i + 1
done

Signed-off-by: Roman Kapl <rka@sysgo.com>

# 320194ae 23-Jan-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

hashtable: remove caps buffer

slre_match() checks if caps == NULL. In this case it does not try to
update it. So there is no need to create a buffer caps which we do not
evaluate.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 4bca3249 14-Dec-2018 AKASHI Takahiro <takahiro.akashi@linaro.org>

hashtable: fix length calculation in hexport_r

The commit below incorrectly fixed hexport_r();
> size = totlen + 1;
One extra byte is necessary to NULL-terminate a whole buffer, "resp."

Fixes: f1b20acb4a03 ("hashtable: Fix length calculation in hexport_r")
Signed-off-by: AKASHI Takahiro <takahiro.akashi@linaro.org>

# f1b20acb 17-Jul-2018 Zubair Lutfullah Kakakhel <zubair@resin.io>

hashtable: Fix length calculation in hexport_r

The length returned by hexport_r has a few redundant characters.
This appears as NULL characters at the end so seems harmless.

Remove the surplus counts in two places

totlen += strlen(ep->key) + 2;
I'm guessing the +2 here is for = and sep char. But there is another
totlen += 2; line that does that.

size = totletn + 1;
Doesn't make sense and isn't justified with any comment.

Signed-off-by: Zubair Lutfullah Kakakhel <zubair@resin.io>

# d9fc9077 09-Jul-2018 Quentin Schulz <quentin.schulz@bootlin.com>

hashtable: do not recreate whole hash table if vars are passed to himport_r

When vars are passed to the himport_r function with H_NOCLEAR flag,
those vars will be overridden in the current environment and if one of
those vars is not in the imported environment, it'll be deleted in the
current environment whatever the flag passed to himport_r.

The H_NOCLEAR flag is used to clear the whole environment whether vars
are passed to the function or not.

This leads to incoherent behaviour. If one passes vars to himport_r
with the H_NOCLEAR flag, if a var in vars is not in the imported env,
that var will be removed from the current env.

If one passes vars to himport_r without the H_NOCLEAR flag, the whole
environment will be removed and vars will be imported from the
environment in RAM.

It makes more sense to keep the variable that is in the current
environment but not in the imported environment if the H_NOCLEAR flag is
set and remove only that variable if the H_NOCLEAR flag is not set.

Let's clear the whole environment only if H_NOCLEAR and vars are not
passed to himport_r.

Let's remove variables that are in the current environment but not in
the imported env only if the H_NOCLEAR flag is not passed.

Suggested-by: Wolfgang Denk <wd@denx.de>
Signed-off-by: Quentin Schulz <quentin.schulz@bootlin.com>

# 83d290c5 06-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert all of our single license tags to Linux Kernel style

When U-Boot started using SPDX tags we were among the early adopters and
there weren't a lot of other examples to borrow from. So we picked the
area of the file that usually had a full license text and replaced it
with an appropriate SPDX-License-Identifier: entry. Since then, the
Linux Kernel has adopted SPDX tags and they place it as the very first
line in a file (except where shebangs are used, then it's second line)
and with slightly different comment styles than us.

In part due to community overlap, in part due to better tag visibility
and in part for other minor reasons, switch over to that style.

This commit changes all instances where we have a single declared
license in the tag as both the before and after are identical in tag
contents. There's also a few places where I found we did not have a tag
and have introduced one.

Signed-off-by: Tom Rini <trini@konsulko.com>

# d2d9bdfc 28-Sep-2016 B, Ravi <ravibabu@ti.com>

spl: saveenv: adding saveenv support in SPL

By default saveenv option is not supported for SPL. This patch
enable the support for save environment variable for SPL build.

Enable save environment support in SPL after setenv. By default
the saveenv option is not provided in SPL, but some boards need
this support in 'Falcon' boot, where SPL need to boot from
different images based on environment variable set by OS. For
example OS may set "reboot_image" environment variable to
"recovery" inorder to boot recovery image by SPL. The SPL read
"reboot_image" and act accordingly and change the reboot_image
to default mode using setenv and save the environemnt.

Signed-off-by: Ravi Babu <ravibabu@ti.com>
Reviewed-by: Simon Glass <sig@chromium.org>

change in v1:
- dropped SUPPORT, use CONFIG_SPL_SAVEENV
- updates the comments in mmc_private.h

# fc0b5948 07-Sep-2016 Robert P. J. Day <rpjday@crashcourse.ca>

Various, accumulated typos collected from around the tree.

Fix various misspellings of:

* deprecated
* partition
* preceding,preceded
* preparation
* its versus it's
* export
* existing
* scenario
* redundant
* remaining
* value
* architecture

Signed-off-by: Robert P. J. Day <rpjday@crashcourse.ca>
Reviewed-by: Jagan Teki <jteki@openedev.com>
Reviewed-by: Stefan Roese <sr@denx.de>

# c55d02b2 22-Jul-2016 Simon Glass <sjg@chromium.org>

hashtable: Fix compiler warning on 32-bit sandbox

This fixes a mismatch between the %zu format and the type used on sandbox.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@konsulko.com>

# 62a3b7dd 15-Jul-2016 Robert P. J. Day <rpjday@crashcourse.ca>

Various, unrelated tree-wide typo fixes.

Fix a number of typos, including:

* "compatble" -> "compatible"
* "eanbeld" -> "enabled"
* "envrionment" -> "environment"
* "FTD" -> "FDT" (for "flattened device tree")
* "ommitted" -> "omitted"
* "overriden" -> "overridden"
* "partiton" -> "partition"
* "propogate" -> "propagate"
* "resourse" -> "resource"
* "rest in piece" -> "rest in peace"
* "suport" -> "support"
* "varible" -> "variable"

Signed-off-by: Robert P. J. Day <rpjday@crashcourse.ca>

# 0226d878 13-Sep-2015 Lukasz Majewski <l.majewski@majess.pl>

env: import: hashtable: Free memory allocated before exiting from himport_r()

ithout this patch memory is not released on early exit.

Signed-off-by: Lukasz Majewski <l.majewski@majess.pl>

# 817e48d8 13-Sep-2015 Lukasz Majewski <l.majewski@majess.pl>

env: import: hashtable: Prevent buffer overrun when importing environment from file

Lets consider following scenario:
- One uses echo -n "key=value" to define environment variable in a file (single variable)
- The file content is "key=value" without any terminating byte (e.g. 0x0a or
0x0d).
- The file is loaded to u-boot non zero'ed RAM buffer (with load command).
- Then "env import -t -r $loadaddr $filesize" is executed.
- Due to lack of proper termination byte we have classical example of buffer
overrun.

This patch prevents from this by allocating one extra byte than size and
explicitly null terminate it.

There should be no change for normal env import operation after applying
this patch.

Signed-off-by: Lukasz Majewski <l.majewski@majess.pl>

# ecd1446f 14-Jul-2014 Alexander Holler <holler@ahsoftware.de>

Add option -r to env import to allow import of text files with CRLF as line endings

When this option is enabled, CRLF is treated like LF when importing environments
from text files, which means CRs ('\r') in front of LFs ('\n') are just ignored.

Drawback of enabling this option is that (maybe exported) variables which have
a trailing CR in their content will get imported without that CR. But this
drawback is very unlikely and the big advantage of letting Windows user create
a *working* uEnv.txt too is likely more welcome.

Signed-off-by: Alexander Holler <holler@ahsoftware.de>

# 9a832331 08-Oct-2013 Pierre Aubert <p.aubert@staubli.com>

env: fix the env export varname

The env export command doesn't export the first variable of the list
since commit 5a31ea04c9ee5544fbb70ad7597ea4b294840eab
"env grep" - reimplement command using hexport_r()

Signed-off-by: Pierre Aubert <p.aubert@staubli.com>

# 1bce2aeb 16-Sep-2013 Robert P. J. Day <rpjday@crashcourse.ca>

Cosmetic: Fix a number of typos, no functional changes.

Fix various misspellings of things like "environment", "kernel",
"default" and "volatile", and throw in a couple grammar fixes.

Signed-off-by: Robert P. J. Day <rpjday@crashcourse.ca>

# eee479cf 08-Jul-2013 Wolfgang Denk <wd@denx.de>

Add LGPL-2.1+ SPDX-License-Identifier to source files

Signed-off-by: Wolfgang Denk <wd@denx.de>

# e4fdcadd 28-Apr-2013 Lucian Cojocar <cojocar@gmail.com>

env: throw an error when an empty key is used

If the environment contains an entry like "=value" "\0" we should throw
an error when parsing the environment. Otherwise, U-Boot will enter in
an infinite loop.

Signed-off-by: Lucian Cojocar <cojocar@gmail.com>

# be29df6a 23-Mar-2013 Wolfgang Denk <wd@denx.de>

"env grep" - add support for regular expression matches

When CONFIG_REGEX is enabled, the new option "-e" becomes available
which causes regular expression matches to be used. This allows for
example things like these:

- print all MAC addresses:

=> env grep -e eth.*addr
eth1addr=00:10:ec:80:c5:15
ethaddr=00:10:ec:00:c5:15

- print all variables that have at least 2 colons in their value:

=> env grep -v -e :.*:
addip=setenv bootargs ${bootargs} ip=${ipaddr}:${serverip}:${gatewayip}:${netmask}:${hostname}:${netdev}:off
panic=1
eth1addr=00:10:ec:80:c5:15
ethaddr=00:10:ec:00:c5:15
ver=U-Boot 2013.04-rc1-00289-g497746b-dirty (Mar 22 2013 - 12:50:25)

etc.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# 5a31ea04 23-Mar-2013 Wolfgang Denk <wd@denx.de>

"env grep" - reimplement command using hexport_r()

Also drop hstrstr_r() which is not needed any more.
The new code is way more flexible.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# ea009d47 23-Mar-2013 Wolfgang Denk <wd@denx.de>

hashtable: preparations to use hexport_r() for "env grep"

The output of "env grep" is unsorted, and printing is done by a
private implementation to parse the hash table. We have all the
needed code in place in hexport_r() alsready, so let's use this
instead. Here we prepare the code for this, without any functional
changes yet.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# 2598090b 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Add environment variable flags

Currently just validates variable types as decimal, hexidecimal,
boolean, ip address, and mac address.

If the entry is not found in the env ".flags", then look in the static
one. This allows the env to override the static definitions, but prevents
the need to have every definition in the environment distracting you.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# 170ab110 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Add support for callbacks to environment vars

Add support for per-variable callbacks to the "hashtable" functions.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

!!!fix comment in callback

# be11235a 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Hide '.' variables in env print by default

When printing all variables with env print, don't print variables that
begin with '.'. If env print is called with a '-a' switch, then
include variables that begin with '.' (just like the ls command).

Variables printed explicitly will be printed even without the -a.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# 7afcf3a5 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Refactor apply into change_ok

Move the read of the old value to inside the check function. In some
cases it can be avoided all together and at the least the code is only
called from one place.

Also name the function and the callback to more clearly describe what
it does.

Pass the ENTRY instead of just the name for direct access to the whole
data structure.

Pass an enum to the callback that specifies the operation being approved.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# 3d3b52f2 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Consolidate common code in hsearch_r()

The same chunk of code was replicated in two places and the following
changes will make that chunk grow a bit, so combine into a static func.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# c4e0057f 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Refactor do_apply to a flag

Use a flag in hsearch_r for insert mode passed from import to allow the
behavior be different based on use.

Now that "do_check" is called for all imports, ensure console init is
complete before updating the console on relocation import

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# 7ac2fe2d 17-Sep-2012 Ilya Yanok <ilya.yanok@cogentembedded.com>

OMAP: networking support for SPL

This patch adds support for networking in SPL. Some devices are
capable of loading SPL via network so it makes sense to load the
main U-Boot binary via network too. This patch tries to use
existing network code as much as possible. Unfortunately, it depends
on environment which in turn depends on other code so SPL size
is increased significantly. No effort was done to decouple network
code and environment so far.

Signed-off-by: Ilya Yanok <ilya.yanok@cogentembedded.com>
Acked-by: Joe Hershberger <joe.hershberger@ni.com>
Signed-off-by: Tom Rini <trini@ti.com>

# d5370feb 26-Aug-2012 Gerlando Falauto <gerlando.falauto@keymile.com>

env: delete selected vars not present in imported env

When variables explicitly specified on the command line are not present
in the imported env, delete them from the running env.
If the variable is also missing from the running env, issue a warning.

Signed-off-by: Gerlando Falauto <gerlando.falauto@keymile.com>
Reviewed-by: Marek Vasut <marex@denx.de>

# 152874b6 23-Aug-2012 Gerlando Falauto <gerlando.falauto@keymile.com>

env: check and apply changes on delete/destroy

Signed-off-by: Gerlando Falauto <gerlando.falauto@keymile.com>
Reviewed-by: Marek Vasut <marex@denx.de>

# c5983592 23-Aug-2012 Gerlando Falauto <gerlando.falauto@keymile.com>

env: add check/apply logic to himport_r()

Change hashtable so that a callback function will decide whether a
variable can be overwritten, and possibly apply the changes.

So add a new field to struct hsearch_data:

o "apply" callback function to check whether a variable can be
overwritten, and possibly immediately apply the changes;
when NULL, no check is performed.

And a new argument to himport_r():
o "do_apply": whether to call the apply callback function

NOTE: This patch does not change the current behavior.

Signed-off-by: Gerlando Falauto <gerlando.falauto@keymile.com>
Reviewed-by: Marek Vasut <marex@denx.de>

# 348b1f1c 23-Aug-2012 Gerlando Falauto <gerlando.falauto@keymile.com>

env: make himport_r() selective on variables

Add 2 new arguments to himport_r():

o "nvars", "vars": number and list of variables to take into account
(0 means ALL)

NOTE: This patch does not change the current behaviour.

Signed-off-by: Gerlando Falauto <gerlando.falauto@keymile.com>
Reviewed-by: Marek Vasut <marex@denx.de>

# ff856286 21-Nov-2011 Simon Glass <sjg@chromium.org>

sandbox: Fix warnings in hashtable.c

This fixes a few printf() strings for size_t which are missing the 'z'
modifier.

Signed-off-by: Simon Glass <sjg@chromium.org>
Acked-by: Mike Frysinger <vapier@gentoo.org>

# 37f2fe74 06-Nov-2011 Wolfgang Denk <wd@denx.de>

env: allow to export only selected variables

New syntax:
env export [-t | -b | -c] [-s size] addr [var ...]

With this change it is possible to provide a list of variables names
that shall be exported. Whenno arguments are given, the whole
environment gets exported.

NOTE: The new handling of the "size" argument means a change to the
user API.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# 4d91a6ec 23-Aug-2011 Jason Hobbs <jason.hobbs@calxeda.com>

Replace space and tab checks with isblank

These are various places I found that checked for conditions equivalent
to isblank.

Signed-off-by: Jason Hobbs <jason.hobbs@calxeda.com>

# 84b5e802 29-Jul-2011 Wolfgang Denk <wd@denx.de>

Constify getenv(), setenv() and hash code functions

This is needed to get rid of build warnings like

main.c:311: warning: passing argument 2 of 'setenv' discards qualifiers from pointer target type

which result from commit 09c2e90 "unify version_string".

Signed-off-by: Wolfgang Denk <wd@denx.de>
Cc: Andreas Bie�mann <andreas.devel@googlemail.com>

# a000b795 05-Apr-2011 Kim Phillips <kim.phillips@freescale.com>

common: add a grepenv command

u-boot environments, esp. when boards are shared across multiple
users, can get pretty large and time consuming to visually parse.
The grepenv command this patch adds can be used in lieu of printenv
to facilitate searching. grepenv works like printenv but limits
its output only to environment strings (variable name and value
pairs) that match the user specified substring.

the following examples are on a board with a 5313 byte environment
that spans multiple screen pages:

Example 1: summarize ethernet configuration:

=> grepenv eth TSEC
etact=FM1@DTSEC2
eth=FM1@DTSEC4
ethact=FM1@DTSEC2
eth1addr=00:E0:0C:00:8b:01
eth2addr=00:E0:0C:00:8b:02
eth3addr=00:E0:0C:00:8b:03
eth4addr=00:E0:0C:00:8b:04
eth5addr=00:E0:0C:00:8b:05
eth6addr=00:E0:0C:00:8b:06
eth7addr=00:E0:0C:00:8b:07
eth8addr=00:E0:0C:00:8b:08
eth9addr=00:E0:0C:00:8b:09
ethaddr=00:E0:0C:00:8b:00
netdev=eth0
uprcw=setenv ethact $eth;setenv filename p4080ds/R_PPSXX_0xe/rcw_0xe_2sgmii_rev2_high.bin;setenv start 0xe8000000;protect off all;run upimage;protect on all
upuboot=setenv ethact $eth;setenv filename u-boot.bin;setenv start eff80000;protect off all;run upimage;protect on all
upucode=setenv ethact $eth;setenv filename fsl_fman_ucode_P4080_101_6.bin;setenv start 0xef000000;protect off all;run upimage;protect on all
usdboot=setenv ethact $eth;tftp 1000000 $dir/$bootfile;tftp 2000000 $dir/initramfs.cpio.gz.uboot;tftp c00000 $dir/p4080ds-usdpaa.dtb;setenv bootargs root=/dev/ram rw console=ttyS0,115200 $othbootargs;bootm 1000000 2000000 c00000;
=>

Example 2: detect unused env vars:

=> grepenv etact
etact=FM1@DTSEC2
=>

Example 3: reveal hardcoded variables; e.g., for fdtaddr:

=> grepenv fdtaddr
fdtaddr=c00000
nfsboot=setenv bootargs root=/dev/nfs rw nfsroot=$serverip:$rootpath ip=$ipaddr:$serverip:$gatewayip:$netmask:$hostname:$netdev:off console=$consoledev,$baudrate $othbootargs;tftp $loadaddr $bootfile;tftp $fdtaddr $fdtfile;bootm $loadaddr - $fdtaddr
ramboot=setenv bootargs root=/dev/ram rw console=$consoledev,$baudrate $othbootargs;tftp $ramdiskaddr $ramdiskfile;tftp $loadaddr $bootfile;tftp $fdtaddr $fdtfile;bootm $loadaddr $ramdiskaddr $fdtaddr
=> grep $fdtaddr
fdtaddr=c00000
my_boot=bootm 0x40000000 0x41000000 0x00c00000
my_dtb=tftp 0x00c00000 $prefix/p4080ds.dtb
nohvboot=tftp 1000000 $dir/$bootfile;tftp 2000000 $dir/$ramdiskfile;tftp c00000 $dir/$fdtfile;setenv bootargs root=/dev/ram rw ramdisk_size=0x10000000 console=ttyS0,115200;bootm 1000000 2000000 c00000;
=>

This patch also enables the grepenv command by default on
corenet_ds based boards (and repositions the DHCP command
entry to keep the list sorted).

Signed-off-by: Kim Phillips <kim.phillips@freescale.com>
Cc: Kumar Gala <kumar.gala@freescale.com>
Cc: Andy Fleming <afleming@freescale.com>

# af4d9074 04-Apr-2011 Kim Phillips <kim.phillips@freescale.com>

env: fix env var autocompletion

commit 560d424b6d7cd4205b062ad95f1b104bd4f8bcc3 "env: re-add
support for auto-completion" fell short of its description -
the 'used' logic in hmatch_r was reversed - 'used' is 0 if
the hash table entry is not used, or -1 if deleted. This
patch makes hmatch_r actually match on valid ('used') entries,
instead of skipping them and failing to match anything.

typing 'printenv tft' and hitting 'tab' now displays valid
choices for variable names.

Signed-off-by: Kim Phillips <kim.phillips@freescale.com>
Cc: Mike Frysinger <vapier@gentoo.org>
Tested-by: Mike Frysinger <vapier@gentoo.org>

# c81c1222 21-Mar-2011 Peter Barada <peter.barada@logicpd.com>

Fix hash table deletion to prevent lost entries

Use negative used value to mark deleted entry. Search keeps probing
past deleted entries. Adding an entry uses first deleted entry when
it hits end of probe chain.

Initially found that "ramdiskimage" and "preboot" collide modulus 347,
causing "preboot" to be inserted at idx 190, "ramdiskimage" at idx 191.
Previous to this fix when "preboot" is deleted, "ramdiskimage" is
orphaned.

Signed-off-by: Peter Barada <peter.barada@logicpd.com>
Tested-by: Wolfgang Denk <wd@denx.de>

# 560d424b 17-Dec-2010 Mike Frysinger <vapier@gentoo.org>

env: re-add support for auto-completion

Currently, only basic completion is supported (no globs), but this is
what we had previously.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 2eb1573f 08-Dec-2010 Mike Frysinger <vapier@gentoo.org>

hashtable: drop all non-reentrant versions

The non-reentrant versions of the hashtable functions operate on a single
shared hashtable. So if two different people try using these funcs for
two different purposes, they'll cause problems for the other.

Avoid this by converting all existing hashtable consumers over to the
reentrant versions and then punting the non-reentrant ones.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 071bc923 27-Oct-2010 Wolfgang Denk <wd@denx.de>

Coding Style cleanup

Signed-off-by: Wolfgang Denk <wd@denx.de>

# fc5fc76b 01-Oct-2010 Andreas Bießmann <andreas.devel@googlemail.com>

lib/hashtable.c: add CONFIG_ENV_MIN_ENTRIES

This patch adds a new config parameter for adjusting the calculation of
hash table size when importing a buffer.

When importing a extremely small buffer (e.g. the default_environment)
the old calculation generated a hash table which could hold at most the
buffer content but no more entires.

The new calculation add a fixed number of entries to the result to fit
better for small import buffers. This amount may be configured by the
user in board file to adjust the behaviour.

Signed-off-by: Andreas Bie�mann <andreas.devel@googlemail.com>

# ea882baf 20-Jun-2010 Wolfgang Denk <wd@denx.de>

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>

# a6826fbc 20-Jun-2010 Wolfgang Denk <wd@denx.de>

Add hash table support as base for new environment code

This implementation is based on code from uClibc-0.9.30.3 but was
modified and extended for use within U-Boot.

Major modifications and extensions:

* hsearch() [modified / extended]:
- While the standard version does not make any assumptions about
the type of the stored data objects at all, this implementation
works with NUL terminated strings only.
- Instead of storing just pointers to the original objects, we
create local copies so the caller does not need to care about the
data any more.
- The standard implementation does not provide a way to update an
existing entry. This version will create a new entry or update an
existing one when both "action == ENTER" and "item.data != NULL".
- hsearch_r(): Instead of returning 1 on success, we return the
index into the internal hash table, which is also guaranteed to be
positive. This allows us direct access to the found hash table
slot for example for functions like hdelete().
* hdelete() [added]:
- The standard implementation of hsearch(3) does not provide any way
to delete any entries from the hash table. We extend the code to
do that.
* hexport() [added]:
- Export the data stored in the hash table in linearized form:
Entries are exported as "name=value" strings, separated by an
arbitrary (non-NUL, of course) separator character. This allows to
use this function both when formatting the U-Boot environment for
external storage (using '\0' as separator), but also when using it
for the "printenv" command to print all variables, simply by using
as '\n" as separator. This can also be used for new features like
exporting the environment data as text file, including the option
for later re-import.
- The entries in the result list will be sorted by ascending key
values.
* himport() [added]:
- Import linearized data into hash table. This is the inverse
function to hexport(): it takes a linear list of "name=value"
pairs and creates hash table entries from it.
- Entries without "value", i. e. consisting of only "name" or
"name=", will cause this entry to be deleted from the hash table.
- The "flag" argument can be used to control the behaviour: when
the H_NOCLEAR bit is set, then an existing hash table will kept,
i. e. new data will be added to an existing hash table;
otherwise, old data will be discarded and a new hash table will
be created.
- The separator character for the "name=value" pairs can be
selected, so we both support importing from externally stored
environment data (separated by NUL characters) and from plain text
files (entries separated by newline characters).
- To allow for nicely formatted text input, leading white space
(sequences of SPACE and TAB chars) is ignored, and entries
starting (after removal of any leading white space) with a '#'
character are considered comments and ignored.
- NOTE: this means that a variable name cannot start with a '#'
character.
- When using a non-NUL separator character, backslash is used as
escape character in the value part, allowing for example fo
multi-line values.
- In theory, arbitrary separator characters can be used, but only
'\0' and '\n' have really been tested.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# 82e0b51c 12-Jun-2022 Tom Rini <trini@konsulko.com>

Convert CONFIG_ENV_MIN_ENTRIES et al to Kconfig

This converts the following to Kconfig:
CONFIG_ENV_MIN_ENTRIES
CONFIG_ENV_MAX_ENTRIES

Cc: Michal Simek <michal.simek@amd.com>
Signed-off-by: Tom Rini <trini@konsulko.com>

# 96434a76 05-Nov-2020 Simon Glass <sjg@chromium.org>

env: Allow returning errors from hdelete_r()

At present this function returns 1 on success and 0 on failure. But in
the latter case it provides no indication of what went wrong.

If an attempt is made to delete a non-existent variable, the caller may
want to ignore this error. This happens when setting a non-existent
variable to "", for example.

Update the function to return 0 on success and a useful error code on
failure. Add a function comment too.

Make sure that env_set() does not return an error if it is deleting a
variable that doesn't exist. We could update env_set() to return useful
error numbers also, but that is beyond the scope of this change.

Signed-off-by: Simon Glass <sjg@chromium.org>

wip

# 34ca77c1 20-Aug-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

lib/hashtable: remove superfluous check

We assign first_deleted = 0. There is no need to check its value without
any further assignment in between.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# d045cbac 07-Jul-2020 Marek Vasut <marex@denx.de>

env: Add support for explicit write access list

This option marks any U-Boot variable which does not have explicit 'w'
writeable flag set as read-only. This way the environment can be locked
down and only variables explicitly configured to be writeable can ever
be changed by either 'env import', 'env set' or loading user environment
from environment storage.

Signed-off-by: Marek Vasut <marex@denx.de>
Reviewed-by: Tom Rini <trini@konsulko.com>

# 47f3b1f2 07-Jul-2020 Marek Vasut <marex@denx.de>

env: Add option to only ever append environment

Add configuration option which prevents the environment hash table to be
ever cleared and reloaded with different content. This is useful in case
the first environment loaded into the hash table contains e.g. sensitive
content which must not be dropped or reloaded.

Signed-off-by: Marek Vasut <marex@denx.de>
Reviewed-by: Tom Rini <trini@konsulko.com>

# 60ffcf21 24-Jun-2020 Sean Anderson <seanga2@gmail.com>

lib: Always set errno in hcreate_r

This could give a confusing error message if it failed and didn't set
errno.

Signed-off-by: Sean Anderson <seanga2@gmail.com>
Reviewed-by: Bin Meng <bmeng.cn@gmail.com>

# f7ae49fc 10-May-2020 Simon Glass <sjg@chromium.org>

common: Drop log.h from common header

Move this header out of the common header.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 09140113 10-May-2020 Simon Glass <sjg@chromium.org>

command: Remove the cmd_tbl_t typedef

We should not use typedefs in U-Boot. They cannot be used as forward
declarations which means that header files must include the full header to
access them.

Drop the typedef and rename the struct to remove the _s suffix which is
now not useful.

This requires quite a few header-file additions.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 080019b8 27-Feb-2020 Rasmus Villemoes <rasmus.villemoes@prevas.dk>

make env_entry::callback conditional on !CONFIG_SPL_BUILD

The callback member of struct env_entry is always NULL for an SPL
build. Removing it thus saves a bit of run-time memory in the
SPL (when CONFIG_SPL_ENV_SUPPORT=y) since struct env_entry is embedded
in struct env_entry_node - i.e. about 2KB for the normal case of
512+change hash table entries.

Two small fixups are needed for this, all other references to the
callback member are already under !CONFIG_SPL_BUILD: Don't initialize
.callback in set_flags() - hsearch_r doesn't use that value
anyway. And make env_callback_init() initialize ->callback to NULL for
a new entry instead of relying on an unused or deleted entry having
NULL in ->callback.

Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 34284970 27-Feb-2020 Rasmus Villemoes <rasmus.villemoes@prevas.dk>

lib/hashtable.c: don't test ->callback in SPL

In SPL, environment callbacks are not supported, so e->callback is
always NULL. Removing this makes the SPL a little smaller (about 400
bytes in my ppc build) with no functional change.

Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 7f529f65 27-Feb-2020 Rasmus Villemoes <rasmus.villemoes@prevas.dk>

lib/hashtable.c: create helper for calling env_entry::callback

This is preparation for compiling out the "call the callback" code and
associated error handling for SPL, where ->callback is always NULL.

Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 8bef79bf 14-Nov-2019 Simon Glass <sjg@chromium.org>

common: Move sorting functions to their own header file

These don't need to be in common.h so move them out into a new header.
Also add some missing comments.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@konsulko.com>

# 3f0d6807 01-Aug-2019 Simon Glass <sjg@chromium.org>

env: Drop the ACTION typedef

Avoid using a typedef here which is unnecessary. Add an 'env_' prefix to
both the enum and its members to make it clear that these are related to
the environment.

Add an ENV prefix to these two flags so that it is clear what they relate
to. Also move them to env.h since they are part of the public API. Use an
enum rather than a #define to tie them together.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 25e51e90 02-Aug-2019 Simon Glass <sjg@chromium.org>

env: Drop _ENTRY

This typedef does not need to be defined in the search.h header since it
is only used in one file (hashtable.c). Remove it from the header and
change it to a struct.

Signed-off-by: Simon Glass <sjg@chromium.org>

# dd2408ca 02-Aug-2019 Simon Glass <sjg@chromium.org>

env: Drop the ENTRY typedef

U-Boot is not supposed to use typedef for structs anymore. Also this name
is the same as the ENTRY() macro used in assembler files, and 'entry'
itself is widely used in U-Boot (>8k matches).

Drop the typedef and rename the struct to env_entry to reduce confusion.

Signed-off-by: Simon Glass <sjg@chromium.org>
Acked-by: Joe Hershberger <joe.hershberger@ni.com>

# 9dfdbd9f 30-Jan-2019 Roman Kapl <rka@sysgo.com>

hashtable: fix environment variable corruption

Only first previously deleted entry was recognized, leading hsearch_r
to think that there was no previously deleted entry. It then conluded
that a free entry was found, even if there were no free entries and it
overwrote a random entry.

This patch makes sure all deleted or free entries are always found and
also introduces constants for the 0 and -1 numbers. Unit tests to excersise a
simple hash table usage and catch the corruption were added.

To trash your environment, simply run this loop:

setenv i 0
while true; do
setenv v_$i $i
setenv v_$i
setexpr i $i + 1
done

Signed-off-by: Roman Kapl <rka@sysgo.com>

# 320194ae 23-Jan-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

hashtable: remove caps buffer

slre_match() checks if caps == NULL. In this case it does not try to
update it. So there is no need to create a buffer caps which we do not
evaluate.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 4bca3249 14-Dec-2018 AKASHI Takahiro <takahiro.akashi@linaro.org>

hashtable: fix length calculation in hexport_r

The commit below incorrectly fixed hexport_r();
> size = totlen + 1;
One extra byte is necessary to NULL-terminate a whole buffer, "resp."

Fixes: f1b20acb4a03 ("hashtable: Fix length calculation in hexport_r")
Signed-off-by: AKASHI Takahiro <takahiro.akashi@linaro.org>

# f1b20acb 17-Jul-2018 Zubair Lutfullah Kakakhel <zubair@resin.io>

hashtable: Fix length calculation in hexport_r

The length returned by hexport_r has a few redundant characters.
This appears as NULL characters at the end so seems harmless.

Remove the surplus counts in two places

totlen += strlen(ep->key) + 2;
I'm guessing the +2 here is for = and sep char. But there is another
totlen += 2; line that does that.

size = totletn + 1;
Doesn't make sense and isn't justified with any comment.

Signed-off-by: Zubair Lutfullah Kakakhel <zubair@resin.io>

# d9fc9077 09-Jul-2018 Quentin Schulz <quentin.schulz@bootlin.com>

hashtable: do not recreate whole hash table if vars are passed to himport_r

When vars are passed to the himport_r function with H_NOCLEAR flag,
those vars will be overridden in the current environment and if one of
those vars is not in the imported environment, it'll be deleted in the
current environment whatever the flag passed to himport_r.

The H_NOCLEAR flag is used to clear the whole environment whether vars
are passed to the function or not.

This leads to incoherent behaviour. If one passes vars to himport_r
with the H_NOCLEAR flag, if a var in vars is not in the imported env,
that var will be removed from the current env.

If one passes vars to himport_r without the H_NOCLEAR flag, the whole
environment will be removed and vars will be imported from the
environment in RAM.

It makes more sense to keep the variable that is in the current
environment but not in the imported environment if the H_NOCLEAR flag is
set and remove only that variable if the H_NOCLEAR flag is not set.

Let's clear the whole environment only if H_NOCLEAR and vars are not
passed to himport_r.

Let's remove variables that are in the current environment but not in
the imported env only if the H_NOCLEAR flag is not passed.

Suggested-by: Wolfgang Denk <wd@denx.de>
Signed-off-by: Quentin Schulz <quentin.schulz@bootlin.com>

# 83d290c5 06-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert all of our single license tags to Linux Kernel style

When U-Boot started using SPDX tags we were among the early adopters and
there weren't a lot of other examples to borrow from. So we picked the
area of the file that usually had a full license text and replaced it
with an appropriate SPDX-License-Identifier: entry. Since then, the
Linux Kernel has adopted SPDX tags and they place it as the very first
line in a file (except where shebangs are used, then it's second line)
and with slightly different comment styles than us.

In part due to community overlap, in part due to better tag visibility
and in part for other minor reasons, switch over to that style.

This commit changes all instances where we have a single declared
license in the tag as both the before and after are identical in tag
contents. There's also a few places where I found we did not have a tag
and have introduced one.

Signed-off-by: Tom Rini <trini@konsulko.com>

# d2d9bdfc 28-Sep-2016 B, Ravi <ravibabu@ti.com>

spl: saveenv: adding saveenv support in SPL

By default saveenv option is not supported for SPL. This patch
enable the support for save environment variable for SPL build.

Enable save environment support in SPL after setenv. By default
the saveenv option is not provided in SPL, but some boards need
this support in 'Falcon' boot, where SPL need to boot from
different images based on environment variable set by OS. For
example OS may set "reboot_image" environment variable to
"recovery" inorder to boot recovery image by SPL. The SPL read
"reboot_image" and act accordingly and change the reboot_image
to default mode using setenv and save the environemnt.

Signed-off-by: Ravi Babu <ravibabu@ti.com>
Reviewed-by: Simon Glass <sig@chromium.org>

change in v1:
- dropped SUPPORT, use CONFIG_SPL_SAVEENV
- updates the comments in mmc_private.h

# fc0b5948 07-Sep-2016 Robert P. J. Day <rpjday@crashcourse.ca>

Various, accumulated typos collected from around the tree.

Fix various misspellings of:

* deprecated
* partition
* preceding,preceded
* preparation
* its versus it's
* export
* existing
* scenario
* redundant
* remaining
* value
* architecture

Signed-off-by: Robert P. J. Day <rpjday@crashcourse.ca>
Reviewed-by: Jagan Teki <jteki@openedev.com>
Reviewed-by: Stefan Roese <sr@denx.de>

# c55d02b2 22-Jul-2016 Simon Glass <sjg@chromium.org>

hashtable: Fix compiler warning on 32-bit sandbox

This fixes a mismatch between the %zu format and the type used on sandbox.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@konsulko.com>

# 62a3b7dd 15-Jul-2016 Robert P. J. Day <rpjday@crashcourse.ca>

Various, unrelated tree-wide typo fixes.

Fix a number of typos, including:

* "compatble" -> "compatible"
* "eanbeld" -> "enabled"
* "envrionment" -> "environment"
* "FTD" -> "FDT" (for "flattened device tree")
* "ommitted" -> "omitted"
* "overriden" -> "overridden"
* "partiton" -> "partition"
* "propogate" -> "propagate"
* "resourse" -> "resource"
* "rest in piece" -> "rest in peace"
* "suport" -> "support"
* "varible" -> "variable"

Signed-off-by: Robert P. J. Day <rpjday@crashcourse.ca>

# 0226d878 13-Sep-2015 Lukasz Majewski <l.majewski@majess.pl>

env: import: hashtable: Free memory allocated before exiting from himport_r()

ithout this patch memory is not released on early exit.

Signed-off-by: Lukasz Majewski <l.majewski@majess.pl>

# 817e48d8 13-Sep-2015 Lukasz Majewski <l.majewski@majess.pl>

env: import: hashtable: Prevent buffer overrun when importing environment from file

Lets consider following scenario:
- One uses echo -n "key=value" to define environment variable in a file (single variable)
- The file content is "key=value" without any terminating byte (e.g. 0x0a or
0x0d).
- The file is loaded to u-boot non zero'ed RAM buffer (with load command).
- Then "env import -t -r $loadaddr $filesize" is executed.
- Due to lack of proper termination byte we have classical example of buffer
overrun.

This patch prevents from this by allocating one extra byte than size and
explicitly null terminate it.

There should be no change for normal env import operation after applying
this patch.

Signed-off-by: Lukasz Majewski <l.majewski@majess.pl>

# ecd1446f 14-Jul-2014 Alexander Holler <holler@ahsoftware.de>

Add option -r to env import to allow import of text files with CRLF as line endings

When this option is enabled, CRLF is treated like LF when importing environments
from text files, which means CRs ('\r') in front of LFs ('\n') are just ignored.

Drawback of enabling this option is that (maybe exported) variables which have
a trailing CR in their content will get imported without that CR. But this
drawback is very unlikely and the big advantage of letting Windows user create
a *working* uEnv.txt too is likely more welcome.

Signed-off-by: Alexander Holler <holler@ahsoftware.de>

# 9a832331 08-Oct-2013 Pierre Aubert <p.aubert@staubli.com>

env: fix the env export varname

The env export command doesn't export the first variable of the list
since commit 5a31ea04c9ee5544fbb70ad7597ea4b294840eab
"env grep" - reimplement command using hexport_r()

Signed-off-by: Pierre Aubert <p.aubert@staubli.com>

# 1bce2aeb 16-Sep-2013 Robert P. J. Day <rpjday@crashcourse.ca>

Cosmetic: Fix a number of typos, no functional changes.

Fix various misspellings of things like "environment", "kernel",
"default" and "volatile", and throw in a couple grammar fixes.

Signed-off-by: Robert P. J. Day <rpjday@crashcourse.ca>

# eee479cf 08-Jul-2013 Wolfgang Denk <wd@denx.de>

Add LGPL-2.1+ SPDX-License-Identifier to source files

Signed-off-by: Wolfgang Denk <wd@denx.de>

# e4fdcadd 28-Apr-2013 Lucian Cojocar <cojocar@gmail.com>

env: throw an error when an empty key is used

If the environment contains an entry like "=value" "\0" we should throw
an error when parsing the environment. Otherwise, U-Boot will enter in
an infinite loop.

Signed-off-by: Lucian Cojocar <cojocar@gmail.com>

# be29df6a 23-Mar-2013 Wolfgang Denk <wd@denx.de>

"env grep" - add support for regular expression matches

When CONFIG_REGEX is enabled, the new option "-e" becomes available
which causes regular expression matches to be used. This allows for
example things like these:

- print all MAC addresses:

=> env grep -e eth.*addr
eth1addr=00:10:ec:80:c5:15
ethaddr=00:10:ec:00:c5:15

- print all variables that have at least 2 colons in their value:

=> env grep -v -e :.*:
addip=setenv bootargs ${bootargs} ip=${ipaddr}:${serverip}:${gatewayip}:${netmask}:${hostname}:${netdev}:off
panic=1
eth1addr=00:10:ec:80:c5:15
ethaddr=00:10:ec:00:c5:15
ver=U-Boot 2013.04-rc1-00289-g497746b-dirty (Mar 22 2013 - 12:50:25)

etc.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# 5a31ea04 23-Mar-2013 Wolfgang Denk <wd@denx.de>

"env grep" - reimplement command using hexport_r()

Also drop hstrstr_r() which is not needed any more.
The new code is way more flexible.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# ea009d47 23-Mar-2013 Wolfgang Denk <wd@denx.de>

hashtable: preparations to use hexport_r() for "env grep"

The output of "env grep" is unsorted, and printing is done by a
private implementation to parse the hash table. We have all the
needed code in place in hexport_r() alsready, so let's use this
instead. Here we prepare the code for this, without any functional
changes yet.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# 2598090b 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Add environment variable flags

Currently just validates variable types as decimal, hexidecimal,
boolean, ip address, and mac address.

If the entry is not found in the env ".flags", then look in the static
one. This allows the env to override the static definitions, but prevents
the need to have every definition in the environment distracting you.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# 170ab110 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Add support for callbacks to environment vars

Add support for per-variable callbacks to the "hashtable" functions.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

!!!fix comment in callback

# be11235a 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Hide '.' variables in env print by default

When printing all variables with env print, don't print variables that
begin with '.'. If env print is called with a '-a' switch, then
include variables that begin with '.' (just like the ls command).

Variables printed explicitly will be printed even without the -a.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# 7afcf3a5 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Refactor apply into change_ok

Move the read of the old value to inside the check function. In some
cases it can be avoided all together and at the least the code is only
called from one place.

Also name the function and the callback to more clearly describe what
it does.

Pass the ENTRY instead of just the name for direct access to the whole
data structure.

Pass an enum to the callback that specifies the operation being approved.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# 3d3b52f2 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Consolidate common code in hsearch_r()

The same chunk of code was replicated in two places and the following
changes will make that chunk grow a bit, so combine into a static func.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# c4e0057f 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Refactor do_apply to a flag

Use a flag in hsearch_r for insert mode passed from import to allow the
behavior be different based on use.

Now that "do_check" is called for all imports, ensure console init is
complete before updating the console on relocation import

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# 7ac2fe2d 17-Sep-2012 Ilya Yanok <ilya.yanok@cogentembedded.com>

OMAP: networking support for SPL

This patch adds support for networking in SPL. Some devices are
capable of loading SPL via network so it makes sense to load the
main U-Boot binary via network too. This patch tries to use
existing network code as much as possible. Unfortunately, it depends
on environment which in turn depends on other code so SPL size
is increased significantly. No effort was done to decouple network
code and environment so far.

Signed-off-by: Ilya Yanok <ilya.yanok@cogentembedded.com>
Acked-by: Joe Hershberger <joe.hershberger@ni.com>
Signed-off-by: Tom Rini <trini@ti.com>

# d5370feb 26-Aug-2012 Gerlando Falauto <gerlando.falauto@keymile.com>

env: delete selected vars not present in imported env

When variables explicitly specified on the command line are not present
in the imported env, delete them from the running env.
If the variable is also missing from the running env, issue a warning.

Signed-off-by: Gerlando Falauto <gerlando.falauto@keymile.com>
Reviewed-by: Marek Vasut <marex@denx.de>

# 152874b6 23-Aug-2012 Gerlando Falauto <gerlando.falauto@keymile.com>

env: check and apply changes on delete/destroy

Signed-off-by: Gerlando Falauto <gerlando.falauto@keymile.com>
Reviewed-by: Marek Vasut <marex@denx.de>

# c5983592 23-Aug-2012 Gerlando Falauto <gerlando.falauto@keymile.com>

env: add check/apply logic to himport_r()

Change hashtable so that a callback function will decide whether a
variable can be overwritten, and possibly apply the changes.

So add a new field to struct hsearch_data:

o "apply" callback function to check whether a variable can be
overwritten, and possibly immediately apply the changes;
when NULL, no check is performed.

And a new argument to himport_r():
o "do_apply": whether to call the apply callback function

NOTE: This patch does not change the current behavior.

Signed-off-by: Gerlando Falauto <gerlando.falauto@keymile.com>
Reviewed-by: Marek Vasut <marex@denx.de>

# 348b1f1c 23-Aug-2012 Gerlando Falauto <gerlando.falauto@keymile.com>

env: make himport_r() selective on variables

Add 2 new arguments to himport_r():

o "nvars", "vars": number and list of variables to take into account
(0 means ALL)

NOTE: This patch does not change the current behaviour.

Signed-off-by: Gerlando Falauto <gerlando.falauto@keymile.com>
Reviewed-by: Marek Vasut <marex@denx.de>

# ff856286 21-Nov-2011 Simon Glass <sjg@chromium.org>

sandbox: Fix warnings in hashtable.c

This fixes a few printf() strings for size_t which are missing the 'z'
modifier.

Signed-off-by: Simon Glass <sjg@chromium.org>
Acked-by: Mike Frysinger <vapier@gentoo.org>

# 37f2fe74 06-Nov-2011 Wolfgang Denk <wd@denx.de>

env: allow to export only selected variables

New syntax:
env export [-t | -b | -c] [-s size] addr [var ...]

With this change it is possible to provide a list of variables names
that shall be exported. Whenno arguments are given, the whole
environment gets exported.

NOTE: The new handling of the "size" argument means a change to the
user API.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# 4d91a6ec 23-Aug-2011 Jason Hobbs <jason.hobbs@calxeda.com>

Replace space and tab checks with isblank

These are various places I found that checked for conditions equivalent
to isblank.

Signed-off-by: Jason Hobbs <jason.hobbs@calxeda.com>

# 84b5e802 29-Jul-2011 Wolfgang Denk <wd@denx.de>

Constify getenv(), setenv() and hash code functions

This is needed to get rid of build warnings like

main.c:311: warning: passing argument 2 of 'setenv' discards qualifiers from pointer target type

which result from commit 09c2e90 "unify version_string".

Signed-off-by: Wolfgang Denk <wd@denx.de>
Cc: Andreas Bie�mann <andreas.devel@googlemail.com>

# a000b795 05-Apr-2011 Kim Phillips <kim.phillips@freescale.com>

common: add a grepenv command

u-boot environments, esp. when boards are shared across multiple
users, can get pretty large and time consuming to visually parse.
The grepenv command this patch adds can be used in lieu of printenv
to facilitate searching. grepenv works like printenv but limits
its output only to environment strings (variable name and value
pairs) that match the user specified substring.

the following examples are on a board with a 5313 byte environment
that spans multiple screen pages:

Example 1: summarize ethernet configuration:

=> grepenv eth TSEC
etact=FM1@DTSEC2
eth=FM1@DTSEC4
ethact=FM1@DTSEC2
eth1addr=00:E0:0C:00:8b:01
eth2addr=00:E0:0C:00:8b:02
eth3addr=00:E0:0C:00:8b:03
eth4addr=00:E0:0C:00:8b:04
eth5addr=00:E0:0C:00:8b:05
eth6addr=00:E0:0C:00:8b:06
eth7addr=00:E0:0C:00:8b:07
eth8addr=00:E0:0C:00:8b:08
eth9addr=00:E0:0C:00:8b:09
ethaddr=00:E0:0C:00:8b:00
netdev=eth0
uprcw=setenv ethact $eth;setenv filename p4080ds/R_PPSXX_0xe/rcw_0xe_2sgmii_rev2_high.bin;setenv start 0xe8000000;protect off all;run upimage;protect on all
upuboot=setenv ethact $eth;setenv filename u-boot.bin;setenv start eff80000;protect off all;run upimage;protect on all
upucode=setenv ethact $eth;setenv filename fsl_fman_ucode_P4080_101_6.bin;setenv start 0xef000000;protect off all;run upimage;protect on all
usdboot=setenv ethact $eth;tftp 1000000 $dir/$bootfile;tftp 2000000 $dir/initramfs.cpio.gz.uboot;tftp c00000 $dir/p4080ds-usdpaa.dtb;setenv bootargs root=/dev/ram rw console=ttyS0,115200 $othbootargs;bootm 1000000 2000000 c00000;
=>

Example 2: detect unused env vars:

=> grepenv etact
etact=FM1@DTSEC2
=>

Example 3: reveal hardcoded variables; e.g., for fdtaddr:

=> grepenv fdtaddr
fdtaddr=c00000
nfsboot=setenv bootargs root=/dev/nfs rw nfsroot=$serverip:$rootpath ip=$ipaddr:$serverip:$gatewayip:$netmask:$hostname:$netdev:off console=$consoledev,$baudrate $othbootargs;tftp $loadaddr $bootfile;tftp $fdtaddr $fdtfile;bootm $loadaddr - $fdtaddr
ramboot=setenv bootargs root=/dev/ram rw console=$consoledev,$baudrate $othbootargs;tftp $ramdiskaddr $ramdiskfile;tftp $loadaddr $bootfile;tftp $fdtaddr $fdtfile;bootm $loadaddr $ramdiskaddr $fdtaddr
=> grep $fdtaddr
fdtaddr=c00000
my_boot=bootm 0x40000000 0x41000000 0x00c00000
my_dtb=tftp 0x00c00000 $prefix/p4080ds.dtb
nohvboot=tftp 1000000 $dir/$bootfile;tftp 2000000 $dir/$ramdiskfile;tftp c00000 $dir/$fdtfile;setenv bootargs root=/dev/ram rw ramdisk_size=0x10000000 console=ttyS0,115200;bootm 1000000 2000000 c00000;
=>

This patch also enables the grepenv command by default on
corenet_ds based boards (and repositions the DHCP command
entry to keep the list sorted).

Signed-off-by: Kim Phillips <kim.phillips@freescale.com>
Cc: Kumar Gala <kumar.gala@freescale.com>
Cc: Andy Fleming <afleming@freescale.com>

# af4d9074 04-Apr-2011 Kim Phillips <kim.phillips@freescale.com>

env: fix env var autocompletion

commit 560d424b6d7cd4205b062ad95f1b104bd4f8bcc3 "env: re-add
support for auto-completion" fell short of its description -
the 'used' logic in hmatch_r was reversed - 'used' is 0 if
the hash table entry is not used, or -1 if deleted. This
patch makes hmatch_r actually match on valid ('used') entries,
instead of skipping them and failing to match anything.

typing 'printenv tft' and hitting 'tab' now displays valid
choices for variable names.

Signed-off-by: Kim Phillips <kim.phillips@freescale.com>
Cc: Mike Frysinger <vapier@gentoo.org>
Tested-by: Mike Frysinger <vapier@gentoo.org>

# c81c1222 21-Mar-2011 Peter Barada <peter.barada@logicpd.com>

Fix hash table deletion to prevent lost entries

Use negative used value to mark deleted entry. Search keeps probing
past deleted entries. Adding an entry uses first deleted entry when
it hits end of probe chain.

Initially found that "ramdiskimage" and "preboot" collide modulus 347,
causing "preboot" to be inserted at idx 190, "ramdiskimage" at idx 191.
Previous to this fix when "preboot" is deleted, "ramdiskimage" is
orphaned.

Signed-off-by: Peter Barada <peter.barada@logicpd.com>
Tested-by: Wolfgang Denk <wd@denx.de>

# 560d424b 17-Dec-2010 Mike Frysinger <vapier@gentoo.org>

env: re-add support for auto-completion

Currently, only basic completion is supported (no globs), but this is
what we had previously.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 2eb1573f 08-Dec-2010 Mike Frysinger <vapier@gentoo.org>

hashtable: drop all non-reentrant versions

The non-reentrant versions of the hashtable functions operate on a single
shared hashtable. So if two different people try using these funcs for
two different purposes, they'll cause problems for the other.

Avoid this by converting all existing hashtable consumers over to the
reentrant versions and then punting the non-reentrant ones.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 071bc923 27-Oct-2010 Wolfgang Denk <wd@denx.de>

Coding Style cleanup

Signed-off-by: Wolfgang Denk <wd@denx.de>

# fc5fc76b 01-Oct-2010 Andreas Bießmann <andreas.devel@googlemail.com>

lib/hashtable.c: add CONFIG_ENV_MIN_ENTRIES

This patch adds a new config parameter for adjusting the calculation of
hash table size when importing a buffer.

When importing a extremely small buffer (e.g. the default_environment)
the old calculation generated a hash table which could hold at most the
buffer content but no more entires.

The new calculation add a fixed number of entries to the result to fit
better for small import buffers. This amount may be configured by the
user in board file to adjust the behaviour.

Signed-off-by: Andreas Bie�mann <andreas.devel@googlemail.com>

# ea882baf 20-Jun-2010 Wolfgang Denk <wd@denx.de>

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>

# a6826fbc 20-Jun-2010 Wolfgang Denk <wd@denx.de>

Add hash table support as base for new environment code

This implementation is based on code from uClibc-0.9.30.3 but was
modified and extended for use within U-Boot.

Major modifications and extensions:

* hsearch() [modified / extended]:
- While the standard version does not make any assumptions about
the type of the stored data objects at all, this implementation
works with NUL terminated strings only.
- Instead of storing just pointers to the original objects, we
create local copies so the caller does not need to care about the
data any more.
- The standard implementation does not provide a way to update an
existing entry. This version will create a new entry or update an
existing one when both "action == ENTER" and "item.data != NULL".
- hsearch_r(): Instead of returning 1 on success, we return the
index into the internal hash table, which is also guaranteed to be
positive. This allows us direct access to the found hash table
slot for example for functions like hdelete().
* hdelete() [added]:
- The standard implementation of hsearch(3) does not provide any way
to delete any entries from the hash table. We extend the code to
do that.
* hexport() [added]:
- Export the data stored in the hash table in linearized form:
Entries are exported as "name=value" strings, separated by an
arbitrary (non-NUL, of course) separator character. This allows to
use this function both when formatting the U-Boot environment for
external storage (using '\0' as separator), but also when using it
for the "printenv" command to print all variables, simply by using
as '\n" as separator. This can also be used for new features like
exporting the environment data as text file, including the option
for later re-import.
- The entries in the result list will be sorted by ascending key
values.
* himport() [added]:
- Import linearized data into hash table. This is the inverse
function to hexport(): it takes a linear list of "name=value"
pairs and creates hash table entries from it.
- Entries without "value", i. e. consisting of only "name" or
"name=", will cause this entry to be deleted from the hash table.
- The "flag" argument can be used to control the behaviour: when
the H_NOCLEAR bit is set, then an existing hash table will kept,
i. e. new data will be added to an existing hash table;
otherwise, old data will be discarded and a new hash table will
be created.
- The separator character for the "name=value" pairs can be
selected, so we both support importing from externally stored
environment data (separated by NUL characters) and from plain text
files (entries separated by newline characters).
- To allow for nicely formatted text input, leading white space
(sequences of SPACE and TAB chars) is ignored, and entries
starting (after removal of any leading white space) with a '#'
character are considered comments and ignored.
- NOTE: this means that a variable name cannot start with a '#'
character.
- When using a non-NUL separator character, backslash is used as
escape character in the value part, allowing for example fo
multi-line values.
- In theory, arbitrary separator characters can be used, but only
'\0' and '\n' have really been tested.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# 96434a76 05-Nov-2020 Simon Glass <sjg@chromium.org>

env: Allow returning errors from hdelete_r()

At present this function returns 1 on success and 0 on failure. But in
the latter case it provides no indication of what went wrong.

If an attempt is made to delete a non-existent variable, the caller may
want to ignore this error. This happens when setting a non-existent
variable to "", for example.

Update the function to return 0 on success and a useful error code on
failure. Add a function comment too.

Make sure that env_set() does not return an error if it is deleting a
variable that doesn't exist. We could update env_set() to return useful
error numbers also, but that is beyond the scope of this change.

Signed-off-by: Simon Glass <sjg@chromium.org>

wip

# 34ca77c1 20-Aug-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

lib/hashtable: remove superfluous check

We assign first_deleted = 0. There is no need to check its value without
any further assignment in between.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# d045cbac 07-Jul-2020 Marek Vasut <marex@denx.de>

env: Add support for explicit write access list

This option marks any U-Boot variable which does not have explicit 'w'
writeable flag set as read-only. This way the environment can be locked
down and only variables explicitly configured to be writeable can ever
be changed by either 'env import', 'env set' or loading user environment
from environment storage.

Signed-off-by: Marek Vasut <marex@denx.de>
Reviewed-by: Tom Rini <trini@konsulko.com>

# 47f3b1f2 07-Jul-2020 Marek Vasut <marex@denx.de>

env: Add option to only ever append environment

Add configuration option which prevents the environment hash table to be
ever cleared and reloaded with different content. This is useful in case
the first environment loaded into the hash table contains e.g. sensitive
content which must not be dropped or reloaded.

Signed-off-by: Marek Vasut <marex@denx.de>
Reviewed-by: Tom Rini <trini@konsulko.com>

# 60ffcf21 24-Jun-2020 Sean Anderson <seanga2@gmail.com>

lib: Always set errno in hcreate_r

This could give a confusing error message if it failed and didn't set
errno.

Signed-off-by: Sean Anderson <seanga2@gmail.com>
Reviewed-by: Bin Meng <bmeng.cn@gmail.com>

# f7ae49fc 10-May-2020 Simon Glass <sjg@chromium.org>

common: Drop log.h from common header

Move this header out of the common header.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 09140113 10-May-2020 Simon Glass <sjg@chromium.org>

command: Remove the cmd_tbl_t typedef

We should not use typedefs in U-Boot. They cannot be used as forward
declarations which means that header files must include the full header to
access them.

Drop the typedef and rename the struct to remove the _s suffix which is
now not useful.

This requires quite a few header-file additions.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 080019b8 27-Feb-2020 Rasmus Villemoes <rasmus.villemoes@prevas.dk>

make env_entry::callback conditional on !CONFIG_SPL_BUILD

The callback member of struct env_entry is always NULL for an SPL
build. Removing it thus saves a bit of run-time memory in the
SPL (when CONFIG_SPL_ENV_SUPPORT=y) since struct env_entry is embedded
in struct env_entry_node - i.e. about 2KB for the normal case of
512+change hash table entries.

Two small fixups are needed for this, all other references to the
callback member are already under !CONFIG_SPL_BUILD: Don't initialize
.callback in set_flags() - hsearch_r doesn't use that value
anyway. And make env_callback_init() initialize ->callback to NULL for
a new entry instead of relying on an unused or deleted entry having
NULL in ->callback.

Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 34284970 27-Feb-2020 Rasmus Villemoes <rasmus.villemoes@prevas.dk>

lib/hashtable.c: don't test ->callback in SPL

In SPL, environment callbacks are not supported, so e->callback is
always NULL. Removing this makes the SPL a little smaller (about 400
bytes in my ppc build) with no functional change.

Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 7f529f65 27-Feb-2020 Rasmus Villemoes <rasmus.villemoes@prevas.dk>

lib/hashtable.c: create helper for calling env_entry::callback

This is preparation for compiling out the "call the callback" code and
associated error handling for SPL, where ->callback is always NULL.

Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 8bef79bf 14-Nov-2019 Simon Glass <sjg@chromium.org>

common: Move sorting functions to their own header file

These don't need to be in common.h so move them out into a new header.
Also add some missing comments.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@konsulko.com>

# 3f0d6807 01-Aug-2019 Simon Glass <sjg@chromium.org>

env: Drop the ACTION typedef

Avoid using a typedef here which is unnecessary. Add an 'env_' prefix to
both the enum and its members to make it clear that these are related to
the environment.

Add an ENV prefix to these two flags so that it is clear what they relate
to. Also move them to env.h since they are part of the public API. Use an
enum rather than a #define to tie them together.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 25e51e90 02-Aug-2019 Simon Glass <sjg@chromium.org>

env: Drop _ENTRY

This typedef does not need to be defined in the search.h header since it
is only used in one file (hashtable.c). Remove it from the header and
change it to a struct.

Signed-off-by: Simon Glass <sjg@chromium.org>

# dd2408ca 02-Aug-2019 Simon Glass <sjg@chromium.org>

env: Drop the ENTRY typedef

U-Boot is not supposed to use typedef for structs anymore. Also this name
is the same as the ENTRY() macro used in assembler files, and 'entry'
itself is widely used in U-Boot (>8k matches).

Drop the typedef and rename the struct to env_entry to reduce confusion.

Signed-off-by: Simon Glass <sjg@chromium.org>
Acked-by: Joe Hershberger <joe.hershberger@ni.com>

# 9dfdbd9f 30-Jan-2019 Roman Kapl <rka@sysgo.com>

hashtable: fix environment variable corruption

Only first previously deleted entry was recognized, leading hsearch_r
to think that there was no previously deleted entry. It then conluded
that a free entry was found, even if there were no free entries and it
overwrote a random entry.

This patch makes sure all deleted or free entries are always found and
also introduces constants for the 0 and -1 numbers. Unit tests to excersise a
simple hash table usage and catch the corruption were added.

To trash your environment, simply run this loop:

setenv i 0
while true; do
setenv v_$i $i
setenv v_$i
setexpr i $i + 1
done

Signed-off-by: Roman Kapl <rka@sysgo.com>

# 320194ae 23-Jan-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

hashtable: remove caps buffer

slre_match() checks if caps == NULL. In this case it does not try to
update it. So there is no need to create a buffer caps which we do not
evaluate.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 4bca3249 14-Dec-2018 AKASHI Takahiro <takahiro.akashi@linaro.org>

hashtable: fix length calculation in hexport_r

The commit below incorrectly fixed hexport_r();
> size = totlen + 1;
One extra byte is necessary to NULL-terminate a whole buffer, "resp."

Fixes: f1b20acb4a03 ("hashtable: Fix length calculation in hexport_r")
Signed-off-by: AKASHI Takahiro <takahiro.akashi@linaro.org>

# f1b20acb 17-Jul-2018 Zubair Lutfullah Kakakhel <zubair@resin.io>

hashtable: Fix length calculation in hexport_r

The length returned by hexport_r has a few redundant characters.
This appears as NULL characters at the end so seems harmless.

Remove the surplus counts in two places

totlen += strlen(ep->key) + 2;
I'm guessing the +2 here is for = and sep char. But there is another
totlen += 2; line that does that.

size = totletn + 1;
Doesn't make sense and isn't justified with any comment.

Signed-off-by: Zubair Lutfullah Kakakhel <zubair@resin.io>

# d9fc9077 09-Jul-2018 Quentin Schulz <quentin.schulz@bootlin.com>

hashtable: do not recreate whole hash table if vars are passed to himport_r

When vars are passed to the himport_r function with H_NOCLEAR flag,
those vars will be overridden in the current environment and if one of
those vars is not in the imported environment, it'll be deleted in the
current environment whatever the flag passed to himport_r.

The H_NOCLEAR flag is used to clear the whole environment whether vars
are passed to the function or not.

This leads to incoherent behaviour. If one passes vars to himport_r
with the H_NOCLEAR flag, if a var in vars is not in the imported env,
that var will be removed from the current env.

If one passes vars to himport_r without the H_NOCLEAR flag, the whole
environment will be removed and vars will be imported from the
environment in RAM.

It makes more sense to keep the variable that is in the current
environment but not in the imported environment if the H_NOCLEAR flag is
set and remove only that variable if the H_NOCLEAR flag is not set.

Let's clear the whole environment only if H_NOCLEAR and vars are not
passed to himport_r.

Let's remove variables that are in the current environment but not in
the imported env only if the H_NOCLEAR flag is not passed.

Suggested-by: Wolfgang Denk <wd@denx.de>
Signed-off-by: Quentin Schulz <quentin.schulz@bootlin.com>

# 83d290c5 06-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert all of our single license tags to Linux Kernel style

When U-Boot started using SPDX tags we were among the early adopters and
there weren't a lot of other examples to borrow from. So we picked the
area of the file that usually had a full license text and replaced it
with an appropriate SPDX-License-Identifier: entry. Since then, the
Linux Kernel has adopted SPDX tags and they place it as the very first
line in a file (except where shebangs are used, then it's second line)
and with slightly different comment styles than us.

In part due to community overlap, in part due to better tag visibility
and in part for other minor reasons, switch over to that style.

This commit changes all instances where we have a single declared
license in the tag as both the before and after are identical in tag
contents. There's also a few places where I found we did not have a tag
and have introduced one.

Signed-off-by: Tom Rini <trini@konsulko.com>

# d2d9bdfc 28-Sep-2016 B, Ravi <ravibabu@ti.com>

spl: saveenv: adding saveenv support in SPL

By default saveenv option is not supported for SPL. This patch
enable the support for save environment variable for SPL build.

Enable save environment support in SPL after setenv. By default
the saveenv option is not provided in SPL, but some boards need
this support in 'Falcon' boot, where SPL need to boot from
different images based on environment variable set by OS. For
example OS may set "reboot_image" environment variable to
"recovery" inorder to boot recovery image by SPL. The SPL read
"reboot_image" and act accordingly and change the reboot_image
to default mode using setenv and save the environemnt.

Signed-off-by: Ravi Babu <ravibabu@ti.com>
Reviewed-by: Simon Glass <sig@chromium.org>

change in v1:
- dropped SUPPORT, use CONFIG_SPL_SAVEENV
- updates the comments in mmc_private.h

# fc0b5948 07-Sep-2016 Robert P. J. Day <rpjday@crashcourse.ca>

Various, accumulated typos collected from around the tree.

Fix various misspellings of:

* deprecated
* partition
* preceding,preceded
* preparation
* its versus it's
* export
* existing
* scenario
* redundant
* remaining
* value
* architecture

Signed-off-by: Robert P. J. Day <rpjday@crashcourse.ca>
Reviewed-by: Jagan Teki <jteki@openedev.com>
Reviewed-by: Stefan Roese <sr@denx.de>

# c55d02b2 22-Jul-2016 Simon Glass <sjg@chromium.org>

hashtable: Fix compiler warning on 32-bit sandbox

This fixes a mismatch between the %zu format and the type used on sandbox.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@konsulko.com>

# 62a3b7dd 15-Jul-2016 Robert P. J. Day <rpjday@crashcourse.ca>

Various, unrelated tree-wide typo fixes.

Fix a number of typos, including:

* "compatble" -> "compatible"
* "eanbeld" -> "enabled"
* "envrionment" -> "environment"
* "FTD" -> "FDT" (for "flattened device tree")
* "ommitted" -> "omitted"
* "overriden" -> "overridden"
* "partiton" -> "partition"
* "propogate" -> "propagate"
* "resourse" -> "resource"
* "rest in piece" -> "rest in peace"
* "suport" -> "support"
* "varible" -> "variable"

Signed-off-by: Robert P. J. Day <rpjday@crashcourse.ca>

# 0226d878 13-Sep-2015 Lukasz Majewski <l.majewski@majess.pl>

env: import: hashtable: Free memory allocated before exiting from himport_r()

ithout this patch memory is not released on early exit.

Signed-off-by: Lukasz Majewski <l.majewski@majess.pl>

# 817e48d8 13-Sep-2015 Lukasz Majewski <l.majewski@majess.pl>

env: import: hashtable: Prevent buffer overrun when importing environment from file

Lets consider following scenario:
- One uses echo -n "key=value" to define environment variable in a file (single variable)
- The file content is "key=value" without any terminating byte (e.g. 0x0a or
0x0d).
- The file is loaded to u-boot non zero'ed RAM buffer (with load command).
- Then "env import -t -r $loadaddr $filesize" is executed.
- Due to lack of proper termination byte we have classical example of buffer
overrun.

This patch prevents from this by allocating one extra byte than size and
explicitly null terminate it.

There should be no change for normal env import operation after applying
this patch.

Signed-off-by: Lukasz Majewski <l.majewski@majess.pl>

# ecd1446f 14-Jul-2014 Alexander Holler <holler@ahsoftware.de>

Add option -r to env import to allow import of text files with CRLF as line endings

When this option is enabled, CRLF is treated like LF when importing environments
from text files, which means CRs ('\r') in front of LFs ('\n') are just ignored.

Drawback of enabling this option is that (maybe exported) variables which have
a trailing CR in their content will get imported without that CR. But this
drawback is very unlikely and the big advantage of letting Windows user create
a *working* uEnv.txt too is likely more welcome.

Signed-off-by: Alexander Holler <holler@ahsoftware.de>

# 9a832331 08-Oct-2013 Pierre Aubert <p.aubert@staubli.com>

env: fix the env export varname

The env export command doesn't export the first variable of the list
since commit 5a31ea04c9ee5544fbb70ad7597ea4b294840eab
"env grep" - reimplement command using hexport_r()

Signed-off-by: Pierre Aubert <p.aubert@staubli.com>

# 1bce2aeb 16-Sep-2013 Robert P. J. Day <rpjday@crashcourse.ca>

Cosmetic: Fix a number of typos, no functional changes.

Fix various misspellings of things like "environment", "kernel",
"default" and "volatile", and throw in a couple grammar fixes.

Signed-off-by: Robert P. J. Day <rpjday@crashcourse.ca>

# eee479cf 08-Jul-2013 Wolfgang Denk <wd@denx.de>

Add LGPL-2.1+ SPDX-License-Identifier to source files

Signed-off-by: Wolfgang Denk <wd@denx.de>

# e4fdcadd 28-Apr-2013 Lucian Cojocar <cojocar@gmail.com>

env: throw an error when an empty key is used

If the environment contains an entry like "=value" "\0" we should throw
an error when parsing the environment. Otherwise, U-Boot will enter in
an infinite loop.

Signed-off-by: Lucian Cojocar <cojocar@gmail.com>

# be29df6a 23-Mar-2013 Wolfgang Denk <wd@denx.de>

"env grep" - add support for regular expression matches

When CONFIG_REGEX is enabled, the new option "-e" becomes available
which causes regular expression matches to be used. This allows for
example things like these:

- print all MAC addresses:

=> env grep -e eth.*addr
eth1addr=00:10:ec:80:c5:15
ethaddr=00:10:ec:00:c5:15

- print all variables that have at least 2 colons in their value:

=> env grep -v -e :.*:
addip=setenv bootargs ${bootargs} ip=${ipaddr}:${serverip}:${gatewayip}:${netmask}:${hostname}:${netdev}:off
panic=1
eth1addr=00:10:ec:80:c5:15
ethaddr=00:10:ec:00:c5:15
ver=U-Boot 2013.04-rc1-00289-g497746b-dirty (Mar 22 2013 - 12:50:25)

etc.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# 5a31ea04 23-Mar-2013 Wolfgang Denk <wd@denx.de>

"env grep" - reimplement command using hexport_r()

Also drop hstrstr_r() which is not needed any more.
The new code is way more flexible.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# ea009d47 23-Mar-2013 Wolfgang Denk <wd@denx.de>

hashtable: preparations to use hexport_r() for "env grep"

The output of "env grep" is unsorted, and printing is done by a
private implementation to parse the hash table. We have all the
needed code in place in hexport_r() alsready, so let's use this
instead. Here we prepare the code for this, without any functional
changes yet.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# 2598090b 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Add environment variable flags

Currently just validates variable types as decimal, hexidecimal,
boolean, ip address, and mac address.

If the entry is not found in the env ".flags", then look in the static
one. This allows the env to override the static definitions, but prevents
the need to have every definition in the environment distracting you.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# 170ab110 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Add support for callbacks to environment vars

Add support for per-variable callbacks to the "hashtable" functions.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

!!!fix comment in callback

# be11235a 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Hide '.' variables in env print by default

When printing all variables with env print, don't print variables that
begin with '.'. If env print is called with a '-a' switch, then
include variables that begin with '.' (just like the ls command).

Variables printed explicitly will be printed even without the -a.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# 7afcf3a5 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Refactor apply into change_ok

Move the read of the old value to inside the check function. In some
cases it can be avoided all together and at the least the code is only
called from one place.

Also name the function and the callback to more clearly describe what
it does.

Pass the ENTRY instead of just the name for direct access to the whole
data structure.

Pass an enum to the callback that specifies the operation being approved.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# 3d3b52f2 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Consolidate common code in hsearch_r()

The same chunk of code was replicated in two places and the following
changes will make that chunk grow a bit, so combine into a static func.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# c4e0057f 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Refactor do_apply to a flag

Use a flag in hsearch_r for insert mode passed from import to allow the
behavior be different based on use.

Now that "do_check" is called for all imports, ensure console init is
complete before updating the console on relocation import

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# 7ac2fe2d 17-Sep-2012 Ilya Yanok <ilya.yanok@cogentembedded.com>

OMAP: networking support for SPL

This patch adds support for networking in SPL. Some devices are
capable of loading SPL via network so it makes sense to load the
main U-Boot binary via network too. This patch tries to use
existing network code as much as possible. Unfortunately, it depends
on environment which in turn depends on other code so SPL size
is increased significantly. No effort was done to decouple network
code and environment so far.

Signed-off-by: Ilya Yanok <ilya.yanok@cogentembedded.com>
Acked-by: Joe Hershberger <joe.hershberger@ni.com>
Signed-off-by: Tom Rini <trini@ti.com>

# d5370feb 26-Aug-2012 Gerlando Falauto <gerlando.falauto@keymile.com>

env: delete selected vars not present in imported env

When variables explicitly specified on the command line are not present
in the imported env, delete them from the running env.
If the variable is also missing from the running env, issue a warning.

Signed-off-by: Gerlando Falauto <gerlando.falauto@keymile.com>
Reviewed-by: Marek Vasut <marex@denx.de>

# 152874b6 23-Aug-2012 Gerlando Falauto <gerlando.falauto@keymile.com>

env: check and apply changes on delete/destroy

Signed-off-by: Gerlando Falauto <gerlando.falauto@keymile.com>
Reviewed-by: Marek Vasut <marex@denx.de>

# c5983592 23-Aug-2012 Gerlando Falauto <gerlando.falauto@keymile.com>

env: add check/apply logic to himport_r()

Change hashtable so that a callback function will decide whether a
variable can be overwritten, and possibly apply the changes.

So add a new field to struct hsearch_data:

o "apply" callback function to check whether a variable can be
overwritten, and possibly immediately apply the changes;
when NULL, no check is performed.

And a new argument to himport_r():
o "do_apply": whether to call the apply callback function

NOTE: This patch does not change the current behavior.

Signed-off-by: Gerlando Falauto <gerlando.falauto@keymile.com>
Reviewed-by: Marek Vasut <marex@denx.de>

# 348b1f1c 23-Aug-2012 Gerlando Falauto <gerlando.falauto@keymile.com>

env: make himport_r() selective on variables

Add 2 new arguments to himport_r():

o "nvars", "vars": number and list of variables to take into account
(0 means ALL)

NOTE: This patch does not change the current behaviour.

Signed-off-by: Gerlando Falauto <gerlando.falauto@keymile.com>
Reviewed-by: Marek Vasut <marex@denx.de>

# ff856286 21-Nov-2011 Simon Glass <sjg@chromium.org>

sandbox: Fix warnings in hashtable.c

This fixes a few printf() strings for size_t which are missing the 'z'
modifier.

Signed-off-by: Simon Glass <sjg@chromium.org>
Acked-by: Mike Frysinger <vapier@gentoo.org>

# 37f2fe74 06-Nov-2011 Wolfgang Denk <wd@denx.de>

env: allow to export only selected variables

New syntax:
env export [-t | -b | -c] [-s size] addr [var ...]

With this change it is possible to provide a list of variables names
that shall be exported. Whenno arguments are given, the whole
environment gets exported.

NOTE: The new handling of the "size" argument means a change to the
user API.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# 4d91a6ec 23-Aug-2011 Jason Hobbs <jason.hobbs@calxeda.com>

Replace space and tab checks with isblank

These are various places I found that checked for conditions equivalent
to isblank.

Signed-off-by: Jason Hobbs <jason.hobbs@calxeda.com>

# 84b5e802 29-Jul-2011 Wolfgang Denk <wd@denx.de>

Constify getenv(), setenv() and hash code functions

This is needed to get rid of build warnings like

main.c:311: warning: passing argument 2 of 'setenv' discards qualifiers from pointer target type

which result from commit 09c2e90 "unify version_string".

Signed-off-by: Wolfgang Denk <wd@denx.de>
Cc: Andreas Bie�mann <andreas.devel@googlemail.com>

# a000b795 05-Apr-2011 Kim Phillips <kim.phillips@freescale.com>

common: add a grepenv command

u-boot environments, esp. when boards are shared across multiple
users, can get pretty large and time consuming to visually parse.
The grepenv command this patch adds can be used in lieu of printenv
to facilitate searching. grepenv works like printenv but limits
its output only to environment strings (variable name and value
pairs) that match the user specified substring.

the following examples are on a board with a 5313 byte environment
that spans multiple screen pages:

Example 1: summarize ethernet configuration:

=> grepenv eth TSEC
etact=FM1@DTSEC2
eth=FM1@DTSEC4
ethact=FM1@DTSEC2
eth1addr=00:E0:0C:00:8b:01
eth2addr=00:E0:0C:00:8b:02
eth3addr=00:E0:0C:00:8b:03
eth4addr=00:E0:0C:00:8b:04
eth5addr=00:E0:0C:00:8b:05
eth6addr=00:E0:0C:00:8b:06
eth7addr=00:E0:0C:00:8b:07
eth8addr=00:E0:0C:00:8b:08
eth9addr=00:E0:0C:00:8b:09
ethaddr=00:E0:0C:00:8b:00
netdev=eth0
uprcw=setenv ethact $eth;setenv filename p4080ds/R_PPSXX_0xe/rcw_0xe_2sgmii_rev2_high.bin;setenv start 0xe8000000;protect off all;run upimage;protect on all
upuboot=setenv ethact $eth;setenv filename u-boot.bin;setenv start eff80000;protect off all;run upimage;protect on all
upucode=setenv ethact $eth;setenv filename fsl_fman_ucode_P4080_101_6.bin;setenv start 0xef000000;protect off all;run upimage;protect on all
usdboot=setenv ethact $eth;tftp 1000000 $dir/$bootfile;tftp 2000000 $dir/initramfs.cpio.gz.uboot;tftp c00000 $dir/p4080ds-usdpaa.dtb;setenv bootargs root=/dev/ram rw console=ttyS0,115200 $othbootargs;bootm 1000000 2000000 c00000;
=>

Example 2: detect unused env vars:

=> grepenv etact
etact=FM1@DTSEC2
=>

Example 3: reveal hardcoded variables; e.g., for fdtaddr:

=> grepenv fdtaddr
fdtaddr=c00000
nfsboot=setenv bootargs root=/dev/nfs rw nfsroot=$serverip:$rootpath ip=$ipaddr:$serverip:$gatewayip:$netmask:$hostname:$netdev:off console=$consoledev,$baudrate $othbootargs;tftp $loadaddr $bootfile;tftp $fdtaddr $fdtfile;bootm $loadaddr - $fdtaddr
ramboot=setenv bootargs root=/dev/ram rw console=$consoledev,$baudrate $othbootargs;tftp $ramdiskaddr $ramdiskfile;tftp $loadaddr $bootfile;tftp $fdtaddr $fdtfile;bootm $loadaddr $ramdiskaddr $fdtaddr
=> grep $fdtaddr
fdtaddr=c00000
my_boot=bootm 0x40000000 0x41000000 0x00c00000
my_dtb=tftp 0x00c00000 $prefix/p4080ds.dtb
nohvboot=tftp 1000000 $dir/$bootfile;tftp 2000000 $dir/$ramdiskfile;tftp c00000 $dir/$fdtfile;setenv bootargs root=/dev/ram rw ramdisk_size=0x10000000 console=ttyS0,115200;bootm 1000000 2000000 c00000;
=>

This patch also enables the grepenv command by default on
corenet_ds based boards (and repositions the DHCP command
entry to keep the list sorted).

Signed-off-by: Kim Phillips <kim.phillips@freescale.com>
Cc: Kumar Gala <kumar.gala@freescale.com>
Cc: Andy Fleming <afleming@freescale.com>

# af4d9074 04-Apr-2011 Kim Phillips <kim.phillips@freescale.com>

env: fix env var autocompletion

commit 560d424b6d7cd4205b062ad95f1b104bd4f8bcc3 "env: re-add
support for auto-completion" fell short of its description -
the 'used' logic in hmatch_r was reversed - 'used' is 0 if
the hash table entry is not used, or -1 if deleted. This
patch makes hmatch_r actually match on valid ('used') entries,
instead of skipping them and failing to match anything.

typing 'printenv tft' and hitting 'tab' now displays valid
choices for variable names.

Signed-off-by: Kim Phillips <kim.phillips@freescale.com>
Cc: Mike Frysinger <vapier@gentoo.org>
Tested-by: Mike Frysinger <vapier@gentoo.org>

# c81c1222 21-Mar-2011 Peter Barada <peter.barada@logicpd.com>

Fix hash table deletion to prevent lost entries

Use negative used value to mark deleted entry. Search keeps probing
past deleted entries. Adding an entry uses first deleted entry when
it hits end of probe chain.

Initially found that "ramdiskimage" and "preboot" collide modulus 347,
causing "preboot" to be inserted at idx 190, "ramdiskimage" at idx 191.
Previous to this fix when "preboot" is deleted, "ramdiskimage" is
orphaned.

Signed-off-by: Peter Barada <peter.barada@logicpd.com>
Tested-by: Wolfgang Denk <wd@denx.de>

# 560d424b 17-Dec-2010 Mike Frysinger <vapier@gentoo.org>

env: re-add support for auto-completion

Currently, only basic completion is supported (no globs), but this is
what we had previously.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 2eb1573f 08-Dec-2010 Mike Frysinger <vapier@gentoo.org>

hashtable: drop all non-reentrant versions

The non-reentrant versions of the hashtable functions operate on a single
shared hashtable. So if two different people try using these funcs for
two different purposes, they'll cause problems for the other.

Avoid this by converting all existing hashtable consumers over to the
reentrant versions and then punting the non-reentrant ones.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 071bc923 27-Oct-2010 Wolfgang Denk <wd@denx.de>

Coding Style cleanup

Signed-off-by: Wolfgang Denk <wd@denx.de>

# fc5fc76b 01-Oct-2010 Andreas Bießmann <andreas.devel@googlemail.com>

lib/hashtable.c: add CONFIG_ENV_MIN_ENTRIES

This patch adds a new config parameter for adjusting the calculation of
hash table size when importing a buffer.

When importing a extremely small buffer (e.g. the default_environment)
the old calculation generated a hash table which could hold at most the
buffer content but no more entires.

The new calculation add a fixed number of entries to the result to fit
better for small import buffers. This amount may be configured by the
user in board file to adjust the behaviour.

Signed-off-by: Andreas Bie�mann <andreas.devel@googlemail.com>

# ea882baf 20-Jun-2010 Wolfgang Denk <wd@denx.de>

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>

# a6826fbc 20-Jun-2010 Wolfgang Denk <wd@denx.de>

Add hash table support as base for new environment code

This implementation is based on code from uClibc-0.9.30.3 but was
modified and extended for use within U-Boot.

Major modifications and extensions:

* hsearch() [modified / extended]:
- While the standard version does not make any assumptions about
the type of the stored data objects at all, this implementation
works with NUL terminated strings only.
- Instead of storing just pointers to the original objects, we
create local copies so the caller does not need to care about the
data any more.
- The standard implementation does not provide a way to update an
existing entry. This version will create a new entry or update an
existing one when both "action == ENTER" and "item.data != NULL".
- hsearch_r(): Instead of returning 1 on success, we return the
index into the internal hash table, which is also guaranteed to be
positive. This allows us direct access to the found hash table
slot for example for functions like hdelete().
* hdelete() [added]:
- The standard implementation of hsearch(3) does not provide any way
to delete any entries from the hash table. We extend the code to
do that.
* hexport() [added]:
- Export the data stored in the hash table in linearized form:
Entries are exported as "name=value" strings, separated by an
arbitrary (non-NUL, of course) separator character. This allows to
use this function both when formatting the U-Boot environment for
external storage (using '\0' as separator), but also when using it
for the "printenv" command to print all variables, simply by using
as '\n" as separator. This can also be used for new features like
exporting the environment data as text file, including the option
for later re-import.
- The entries in the result list will be sorted by ascending key
values.
* himport() [added]:
- Import linearized data into hash table. This is the inverse
function to hexport(): it takes a linear list of "name=value"
pairs and creates hash table entries from it.
- Entries without "value", i. e. consisting of only "name" or
"name=", will cause this entry to be deleted from the hash table.
- The "flag" argument can be used to control the behaviour: when
the H_NOCLEAR bit is set, then an existing hash table will kept,
i. e. new data will be added to an existing hash table;
otherwise, old data will be discarded and a new hash table will
be created.
- The separator character for the "name=value" pairs can be
selected, so we both support importing from externally stored
environment data (separated by NUL characters) and from plain text
files (entries separated by newline characters).
- To allow for nicely formatted text input, leading white space
(sequences of SPACE and TAB chars) is ignored, and entries
starting (after removal of any leading white space) with a '#'
character are considered comments and ignored.
- NOTE: this means that a variable name cannot start with a '#'
character.
- When using a non-NUL separator character, backslash is used as
escape character in the value part, allowing for example fo
multi-line values.
- In theory, arbitrary separator characters can be used, but only
'\0' and '\n' have really been tested.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# 34ca77c1 20-Aug-2020 Heinrich Schuchardt <xypron.glpk@gmx.de>

lib/hashtable: remove superfluous check

We assign first_deleted = 0. There is no need to check its value without
any further assignment in between.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>
Reviewed-by: Simon Glass <sjg@chromium.org>

# d045cbac 07-Jul-2020 Marek Vasut <marex@denx.de>

env: Add support for explicit write access list

This option marks any U-Boot variable which does not have explicit 'w'
writeable flag set as read-only. This way the environment can be locked
down and only variables explicitly configured to be writeable can ever
be changed by either 'env import', 'env set' or loading user environment
from environment storage.

Signed-off-by: Marek Vasut <marex@denx.de>
Reviewed-by: Tom Rini <trini@konsulko.com>

# 47f3b1f2 07-Jul-2020 Marek Vasut <marex@denx.de>

env: Add option to only ever append environment

Add configuration option which prevents the environment hash table to be
ever cleared and reloaded with different content. This is useful in case
the first environment loaded into the hash table contains e.g. sensitive
content which must not be dropped or reloaded.

Signed-off-by: Marek Vasut <marex@denx.de>
Reviewed-by: Tom Rini <trini@konsulko.com>

# 60ffcf21 24-Jun-2020 Sean Anderson <seanga2@gmail.com>

lib: Always set errno in hcreate_r

This could give a confusing error message if it failed and didn't set
errno.

Signed-off-by: Sean Anderson <seanga2@gmail.com>
Reviewed-by: Bin Meng <bmeng.cn@gmail.com>

# f7ae49fc 10-May-2020 Simon Glass <sjg@chromium.org>

common: Drop log.h from common header

Move this header out of the common header.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 09140113 10-May-2020 Simon Glass <sjg@chromium.org>

command: Remove the cmd_tbl_t typedef

We should not use typedefs in U-Boot. They cannot be used as forward
declarations which means that header files must include the full header to
access them.

Drop the typedef and rename the struct to remove the _s suffix which is
now not useful.

This requires quite a few header-file additions.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 080019b8 27-Feb-2020 Rasmus Villemoes <rasmus.villemoes@prevas.dk>

make env_entry::callback conditional on !CONFIG_SPL_BUILD

The callback member of struct env_entry is always NULL for an SPL
build. Removing it thus saves a bit of run-time memory in the
SPL (when CONFIG_SPL_ENV_SUPPORT=y) since struct env_entry is embedded
in struct env_entry_node - i.e. about 2KB for the normal case of
512+change hash table entries.

Two small fixups are needed for this, all other references to the
callback member are already under !CONFIG_SPL_BUILD: Don't initialize
.callback in set_flags() - hsearch_r doesn't use that value
anyway. And make env_callback_init() initialize ->callback to NULL for
a new entry instead of relying on an unused or deleted entry having
NULL in ->callback.

Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 34284970 27-Feb-2020 Rasmus Villemoes <rasmus.villemoes@prevas.dk>

lib/hashtable.c: don't test ->callback in SPL

In SPL, environment callbacks are not supported, so e->callback is
always NULL. Removing this makes the SPL a little smaller (about 400
bytes in my ppc build) with no functional change.

Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 7f529f65 27-Feb-2020 Rasmus Villemoes <rasmus.villemoes@prevas.dk>

lib/hashtable.c: create helper for calling env_entry::callback

This is preparation for compiling out the "call the callback" code and
associated error handling for SPL, where ->callback is always NULL.

Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 8bef79bf 14-Nov-2019 Simon Glass <sjg@chromium.org>

common: Move sorting functions to their own header file

These don't need to be in common.h so move them out into a new header.
Also add some missing comments.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@konsulko.com>

# 3f0d6807 01-Aug-2019 Simon Glass <sjg@chromium.org>

env: Drop the ACTION typedef

Avoid using a typedef here which is unnecessary. Add an 'env_' prefix to
both the enum and its members to make it clear that these are related to
the environment.

Add an ENV prefix to these two flags so that it is clear what they relate
to. Also move them to env.h since they are part of the public API. Use an
enum rather than a #define to tie them together.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 25e51e90 02-Aug-2019 Simon Glass <sjg@chromium.org>

env: Drop _ENTRY

This typedef does not need to be defined in the search.h header since it
is only used in one file (hashtable.c). Remove it from the header and
change it to a struct.

Signed-off-by: Simon Glass <sjg@chromium.org>

# dd2408ca 02-Aug-2019 Simon Glass <sjg@chromium.org>

env: Drop the ENTRY typedef

U-Boot is not supposed to use typedef for structs anymore. Also this name
is the same as the ENTRY() macro used in assembler files, and 'entry'
itself is widely used in U-Boot (>8k matches).

Drop the typedef and rename the struct to env_entry to reduce confusion.

Signed-off-by: Simon Glass <sjg@chromium.org>
Acked-by: Joe Hershberger <joe.hershberger@ni.com>

# 9dfdbd9f 30-Jan-2019 Roman Kapl <rka@sysgo.com>

hashtable: fix environment variable corruption

Only first previously deleted entry was recognized, leading hsearch_r
to think that there was no previously deleted entry. It then conluded
that a free entry was found, even if there were no free entries and it
overwrote a random entry.

This patch makes sure all deleted or free entries are always found and
also introduces constants for the 0 and -1 numbers. Unit tests to excersise a
simple hash table usage and catch the corruption were added.

To trash your environment, simply run this loop:

setenv i 0
while true; do
setenv v_$i $i
setenv v_$i
setexpr i $i + 1
done

Signed-off-by: Roman Kapl <rka@sysgo.com>

# 320194ae 23-Jan-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

hashtable: remove caps buffer

slre_match() checks if caps == NULL. In this case it does not try to
update it. So there is no need to create a buffer caps which we do not
evaluate.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 4bca3249 14-Dec-2018 AKASHI Takahiro <takahiro.akashi@linaro.org>

hashtable: fix length calculation in hexport_r

The commit below incorrectly fixed hexport_r();
> size = totlen + 1;
One extra byte is necessary to NULL-terminate a whole buffer, "resp."

Fixes: f1b20acb4a03 ("hashtable: Fix length calculation in hexport_r")
Signed-off-by: AKASHI Takahiro <takahiro.akashi@linaro.org>

# f1b20acb 17-Jul-2018 Zubair Lutfullah Kakakhel <zubair@resin.io>

hashtable: Fix length calculation in hexport_r

The length returned by hexport_r has a few redundant characters.
This appears as NULL characters at the end so seems harmless.

Remove the surplus counts in two places

totlen += strlen(ep->key) + 2;
I'm guessing the +2 here is for = and sep char. But there is another
totlen += 2; line that does that.

size = totletn + 1;
Doesn't make sense and isn't justified with any comment.

Signed-off-by: Zubair Lutfullah Kakakhel <zubair@resin.io>

# d9fc9077 09-Jul-2018 Quentin Schulz <quentin.schulz@bootlin.com>

hashtable: do not recreate whole hash table if vars are passed to himport_r

When vars are passed to the himport_r function with H_NOCLEAR flag,
those vars will be overridden in the current environment and if one of
those vars is not in the imported environment, it'll be deleted in the
current environment whatever the flag passed to himport_r.

The H_NOCLEAR flag is used to clear the whole environment whether vars
are passed to the function or not.

This leads to incoherent behaviour. If one passes vars to himport_r
with the H_NOCLEAR flag, if a var in vars is not in the imported env,
that var will be removed from the current env.

If one passes vars to himport_r without the H_NOCLEAR flag, the whole
environment will be removed and vars will be imported from the
environment in RAM.

It makes more sense to keep the variable that is in the current
environment but not in the imported environment if the H_NOCLEAR flag is
set and remove only that variable if the H_NOCLEAR flag is not set.

Let's clear the whole environment only if H_NOCLEAR and vars are not
passed to himport_r.

Let's remove variables that are in the current environment but not in
the imported env only if the H_NOCLEAR flag is not passed.

Suggested-by: Wolfgang Denk <wd@denx.de>
Signed-off-by: Quentin Schulz <quentin.schulz@bootlin.com>

# 83d290c5 06-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert all of our single license tags to Linux Kernel style

When U-Boot started using SPDX tags we were among the early adopters and
there weren't a lot of other examples to borrow from. So we picked the
area of the file that usually had a full license text and replaced it
with an appropriate SPDX-License-Identifier: entry. Since then, the
Linux Kernel has adopted SPDX tags and they place it as the very first
line in a file (except where shebangs are used, then it's second line)
and with slightly different comment styles than us.

In part due to community overlap, in part due to better tag visibility
and in part for other minor reasons, switch over to that style.

This commit changes all instances where we have a single declared
license in the tag as both the before and after are identical in tag
contents. There's also a few places where I found we did not have a tag
and have introduced one.

Signed-off-by: Tom Rini <trini@konsulko.com>

# d2d9bdfc 28-Sep-2016 B, Ravi <ravibabu@ti.com>

spl: saveenv: adding saveenv support in SPL

By default saveenv option is not supported for SPL. This patch
enable the support for save environment variable for SPL build.

Enable save environment support in SPL after setenv. By default
the saveenv option is not provided in SPL, but some boards need
this support in 'Falcon' boot, where SPL need to boot from
different images based on environment variable set by OS. For
example OS may set "reboot_image" environment variable to
"recovery" inorder to boot recovery image by SPL. The SPL read
"reboot_image" and act accordingly and change the reboot_image
to default mode using setenv and save the environemnt.

Signed-off-by: Ravi Babu <ravibabu@ti.com>
Reviewed-by: Simon Glass <sig@chromium.org>

change in v1:
- dropped SUPPORT, use CONFIG_SPL_SAVEENV
- updates the comments in mmc_private.h

# fc0b5948 07-Sep-2016 Robert P. J. Day <rpjday@crashcourse.ca>

Various, accumulated typos collected from around the tree.

Fix various misspellings of:

* deprecated
* partition
* preceding,preceded
* preparation
* its versus it's
* export
* existing
* scenario
* redundant
* remaining
* value
* architecture

Signed-off-by: Robert P. J. Day <rpjday@crashcourse.ca>
Reviewed-by: Jagan Teki <jteki@openedev.com>
Reviewed-by: Stefan Roese <sr@denx.de>

# c55d02b2 22-Jul-2016 Simon Glass <sjg@chromium.org>

hashtable: Fix compiler warning on 32-bit sandbox

This fixes a mismatch between the %zu format and the type used on sandbox.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@konsulko.com>

# 62a3b7dd 15-Jul-2016 Robert P. J. Day <rpjday@crashcourse.ca>

Various, unrelated tree-wide typo fixes.

Fix a number of typos, including:

* "compatble" -> "compatible"
* "eanbeld" -> "enabled"
* "envrionment" -> "environment"
* "FTD" -> "FDT" (for "flattened device tree")
* "ommitted" -> "omitted"
* "overriden" -> "overridden"
* "partiton" -> "partition"
* "propogate" -> "propagate"
* "resourse" -> "resource"
* "rest in piece" -> "rest in peace"
* "suport" -> "support"
* "varible" -> "variable"

Signed-off-by: Robert P. J. Day <rpjday@crashcourse.ca>

# 0226d878 13-Sep-2015 Lukasz Majewski <l.majewski@majess.pl>

env: import: hashtable: Free memory allocated before exiting from himport_r()

ithout this patch memory is not released on early exit.

Signed-off-by: Lukasz Majewski <l.majewski@majess.pl>

# 817e48d8 13-Sep-2015 Lukasz Majewski <l.majewski@majess.pl>

env: import: hashtable: Prevent buffer overrun when importing environment from file

Lets consider following scenario:
- One uses echo -n "key=value" to define environment variable in a file (single variable)
- The file content is "key=value" without any terminating byte (e.g. 0x0a or
0x0d).
- The file is loaded to u-boot non zero'ed RAM buffer (with load command).
- Then "env import -t -r $loadaddr $filesize" is executed.
- Due to lack of proper termination byte we have classical example of buffer
overrun.

This patch prevents from this by allocating one extra byte than size and
explicitly null terminate it.

There should be no change for normal env import operation after applying
this patch.

Signed-off-by: Lukasz Majewski <l.majewski@majess.pl>

# ecd1446f 14-Jul-2014 Alexander Holler <holler@ahsoftware.de>

Add option -r to env import to allow import of text files with CRLF as line endings

When this option is enabled, CRLF is treated like LF when importing environments
from text files, which means CRs ('\r') in front of LFs ('\n') are just ignored.

Drawback of enabling this option is that (maybe exported) variables which have
a trailing CR in their content will get imported without that CR. But this
drawback is very unlikely and the big advantage of letting Windows user create
a *working* uEnv.txt too is likely more welcome.

Signed-off-by: Alexander Holler <holler@ahsoftware.de>

# 9a832331 08-Oct-2013 Pierre Aubert <p.aubert@staubli.com>

env: fix the env export varname

The env export command doesn't export the first variable of the list
since commit 5a31ea04c9ee5544fbb70ad7597ea4b294840eab
"env grep" - reimplement command using hexport_r()

Signed-off-by: Pierre Aubert <p.aubert@staubli.com>

# 1bce2aeb 16-Sep-2013 Robert P. J. Day <rpjday@crashcourse.ca>

Cosmetic: Fix a number of typos, no functional changes.

Fix various misspellings of things like "environment", "kernel",
"default" and "volatile", and throw in a couple grammar fixes.

Signed-off-by: Robert P. J. Day <rpjday@crashcourse.ca>

# eee479cf 08-Jul-2013 Wolfgang Denk <wd@denx.de>

Add LGPL-2.1+ SPDX-License-Identifier to source files

Signed-off-by: Wolfgang Denk <wd@denx.de>

# e4fdcadd 28-Apr-2013 Lucian Cojocar <cojocar@gmail.com>

env: throw an error when an empty key is used

If the environment contains an entry like "=value" "\0" we should throw
an error when parsing the environment. Otherwise, U-Boot will enter in
an infinite loop.

Signed-off-by: Lucian Cojocar <cojocar@gmail.com>

# be29df6a 23-Mar-2013 Wolfgang Denk <wd@denx.de>

"env grep" - add support for regular expression matches

When CONFIG_REGEX is enabled, the new option "-e" becomes available
which causes regular expression matches to be used. This allows for
example things like these:

- print all MAC addresses:

=> env grep -e eth.*addr
eth1addr=00:10:ec:80:c5:15
ethaddr=00:10:ec:00:c5:15

- print all variables that have at least 2 colons in their value:

=> env grep -v -e :.*:
addip=setenv bootargs ${bootargs} ip=${ipaddr}:${serverip}:${gatewayip}:${netmask}:${hostname}:${netdev}:off
panic=1
eth1addr=00:10:ec:80:c5:15
ethaddr=00:10:ec:00:c5:15
ver=U-Boot 2013.04-rc1-00289-g497746b-dirty (Mar 22 2013 - 12:50:25)

etc.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# 5a31ea04 23-Mar-2013 Wolfgang Denk <wd@denx.de>

"env grep" - reimplement command using hexport_r()

Also drop hstrstr_r() which is not needed any more.
The new code is way more flexible.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# ea009d47 23-Mar-2013 Wolfgang Denk <wd@denx.de>

hashtable: preparations to use hexport_r() for "env grep"

The output of "env grep" is unsorted, and printing is done by a
private implementation to parse the hash table. We have all the
needed code in place in hexport_r() alsready, so let's use this
instead. Here we prepare the code for this, without any functional
changes yet.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# 2598090b 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Add environment variable flags

Currently just validates variable types as decimal, hexidecimal,
boolean, ip address, and mac address.

If the entry is not found in the env ".flags", then look in the static
one. This allows the env to override the static definitions, but prevents
the need to have every definition in the environment distracting you.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# 170ab110 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Add support for callbacks to environment vars

Add support for per-variable callbacks to the "hashtable" functions.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

!!!fix comment in callback

# be11235a 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Hide '.' variables in env print by default

When printing all variables with env print, don't print variables that
begin with '.'. If env print is called with a '-a' switch, then
include variables that begin with '.' (just like the ls command).

Variables printed explicitly will be printed even without the -a.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# 7afcf3a5 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Refactor apply into change_ok

Move the read of the old value to inside the check function. In some
cases it can be avoided all together and at the least the code is only
called from one place.

Also name the function and the callback to more clearly describe what
it does.

Pass the ENTRY instead of just the name for direct access to the whole
data structure.

Pass an enum to the callback that specifies the operation being approved.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# 3d3b52f2 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Consolidate common code in hsearch_r()

The same chunk of code was replicated in two places and the following
changes will make that chunk grow a bit, so combine into a static func.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# c4e0057f 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Refactor do_apply to a flag

Use a flag in hsearch_r for insert mode passed from import to allow the
behavior be different based on use.

Now that "do_check" is called for all imports, ensure console init is
complete before updating the console on relocation import

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# 7ac2fe2d 17-Sep-2012 Ilya Yanok <ilya.yanok@cogentembedded.com>

OMAP: networking support for SPL

This patch adds support for networking in SPL. Some devices are
capable of loading SPL via network so it makes sense to load the
main U-Boot binary via network too. This patch tries to use
existing network code as much as possible. Unfortunately, it depends
on environment which in turn depends on other code so SPL size
is increased significantly. No effort was done to decouple network
code and environment so far.

Signed-off-by: Ilya Yanok <ilya.yanok@cogentembedded.com>
Acked-by: Joe Hershberger <joe.hershberger@ni.com>
Signed-off-by: Tom Rini <trini@ti.com>

# d5370feb 26-Aug-2012 Gerlando Falauto <gerlando.falauto@keymile.com>

env: delete selected vars not present in imported env

When variables explicitly specified on the command line are not present
in the imported env, delete them from the running env.
If the variable is also missing from the running env, issue a warning.

Signed-off-by: Gerlando Falauto <gerlando.falauto@keymile.com>
Reviewed-by: Marek Vasut <marex@denx.de>

# 152874b6 23-Aug-2012 Gerlando Falauto <gerlando.falauto@keymile.com>

env: check and apply changes on delete/destroy

Signed-off-by: Gerlando Falauto <gerlando.falauto@keymile.com>
Reviewed-by: Marek Vasut <marex@denx.de>

# c5983592 23-Aug-2012 Gerlando Falauto <gerlando.falauto@keymile.com>

env: add check/apply logic to himport_r()

Change hashtable so that a callback function will decide whether a
variable can be overwritten, and possibly apply the changes.

So add a new field to struct hsearch_data:

o "apply" callback function to check whether a variable can be
overwritten, and possibly immediately apply the changes;
when NULL, no check is performed.

And a new argument to himport_r():
o "do_apply": whether to call the apply callback function

NOTE: This patch does not change the current behavior.

Signed-off-by: Gerlando Falauto <gerlando.falauto@keymile.com>
Reviewed-by: Marek Vasut <marex@denx.de>

# 348b1f1c 23-Aug-2012 Gerlando Falauto <gerlando.falauto@keymile.com>

env: make himport_r() selective on variables

Add 2 new arguments to himport_r():

o "nvars", "vars": number and list of variables to take into account
(0 means ALL)

NOTE: This patch does not change the current behaviour.

Signed-off-by: Gerlando Falauto <gerlando.falauto@keymile.com>
Reviewed-by: Marek Vasut <marex@denx.de>

# ff856286 21-Nov-2011 Simon Glass <sjg@chromium.org>

sandbox: Fix warnings in hashtable.c

This fixes a few printf() strings for size_t which are missing the 'z'
modifier.

Signed-off-by: Simon Glass <sjg@chromium.org>
Acked-by: Mike Frysinger <vapier@gentoo.org>

# 37f2fe74 06-Nov-2011 Wolfgang Denk <wd@denx.de>

env: allow to export only selected variables

New syntax:
env export [-t | -b | -c] [-s size] addr [var ...]

With this change it is possible to provide a list of variables names
that shall be exported. Whenno arguments are given, the whole
environment gets exported.

NOTE: The new handling of the "size" argument means a change to the
user API.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# 4d91a6ec 23-Aug-2011 Jason Hobbs <jason.hobbs@calxeda.com>

Replace space and tab checks with isblank

These are various places I found that checked for conditions equivalent
to isblank.

Signed-off-by: Jason Hobbs <jason.hobbs@calxeda.com>

# 84b5e802 29-Jul-2011 Wolfgang Denk <wd@denx.de>

Constify getenv(), setenv() and hash code functions

This is needed to get rid of build warnings like

main.c:311: warning: passing argument 2 of 'setenv' discards qualifiers from pointer target type

which result from commit 09c2e90 "unify version_string".

Signed-off-by: Wolfgang Denk <wd@denx.de>
Cc: Andreas Bie�mann <andreas.devel@googlemail.com>

# a000b795 05-Apr-2011 Kim Phillips <kim.phillips@freescale.com>

common: add a grepenv command

u-boot environments, esp. when boards are shared across multiple
users, can get pretty large and time consuming to visually parse.
The grepenv command this patch adds can be used in lieu of printenv
to facilitate searching. grepenv works like printenv but limits
its output only to environment strings (variable name and value
pairs) that match the user specified substring.

the following examples are on a board with a 5313 byte environment
that spans multiple screen pages:

Example 1: summarize ethernet configuration:

=> grepenv eth TSEC
etact=FM1@DTSEC2
eth=FM1@DTSEC4
ethact=FM1@DTSEC2
eth1addr=00:E0:0C:00:8b:01
eth2addr=00:E0:0C:00:8b:02
eth3addr=00:E0:0C:00:8b:03
eth4addr=00:E0:0C:00:8b:04
eth5addr=00:E0:0C:00:8b:05
eth6addr=00:E0:0C:00:8b:06
eth7addr=00:E0:0C:00:8b:07
eth8addr=00:E0:0C:00:8b:08
eth9addr=00:E0:0C:00:8b:09
ethaddr=00:E0:0C:00:8b:00
netdev=eth0
uprcw=setenv ethact $eth;setenv filename p4080ds/R_PPSXX_0xe/rcw_0xe_2sgmii_rev2_high.bin;setenv start 0xe8000000;protect off all;run upimage;protect on all
upuboot=setenv ethact $eth;setenv filename u-boot.bin;setenv start eff80000;protect off all;run upimage;protect on all
upucode=setenv ethact $eth;setenv filename fsl_fman_ucode_P4080_101_6.bin;setenv start 0xef000000;protect off all;run upimage;protect on all
usdboot=setenv ethact $eth;tftp 1000000 $dir/$bootfile;tftp 2000000 $dir/initramfs.cpio.gz.uboot;tftp c00000 $dir/p4080ds-usdpaa.dtb;setenv bootargs root=/dev/ram rw console=ttyS0,115200 $othbootargs;bootm 1000000 2000000 c00000;
=>

Example 2: detect unused env vars:

=> grepenv etact
etact=FM1@DTSEC2
=>

Example 3: reveal hardcoded variables; e.g., for fdtaddr:

=> grepenv fdtaddr
fdtaddr=c00000
nfsboot=setenv bootargs root=/dev/nfs rw nfsroot=$serverip:$rootpath ip=$ipaddr:$serverip:$gatewayip:$netmask:$hostname:$netdev:off console=$consoledev,$baudrate $othbootargs;tftp $loadaddr $bootfile;tftp $fdtaddr $fdtfile;bootm $loadaddr - $fdtaddr
ramboot=setenv bootargs root=/dev/ram rw console=$consoledev,$baudrate $othbootargs;tftp $ramdiskaddr $ramdiskfile;tftp $loadaddr $bootfile;tftp $fdtaddr $fdtfile;bootm $loadaddr $ramdiskaddr $fdtaddr
=> grep $fdtaddr
fdtaddr=c00000
my_boot=bootm 0x40000000 0x41000000 0x00c00000
my_dtb=tftp 0x00c00000 $prefix/p4080ds.dtb
nohvboot=tftp 1000000 $dir/$bootfile;tftp 2000000 $dir/$ramdiskfile;tftp c00000 $dir/$fdtfile;setenv bootargs root=/dev/ram rw ramdisk_size=0x10000000 console=ttyS0,115200;bootm 1000000 2000000 c00000;
=>

This patch also enables the grepenv command by default on
corenet_ds based boards (and repositions the DHCP command
entry to keep the list sorted).

Signed-off-by: Kim Phillips <kim.phillips@freescale.com>
Cc: Kumar Gala <kumar.gala@freescale.com>
Cc: Andy Fleming <afleming@freescale.com>

# af4d9074 04-Apr-2011 Kim Phillips <kim.phillips@freescale.com>

env: fix env var autocompletion

commit 560d424b6d7cd4205b062ad95f1b104bd4f8bcc3 "env: re-add
support for auto-completion" fell short of its description -
the 'used' logic in hmatch_r was reversed - 'used' is 0 if
the hash table entry is not used, or -1 if deleted. This
patch makes hmatch_r actually match on valid ('used') entries,
instead of skipping them and failing to match anything.

typing 'printenv tft' and hitting 'tab' now displays valid
choices for variable names.

Signed-off-by: Kim Phillips <kim.phillips@freescale.com>
Cc: Mike Frysinger <vapier@gentoo.org>
Tested-by: Mike Frysinger <vapier@gentoo.org>

# c81c1222 21-Mar-2011 Peter Barada <peter.barada@logicpd.com>

Fix hash table deletion to prevent lost entries

Use negative used value to mark deleted entry. Search keeps probing
past deleted entries. Adding an entry uses first deleted entry when
it hits end of probe chain.

Initially found that "ramdiskimage" and "preboot" collide modulus 347,
causing "preboot" to be inserted at idx 190, "ramdiskimage" at idx 191.
Previous to this fix when "preboot" is deleted, "ramdiskimage" is
orphaned.

Signed-off-by: Peter Barada <peter.barada@logicpd.com>
Tested-by: Wolfgang Denk <wd@denx.de>

# 560d424b 17-Dec-2010 Mike Frysinger <vapier@gentoo.org>

env: re-add support for auto-completion

Currently, only basic completion is supported (no globs), but this is
what we had previously.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 2eb1573f 08-Dec-2010 Mike Frysinger <vapier@gentoo.org>

hashtable: drop all non-reentrant versions

The non-reentrant versions of the hashtable functions operate on a single
shared hashtable. So if two different people try using these funcs for
two different purposes, they'll cause problems for the other.

Avoid this by converting all existing hashtable consumers over to the
reentrant versions and then punting the non-reentrant ones.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 071bc923 27-Oct-2010 Wolfgang Denk <wd@denx.de>

Coding Style cleanup

Signed-off-by: Wolfgang Denk <wd@denx.de>

# fc5fc76b 01-Oct-2010 Andreas Bießmann <andreas.devel@googlemail.com>

lib/hashtable.c: add CONFIG_ENV_MIN_ENTRIES

This patch adds a new config parameter for adjusting the calculation of
hash table size when importing a buffer.

When importing a extremely small buffer (e.g. the default_environment)
the old calculation generated a hash table which could hold at most the
buffer content but no more entires.

The new calculation add a fixed number of entries to the result to fit
better for small import buffers. This amount may be configured by the
user in board file to adjust the behaviour.

Signed-off-by: Andreas Bie�mann <andreas.devel@googlemail.com>

# ea882baf 20-Jun-2010 Wolfgang Denk <wd@denx.de>

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>

# a6826fbc 20-Jun-2010 Wolfgang Denk <wd@denx.de>

Add hash table support as base for new environment code

This implementation is based on code from uClibc-0.9.30.3 but was
modified and extended for use within U-Boot.

Major modifications and extensions:

* hsearch() [modified / extended]:
- While the standard version does not make any assumptions about
the type of the stored data objects at all, this implementation
works with NUL terminated strings only.
- Instead of storing just pointers to the original objects, we
create local copies so the caller does not need to care about the
data any more.
- The standard implementation does not provide a way to update an
existing entry. This version will create a new entry or update an
existing one when both "action == ENTER" and "item.data != NULL".
- hsearch_r(): Instead of returning 1 on success, we return the
index into the internal hash table, which is also guaranteed to be
positive. This allows us direct access to the found hash table
slot for example for functions like hdelete().
* hdelete() [added]:
- The standard implementation of hsearch(3) does not provide any way
to delete any entries from the hash table. We extend the code to
do that.
* hexport() [added]:
- Export the data stored in the hash table in linearized form:
Entries are exported as "name=value" strings, separated by an
arbitrary (non-NUL, of course) separator character. This allows to
use this function both when formatting the U-Boot environment for
external storage (using '\0' as separator), but also when using it
for the "printenv" command to print all variables, simply by using
as '\n" as separator. This can also be used for new features like
exporting the environment data as text file, including the option
for later re-import.
- The entries in the result list will be sorted by ascending key
values.
* himport() [added]:
- Import linearized data into hash table. This is the inverse
function to hexport(): it takes a linear list of "name=value"
pairs and creates hash table entries from it.
- Entries without "value", i. e. consisting of only "name" or
"name=", will cause this entry to be deleted from the hash table.
- The "flag" argument can be used to control the behaviour: when
the H_NOCLEAR bit is set, then an existing hash table will kept,
i. e. new data will be added to an existing hash table;
otherwise, old data will be discarded and a new hash table will
be created.
- The separator character for the "name=value" pairs can be
selected, so we both support importing from externally stored
environment data (separated by NUL characters) and from plain text
files (entries separated by newline characters).
- To allow for nicely formatted text input, leading white space
(sequences of SPACE and TAB chars) is ignored, and entries
starting (after removal of any leading white space) with a '#'
character are considered comments and ignored.
- NOTE: this means that a variable name cannot start with a '#'
character.
- When using a non-NUL separator character, backslash is used as
escape character in the value part, allowing for example fo
multi-line values.
- In theory, arbitrary separator characters can be used, but only
'\0' and '\n' have really been tested.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# d045cbac 07-Jul-2020 Marek Vasut <marex@denx.de>

env: Add support for explicit write access list

This option marks any U-Boot variable which does not have explicit 'w'
writeable flag set as read-only. This way the environment can be locked
down and only variables explicitly configured to be writeable can ever
be changed by either 'env import', 'env set' or loading user environment
from environment storage.

Signed-off-by: Marek Vasut <marex@denx.de>
Reviewed-by: Tom Rini <trini@konsulko.com>

# 47f3b1f2 07-Jul-2020 Marek Vasut <marex@denx.de>

env: Add option to only ever append environment

Add configuration option which prevents the environment hash table to be
ever cleared and reloaded with different content. This is useful in case
the first environment loaded into the hash table contains e.g. sensitive
content which must not be dropped or reloaded.

Signed-off-by: Marek Vasut <marex@denx.de>
Reviewed-by: Tom Rini <trini@konsulko.com>

# 60ffcf21 24-Jun-2020 Sean Anderson <seanga2@gmail.com>

lib: Always set errno in hcreate_r

This could give a confusing error message if it failed and didn't set
errno.

Signed-off-by: Sean Anderson <seanga2@gmail.com>
Reviewed-by: Bin Meng <bmeng.cn@gmail.com>

# f7ae49fc 10-May-2020 Simon Glass <sjg@chromium.org>

common: Drop log.h from common header

Move this header out of the common header.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 09140113 10-May-2020 Simon Glass <sjg@chromium.org>

command: Remove the cmd_tbl_t typedef

We should not use typedefs in U-Boot. They cannot be used as forward
declarations which means that header files must include the full header to
access them.

Drop the typedef and rename the struct to remove the _s suffix which is
now not useful.

This requires quite a few header-file additions.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 080019b8 27-Feb-2020 Rasmus Villemoes <rasmus.villemoes@prevas.dk>

make env_entry::callback conditional on !CONFIG_SPL_BUILD

The callback member of struct env_entry is always NULL for an SPL
build. Removing it thus saves a bit of run-time memory in the
SPL (when CONFIG_SPL_ENV_SUPPORT=y) since struct env_entry is embedded
in struct env_entry_node - i.e. about 2KB for the normal case of
512+change hash table entries.

Two small fixups are needed for this, all other references to the
callback member are already under !CONFIG_SPL_BUILD: Don't initialize
.callback in set_flags() - hsearch_r doesn't use that value
anyway. And make env_callback_init() initialize ->callback to NULL for
a new entry instead of relying on an unused or deleted entry having
NULL in ->callback.

Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 34284970 27-Feb-2020 Rasmus Villemoes <rasmus.villemoes@prevas.dk>

lib/hashtable.c: don't test ->callback in SPL

In SPL, environment callbacks are not supported, so e->callback is
always NULL. Removing this makes the SPL a little smaller (about 400
bytes in my ppc build) with no functional change.

Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 7f529f65 27-Feb-2020 Rasmus Villemoes <rasmus.villemoes@prevas.dk>

lib/hashtable.c: create helper for calling env_entry::callback

This is preparation for compiling out the "call the callback" code and
associated error handling for SPL, where ->callback is always NULL.

Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 8bef79bf 14-Nov-2019 Simon Glass <sjg@chromium.org>

common: Move sorting functions to their own header file

These don't need to be in common.h so move them out into a new header.
Also add some missing comments.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@konsulko.com>

# 3f0d6807 01-Aug-2019 Simon Glass <sjg@chromium.org>

env: Drop the ACTION typedef

Avoid using a typedef here which is unnecessary. Add an 'env_' prefix to
both the enum and its members to make it clear that these are related to
the environment.

Add an ENV prefix to these two flags so that it is clear what they relate
to. Also move them to env.h since they are part of the public API. Use an
enum rather than a #define to tie them together.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 25e51e90 02-Aug-2019 Simon Glass <sjg@chromium.org>

env: Drop _ENTRY

This typedef does not need to be defined in the search.h header since it
is only used in one file (hashtable.c). Remove it from the header and
change it to a struct.

Signed-off-by: Simon Glass <sjg@chromium.org>

# dd2408ca 02-Aug-2019 Simon Glass <sjg@chromium.org>

env: Drop the ENTRY typedef

U-Boot is not supposed to use typedef for structs anymore. Also this name
is the same as the ENTRY() macro used in assembler files, and 'entry'
itself is widely used in U-Boot (>8k matches).

Drop the typedef and rename the struct to env_entry to reduce confusion.

Signed-off-by: Simon Glass <sjg@chromium.org>
Acked-by: Joe Hershberger <joe.hershberger@ni.com>

# 9dfdbd9f 30-Jan-2019 Roman Kapl <rka@sysgo.com>

hashtable: fix environment variable corruption

Only first previously deleted entry was recognized, leading hsearch_r
to think that there was no previously deleted entry. It then conluded
that a free entry was found, even if there were no free entries and it
overwrote a random entry.

This patch makes sure all deleted or free entries are always found and
also introduces constants for the 0 and -1 numbers. Unit tests to excersise a
simple hash table usage and catch the corruption were added.

To trash your environment, simply run this loop:

setenv i 0
while true; do
setenv v_$i $i
setenv v_$i
setexpr i $i + 1
done

Signed-off-by: Roman Kapl <rka@sysgo.com>

# 320194ae 23-Jan-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

hashtable: remove caps buffer

slre_match() checks if caps == NULL. In this case it does not try to
update it. So there is no need to create a buffer caps which we do not
evaluate.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 4bca3249 14-Dec-2018 AKASHI Takahiro <takahiro.akashi@linaro.org>

hashtable: fix length calculation in hexport_r

The commit below incorrectly fixed hexport_r();
> size = totlen + 1;
One extra byte is necessary to NULL-terminate a whole buffer, "resp."

Fixes: f1b20acb4a03 ("hashtable: Fix length calculation in hexport_r")
Signed-off-by: AKASHI Takahiro <takahiro.akashi@linaro.org>

# f1b20acb 17-Jul-2018 Zubair Lutfullah Kakakhel <zubair@resin.io>

hashtable: Fix length calculation in hexport_r

The length returned by hexport_r has a few redundant characters.
This appears as NULL characters at the end so seems harmless.

Remove the surplus counts in two places

totlen += strlen(ep->key) + 2;
I'm guessing the +2 here is for = and sep char. But there is another
totlen += 2; line that does that.

size = totletn + 1;
Doesn't make sense and isn't justified with any comment.

Signed-off-by: Zubair Lutfullah Kakakhel <zubair@resin.io>

# d9fc9077 09-Jul-2018 Quentin Schulz <quentin.schulz@bootlin.com>

hashtable: do not recreate whole hash table if vars are passed to himport_r

When vars are passed to the himport_r function with H_NOCLEAR flag,
those vars will be overridden in the current environment and if one of
those vars is not in the imported environment, it'll be deleted in the
current environment whatever the flag passed to himport_r.

The H_NOCLEAR flag is used to clear the whole environment whether vars
are passed to the function or not.

This leads to incoherent behaviour. If one passes vars to himport_r
with the H_NOCLEAR flag, if a var in vars is not in the imported env,
that var will be removed from the current env.

If one passes vars to himport_r without the H_NOCLEAR flag, the whole
environment will be removed and vars will be imported from the
environment in RAM.

It makes more sense to keep the variable that is in the current
environment but not in the imported environment if the H_NOCLEAR flag is
set and remove only that variable if the H_NOCLEAR flag is not set.

Let's clear the whole environment only if H_NOCLEAR and vars are not
passed to himport_r.

Let's remove variables that are in the current environment but not in
the imported env only if the H_NOCLEAR flag is not passed.

Suggested-by: Wolfgang Denk <wd@denx.de>
Signed-off-by: Quentin Schulz <quentin.schulz@bootlin.com>

# 83d290c5 06-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert all of our single license tags to Linux Kernel style

When U-Boot started using SPDX tags we were among the early adopters and
there weren't a lot of other examples to borrow from. So we picked the
area of the file that usually had a full license text and replaced it
with an appropriate SPDX-License-Identifier: entry. Since then, the
Linux Kernel has adopted SPDX tags and they place it as the very first
line in a file (except where shebangs are used, then it's second line)
and with slightly different comment styles than us.

In part due to community overlap, in part due to better tag visibility
and in part for other minor reasons, switch over to that style.

This commit changes all instances where we have a single declared
license in the tag as both the before and after are identical in tag
contents. There's also a few places where I found we did not have a tag
and have introduced one.

Signed-off-by: Tom Rini <trini@konsulko.com>

# d2d9bdfc 28-Sep-2016 B, Ravi <ravibabu@ti.com>

spl: saveenv: adding saveenv support in SPL

By default saveenv option is not supported for SPL. This patch
enable the support for save environment variable for SPL build.

Enable save environment support in SPL after setenv. By default
the saveenv option is not provided in SPL, but some boards need
this support in 'Falcon' boot, where SPL need to boot from
different images based on environment variable set by OS. For
example OS may set "reboot_image" environment variable to
"recovery" inorder to boot recovery image by SPL. The SPL read
"reboot_image" and act accordingly and change the reboot_image
to default mode using setenv and save the environemnt.

Signed-off-by: Ravi Babu <ravibabu@ti.com>
Reviewed-by: Simon Glass <sig@chromium.org>

change in v1:
- dropped SUPPORT, use CONFIG_SPL_SAVEENV
- updates the comments in mmc_private.h

# fc0b5948 07-Sep-2016 Robert P. J. Day <rpjday@crashcourse.ca>

Various, accumulated typos collected from around the tree.

Fix various misspellings of:

* deprecated
* partition
* preceding,preceded
* preparation
* its versus it's
* export
* existing
* scenario
* redundant
* remaining
* value
* architecture

Signed-off-by: Robert P. J. Day <rpjday@crashcourse.ca>
Reviewed-by: Jagan Teki <jteki@openedev.com>
Reviewed-by: Stefan Roese <sr@denx.de>

# c55d02b2 22-Jul-2016 Simon Glass <sjg@chromium.org>

hashtable: Fix compiler warning on 32-bit sandbox

This fixes a mismatch between the %zu format and the type used on sandbox.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@konsulko.com>

# 62a3b7dd 15-Jul-2016 Robert P. J. Day <rpjday@crashcourse.ca>

Various, unrelated tree-wide typo fixes.

Fix a number of typos, including:

* "compatble" -> "compatible"
* "eanbeld" -> "enabled"
* "envrionment" -> "environment"
* "FTD" -> "FDT" (for "flattened device tree")
* "ommitted" -> "omitted"
* "overriden" -> "overridden"
* "partiton" -> "partition"
* "propogate" -> "propagate"
* "resourse" -> "resource"
* "rest in piece" -> "rest in peace"
* "suport" -> "support"
* "varible" -> "variable"

Signed-off-by: Robert P. J. Day <rpjday@crashcourse.ca>

# 0226d878 13-Sep-2015 Lukasz Majewski <l.majewski@majess.pl>

env: import: hashtable: Free memory allocated before exiting from himport_r()

ithout this patch memory is not released on early exit.

Signed-off-by: Lukasz Majewski <l.majewski@majess.pl>

# 817e48d8 13-Sep-2015 Lukasz Majewski <l.majewski@majess.pl>

env: import: hashtable: Prevent buffer overrun when importing environment from file

Lets consider following scenario:
- One uses echo -n "key=value" to define environment variable in a file (single variable)
- The file content is "key=value" without any terminating byte (e.g. 0x0a or
0x0d).
- The file is loaded to u-boot non zero'ed RAM buffer (with load command).
- Then "env import -t -r $loadaddr $filesize" is executed.
- Due to lack of proper termination byte we have classical example of buffer
overrun.

This patch prevents from this by allocating one extra byte than size and
explicitly null terminate it.

There should be no change for normal env import operation after applying
this patch.

Signed-off-by: Lukasz Majewski <l.majewski@majess.pl>

# ecd1446f 14-Jul-2014 Alexander Holler <holler@ahsoftware.de>

Add option -r to env import to allow import of text files with CRLF as line endings

When this option is enabled, CRLF is treated like LF when importing environments
from text files, which means CRs ('\r') in front of LFs ('\n') are just ignored.

Drawback of enabling this option is that (maybe exported) variables which have
a trailing CR in their content will get imported without that CR. But this
drawback is very unlikely and the big advantage of letting Windows user create
a *working* uEnv.txt too is likely more welcome.

Signed-off-by: Alexander Holler <holler@ahsoftware.de>

# 9a832331 08-Oct-2013 Pierre Aubert <p.aubert@staubli.com>

env: fix the env export varname

The env export command doesn't export the first variable of the list
since commit 5a31ea04c9ee5544fbb70ad7597ea4b294840eab
"env grep" - reimplement command using hexport_r()

Signed-off-by: Pierre Aubert <p.aubert@staubli.com>

# 1bce2aeb 16-Sep-2013 Robert P. J. Day <rpjday@crashcourse.ca>

Cosmetic: Fix a number of typos, no functional changes.

Fix various misspellings of things like "environment", "kernel",
"default" and "volatile", and throw in a couple grammar fixes.

Signed-off-by: Robert P. J. Day <rpjday@crashcourse.ca>

# eee479cf 08-Jul-2013 Wolfgang Denk <wd@denx.de>

Add LGPL-2.1+ SPDX-License-Identifier to source files

Signed-off-by: Wolfgang Denk <wd@denx.de>

# e4fdcadd 28-Apr-2013 Lucian Cojocar <cojocar@gmail.com>

env: throw an error when an empty key is used

If the environment contains an entry like "=value" "\0" we should throw
an error when parsing the environment. Otherwise, U-Boot will enter in
an infinite loop.

Signed-off-by: Lucian Cojocar <cojocar@gmail.com>

# be29df6a 23-Mar-2013 Wolfgang Denk <wd@denx.de>

"env grep" - add support for regular expression matches

When CONFIG_REGEX is enabled, the new option "-e" becomes available
which causes regular expression matches to be used. This allows for
example things like these:

- print all MAC addresses:

=> env grep -e eth.*addr
eth1addr=00:10:ec:80:c5:15
ethaddr=00:10:ec:00:c5:15

- print all variables that have at least 2 colons in their value:

=> env grep -v -e :.*:
addip=setenv bootargs ${bootargs} ip=${ipaddr}:${serverip}:${gatewayip}:${netmask}:${hostname}:${netdev}:off
panic=1
eth1addr=00:10:ec:80:c5:15
ethaddr=00:10:ec:00:c5:15
ver=U-Boot 2013.04-rc1-00289-g497746b-dirty (Mar 22 2013 - 12:50:25)

etc.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# 5a31ea04 23-Mar-2013 Wolfgang Denk <wd@denx.de>

"env grep" - reimplement command using hexport_r()

Also drop hstrstr_r() which is not needed any more.
The new code is way more flexible.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# ea009d47 23-Mar-2013 Wolfgang Denk <wd@denx.de>

hashtable: preparations to use hexport_r() for "env grep"

The output of "env grep" is unsorted, and printing is done by a
private implementation to parse the hash table. We have all the
needed code in place in hexport_r() alsready, so let's use this
instead. Here we prepare the code for this, without any functional
changes yet.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# 2598090b 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Add environment variable flags

Currently just validates variable types as decimal, hexidecimal,
boolean, ip address, and mac address.

If the entry is not found in the env ".flags", then look in the static
one. This allows the env to override the static definitions, but prevents
the need to have every definition in the environment distracting you.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# 170ab110 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Add support for callbacks to environment vars

Add support for per-variable callbacks to the "hashtable" functions.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

!!!fix comment in callback

# be11235a 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Hide '.' variables in env print by default

When printing all variables with env print, don't print variables that
begin with '.'. If env print is called with a '-a' switch, then
include variables that begin with '.' (just like the ls command).

Variables printed explicitly will be printed even without the -a.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# 7afcf3a5 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Refactor apply into change_ok

Move the read of the old value to inside the check function. In some
cases it can be avoided all together and at the least the code is only
called from one place.

Also name the function and the callback to more clearly describe what
it does.

Pass the ENTRY instead of just the name for direct access to the whole
data structure.

Pass an enum to the callback that specifies the operation being approved.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# 3d3b52f2 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Consolidate common code in hsearch_r()

The same chunk of code was replicated in two places and the following
changes will make that chunk grow a bit, so combine into a static func.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# c4e0057f 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Refactor do_apply to a flag

Use a flag in hsearch_r for insert mode passed from import to allow the
behavior be different based on use.

Now that "do_check" is called for all imports, ensure console init is
complete before updating the console on relocation import

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# 7ac2fe2d 17-Sep-2012 Ilya Yanok <ilya.yanok@cogentembedded.com>

OMAP: networking support for SPL

This patch adds support for networking in SPL. Some devices are
capable of loading SPL via network so it makes sense to load the
main U-Boot binary via network too. This patch tries to use
existing network code as much as possible. Unfortunately, it depends
on environment which in turn depends on other code so SPL size
is increased significantly. No effort was done to decouple network
code and environment so far.

Signed-off-by: Ilya Yanok <ilya.yanok@cogentembedded.com>
Acked-by: Joe Hershberger <joe.hershberger@ni.com>
Signed-off-by: Tom Rini <trini@ti.com>

# d5370feb 26-Aug-2012 Gerlando Falauto <gerlando.falauto@keymile.com>

env: delete selected vars not present in imported env

When variables explicitly specified on the command line are not present
in the imported env, delete them from the running env.
If the variable is also missing from the running env, issue a warning.

Signed-off-by: Gerlando Falauto <gerlando.falauto@keymile.com>
Reviewed-by: Marek Vasut <marex@denx.de>

# 152874b6 23-Aug-2012 Gerlando Falauto <gerlando.falauto@keymile.com>

env: check and apply changes on delete/destroy

Signed-off-by: Gerlando Falauto <gerlando.falauto@keymile.com>
Reviewed-by: Marek Vasut <marex@denx.de>

# c5983592 23-Aug-2012 Gerlando Falauto <gerlando.falauto@keymile.com>

env: add check/apply logic to himport_r()

Change hashtable so that a callback function will decide whether a
variable can be overwritten, and possibly apply the changes.

So add a new field to struct hsearch_data:

o "apply" callback function to check whether a variable can be
overwritten, and possibly immediately apply the changes;
when NULL, no check is performed.

And a new argument to himport_r():
o "do_apply": whether to call the apply callback function

NOTE: This patch does not change the current behavior.

Signed-off-by: Gerlando Falauto <gerlando.falauto@keymile.com>
Reviewed-by: Marek Vasut <marex@denx.de>

# 348b1f1c 23-Aug-2012 Gerlando Falauto <gerlando.falauto@keymile.com>

env: make himport_r() selective on variables

Add 2 new arguments to himport_r():

o "nvars", "vars": number and list of variables to take into account
(0 means ALL)

NOTE: This patch does not change the current behaviour.

Signed-off-by: Gerlando Falauto <gerlando.falauto@keymile.com>
Reviewed-by: Marek Vasut <marex@denx.de>

# ff856286 21-Nov-2011 Simon Glass <sjg@chromium.org>

sandbox: Fix warnings in hashtable.c

This fixes a few printf() strings for size_t which are missing the 'z'
modifier.

Signed-off-by: Simon Glass <sjg@chromium.org>
Acked-by: Mike Frysinger <vapier@gentoo.org>

# 37f2fe74 06-Nov-2011 Wolfgang Denk <wd@denx.de>

env: allow to export only selected variables

New syntax:
env export [-t | -b | -c] [-s size] addr [var ...]

With this change it is possible to provide a list of variables names
that shall be exported. Whenno arguments are given, the whole
environment gets exported.

NOTE: The new handling of the "size" argument means a change to the
user API.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# 4d91a6ec 23-Aug-2011 Jason Hobbs <jason.hobbs@calxeda.com>

Replace space and tab checks with isblank

These are various places I found that checked for conditions equivalent
to isblank.

Signed-off-by: Jason Hobbs <jason.hobbs@calxeda.com>

# 84b5e802 29-Jul-2011 Wolfgang Denk <wd@denx.de>

Constify getenv(), setenv() and hash code functions

This is needed to get rid of build warnings like

main.c:311: warning: passing argument 2 of 'setenv' discards qualifiers from pointer target type

which result from commit 09c2e90 "unify version_string".

Signed-off-by: Wolfgang Denk <wd@denx.de>
Cc: Andreas Bie�mann <andreas.devel@googlemail.com>

# a000b795 05-Apr-2011 Kim Phillips <kim.phillips@freescale.com>

common: add a grepenv command

u-boot environments, esp. when boards are shared across multiple
users, can get pretty large and time consuming to visually parse.
The grepenv command this patch adds can be used in lieu of printenv
to facilitate searching. grepenv works like printenv but limits
its output only to environment strings (variable name and value
pairs) that match the user specified substring.

the following examples are on a board with a 5313 byte environment
that spans multiple screen pages:

Example 1: summarize ethernet configuration:

=> grepenv eth TSEC
etact=FM1@DTSEC2
eth=FM1@DTSEC4
ethact=FM1@DTSEC2
eth1addr=00:E0:0C:00:8b:01
eth2addr=00:E0:0C:00:8b:02
eth3addr=00:E0:0C:00:8b:03
eth4addr=00:E0:0C:00:8b:04
eth5addr=00:E0:0C:00:8b:05
eth6addr=00:E0:0C:00:8b:06
eth7addr=00:E0:0C:00:8b:07
eth8addr=00:E0:0C:00:8b:08
eth9addr=00:E0:0C:00:8b:09
ethaddr=00:E0:0C:00:8b:00
netdev=eth0
uprcw=setenv ethact $eth;setenv filename p4080ds/R_PPSXX_0xe/rcw_0xe_2sgmii_rev2_high.bin;setenv start 0xe8000000;protect off all;run upimage;protect on all
upuboot=setenv ethact $eth;setenv filename u-boot.bin;setenv start eff80000;protect off all;run upimage;protect on all
upucode=setenv ethact $eth;setenv filename fsl_fman_ucode_P4080_101_6.bin;setenv start 0xef000000;protect off all;run upimage;protect on all
usdboot=setenv ethact $eth;tftp 1000000 $dir/$bootfile;tftp 2000000 $dir/initramfs.cpio.gz.uboot;tftp c00000 $dir/p4080ds-usdpaa.dtb;setenv bootargs root=/dev/ram rw console=ttyS0,115200 $othbootargs;bootm 1000000 2000000 c00000;
=>

Example 2: detect unused env vars:

=> grepenv etact
etact=FM1@DTSEC2
=>

Example 3: reveal hardcoded variables; e.g., for fdtaddr:

=> grepenv fdtaddr
fdtaddr=c00000
nfsboot=setenv bootargs root=/dev/nfs rw nfsroot=$serverip:$rootpath ip=$ipaddr:$serverip:$gatewayip:$netmask:$hostname:$netdev:off console=$consoledev,$baudrate $othbootargs;tftp $loadaddr $bootfile;tftp $fdtaddr $fdtfile;bootm $loadaddr - $fdtaddr
ramboot=setenv bootargs root=/dev/ram rw console=$consoledev,$baudrate $othbootargs;tftp $ramdiskaddr $ramdiskfile;tftp $loadaddr $bootfile;tftp $fdtaddr $fdtfile;bootm $loadaddr $ramdiskaddr $fdtaddr
=> grep $fdtaddr
fdtaddr=c00000
my_boot=bootm 0x40000000 0x41000000 0x00c00000
my_dtb=tftp 0x00c00000 $prefix/p4080ds.dtb
nohvboot=tftp 1000000 $dir/$bootfile;tftp 2000000 $dir/$ramdiskfile;tftp c00000 $dir/$fdtfile;setenv bootargs root=/dev/ram rw ramdisk_size=0x10000000 console=ttyS0,115200;bootm 1000000 2000000 c00000;
=>

This patch also enables the grepenv command by default on
corenet_ds based boards (and repositions the DHCP command
entry to keep the list sorted).

Signed-off-by: Kim Phillips <kim.phillips@freescale.com>
Cc: Kumar Gala <kumar.gala@freescale.com>
Cc: Andy Fleming <afleming@freescale.com>

# af4d9074 04-Apr-2011 Kim Phillips <kim.phillips@freescale.com>

env: fix env var autocompletion

commit 560d424b6d7cd4205b062ad95f1b104bd4f8bcc3 "env: re-add
support for auto-completion" fell short of its description -
the 'used' logic in hmatch_r was reversed - 'used' is 0 if
the hash table entry is not used, or -1 if deleted. This
patch makes hmatch_r actually match on valid ('used') entries,
instead of skipping them and failing to match anything.

typing 'printenv tft' and hitting 'tab' now displays valid
choices for variable names.

Signed-off-by: Kim Phillips <kim.phillips@freescale.com>
Cc: Mike Frysinger <vapier@gentoo.org>
Tested-by: Mike Frysinger <vapier@gentoo.org>

# c81c1222 21-Mar-2011 Peter Barada <peter.barada@logicpd.com>

Fix hash table deletion to prevent lost entries

Use negative used value to mark deleted entry. Search keeps probing
past deleted entries. Adding an entry uses first deleted entry when
it hits end of probe chain.

Initially found that "ramdiskimage" and "preboot" collide modulus 347,
causing "preboot" to be inserted at idx 190, "ramdiskimage" at idx 191.
Previous to this fix when "preboot" is deleted, "ramdiskimage" is
orphaned.

Signed-off-by: Peter Barada <peter.barada@logicpd.com>
Tested-by: Wolfgang Denk <wd@denx.de>

# 560d424b 17-Dec-2010 Mike Frysinger <vapier@gentoo.org>

env: re-add support for auto-completion

Currently, only basic completion is supported (no globs), but this is
what we had previously.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 2eb1573f 08-Dec-2010 Mike Frysinger <vapier@gentoo.org>

hashtable: drop all non-reentrant versions

The non-reentrant versions of the hashtable functions operate on a single
shared hashtable. So if two different people try using these funcs for
two different purposes, they'll cause problems for the other.

Avoid this by converting all existing hashtable consumers over to the
reentrant versions and then punting the non-reentrant ones.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 071bc923 27-Oct-2010 Wolfgang Denk <wd@denx.de>

Coding Style cleanup

Signed-off-by: Wolfgang Denk <wd@denx.de>

# fc5fc76b 01-Oct-2010 Andreas Bießmann <andreas.devel@googlemail.com>

lib/hashtable.c: add CONFIG_ENV_MIN_ENTRIES

This patch adds a new config parameter for adjusting the calculation of
hash table size when importing a buffer.

When importing a extremely small buffer (e.g. the default_environment)
the old calculation generated a hash table which could hold at most the
buffer content but no more entires.

The new calculation add a fixed number of entries to the result to fit
better for small import buffers. This amount may be configured by the
user in board file to adjust the behaviour.

Signed-off-by: Andreas Bie�mann <andreas.devel@googlemail.com>

# ea882baf 20-Jun-2010 Wolfgang Denk <wd@denx.de>

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>

# a6826fbc 20-Jun-2010 Wolfgang Denk <wd@denx.de>

Add hash table support as base for new environment code

This implementation is based on code from uClibc-0.9.30.3 but was
modified and extended for use within U-Boot.

Major modifications and extensions:

* hsearch() [modified / extended]:
- While the standard version does not make any assumptions about
the type of the stored data objects at all, this implementation
works with NUL terminated strings only.
- Instead of storing just pointers to the original objects, we
create local copies so the caller does not need to care about the
data any more.
- The standard implementation does not provide a way to update an
existing entry. This version will create a new entry or update an
existing one when both "action == ENTER" and "item.data != NULL".
- hsearch_r(): Instead of returning 1 on success, we return the
index into the internal hash table, which is also guaranteed to be
positive. This allows us direct access to the found hash table
slot for example for functions like hdelete().
* hdelete() [added]:
- The standard implementation of hsearch(3) does not provide any way
to delete any entries from the hash table. We extend the code to
do that.
* hexport() [added]:
- Export the data stored in the hash table in linearized form:
Entries are exported as "name=value" strings, separated by an
arbitrary (non-NUL, of course) separator character. This allows to
use this function both when formatting the U-Boot environment for
external storage (using '\0' as separator), but also when using it
for the "printenv" command to print all variables, simply by using
as '\n" as separator. This can also be used for new features like
exporting the environment data as text file, including the option
for later re-import.
- The entries in the result list will be sorted by ascending key
values.
* himport() [added]:
- Import linearized data into hash table. This is the inverse
function to hexport(): it takes a linear list of "name=value"
pairs and creates hash table entries from it.
- Entries without "value", i. e. consisting of only "name" or
"name=", will cause this entry to be deleted from the hash table.
- The "flag" argument can be used to control the behaviour: when
the H_NOCLEAR bit is set, then an existing hash table will kept,
i. e. new data will be added to an existing hash table;
otherwise, old data will be discarded and a new hash table will
be created.
- The separator character for the "name=value" pairs can be
selected, so we both support importing from externally stored
environment data (separated by NUL characters) and from plain text
files (entries separated by newline characters).
- To allow for nicely formatted text input, leading white space
(sequences of SPACE and TAB chars) is ignored, and entries
starting (after removal of any leading white space) with a '#'
character are considered comments and ignored.
- NOTE: this means that a variable name cannot start with a '#'
character.
- When using a non-NUL separator character, backslash is used as
escape character in the value part, allowing for example fo
multi-line values.
- In theory, arbitrary separator characters can be used, but only
'\0' and '\n' have really been tested.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# 60ffcf21 24-Jun-2020 Sean Anderson <seanga2@gmail.com>

lib: Always set errno in hcreate_r

This could give a confusing error message if it failed and didn't set
errno.

Signed-off-by: Sean Anderson <seanga2@gmail.com>
Reviewed-by: Bin Meng <bmeng.cn@gmail.com>

# f7ae49fc 10-May-2020 Simon Glass <sjg@chromium.org>

common: Drop log.h from common header

Move this header out of the common header.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 09140113 10-May-2020 Simon Glass <sjg@chromium.org>

command: Remove the cmd_tbl_t typedef

We should not use typedefs in U-Boot. They cannot be used as forward
declarations which means that header files must include the full header to
access them.

Drop the typedef and rename the struct to remove the _s suffix which is
now not useful.

This requires quite a few header-file additions.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 080019b8 27-Feb-2020 Rasmus Villemoes <rasmus.villemoes@prevas.dk>

make env_entry::callback conditional on !CONFIG_SPL_BUILD

The callback member of struct env_entry is always NULL for an SPL
build. Removing it thus saves a bit of run-time memory in the
SPL (when CONFIG_SPL_ENV_SUPPORT=y) since struct env_entry is embedded
in struct env_entry_node - i.e. about 2KB for the normal case of
512+change hash table entries.

Two small fixups are needed for this, all other references to the
callback member are already under !CONFIG_SPL_BUILD: Don't initialize
.callback in set_flags() - hsearch_r doesn't use that value
anyway. And make env_callback_init() initialize ->callback to NULL for
a new entry instead of relying on an unused or deleted entry having
NULL in ->callback.

Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 34284970 27-Feb-2020 Rasmus Villemoes <rasmus.villemoes@prevas.dk>

lib/hashtable.c: don't test ->callback in SPL

In SPL, environment callbacks are not supported, so e->callback is
always NULL. Removing this makes the SPL a little smaller (about 400
bytes in my ppc build) with no functional change.

Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 7f529f65 27-Feb-2020 Rasmus Villemoes <rasmus.villemoes@prevas.dk>

lib/hashtable.c: create helper for calling env_entry::callback

This is preparation for compiling out the "call the callback" code and
associated error handling for SPL, where ->callback is always NULL.

Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 8bef79bf 14-Nov-2019 Simon Glass <sjg@chromium.org>

common: Move sorting functions to their own header file

These don't need to be in common.h so move them out into a new header.
Also add some missing comments.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@konsulko.com>

# 3f0d6807 01-Aug-2019 Simon Glass <sjg@chromium.org>

env: Drop the ACTION typedef

Avoid using a typedef here which is unnecessary. Add an 'env_' prefix to
both the enum and its members to make it clear that these are related to
the environment.

Add an ENV prefix to these two flags so that it is clear what they relate
to. Also move them to env.h since they are part of the public API. Use an
enum rather than a #define to tie them together.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 25e51e90 02-Aug-2019 Simon Glass <sjg@chromium.org>

env: Drop _ENTRY

This typedef does not need to be defined in the search.h header since it
is only used in one file (hashtable.c). Remove it from the header and
change it to a struct.

Signed-off-by: Simon Glass <sjg@chromium.org>

# dd2408ca 02-Aug-2019 Simon Glass <sjg@chromium.org>

env: Drop the ENTRY typedef

U-Boot is not supposed to use typedef for structs anymore. Also this name
is the same as the ENTRY() macro used in assembler files, and 'entry'
itself is widely used in U-Boot (>8k matches).

Drop the typedef and rename the struct to env_entry to reduce confusion.

Signed-off-by: Simon Glass <sjg@chromium.org>
Acked-by: Joe Hershberger <joe.hershberger@ni.com>

# 9dfdbd9f 30-Jan-2019 Roman Kapl <rka@sysgo.com>

hashtable: fix environment variable corruption

Only first previously deleted entry was recognized, leading hsearch_r
to think that there was no previously deleted entry. It then conluded
that a free entry was found, even if there were no free entries and it
overwrote a random entry.

This patch makes sure all deleted or free entries are always found and
also introduces constants for the 0 and -1 numbers. Unit tests to excersise a
simple hash table usage and catch the corruption were added.

To trash your environment, simply run this loop:

setenv i 0
while true; do
setenv v_$i $i
setenv v_$i
setexpr i $i + 1
done

Signed-off-by: Roman Kapl <rka@sysgo.com>

# 320194ae 23-Jan-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

hashtable: remove caps buffer

slre_match() checks if caps == NULL. In this case it does not try to
update it. So there is no need to create a buffer caps which we do not
evaluate.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 4bca3249 14-Dec-2018 AKASHI Takahiro <takahiro.akashi@linaro.org>

hashtable: fix length calculation in hexport_r

The commit below incorrectly fixed hexport_r();
> size = totlen + 1;
One extra byte is necessary to NULL-terminate a whole buffer, "resp."

Fixes: f1b20acb4a03 ("hashtable: Fix length calculation in hexport_r")
Signed-off-by: AKASHI Takahiro <takahiro.akashi@linaro.org>

# f1b20acb 17-Jul-2018 Zubair Lutfullah Kakakhel <zubair@resin.io>

hashtable: Fix length calculation in hexport_r

The length returned by hexport_r has a few redundant characters.
This appears as NULL characters at the end so seems harmless.

Remove the surplus counts in two places

totlen += strlen(ep->key) + 2;
I'm guessing the +2 here is for = and sep char. But there is another
totlen += 2; line that does that.

size = totletn + 1;
Doesn't make sense and isn't justified with any comment.

Signed-off-by: Zubair Lutfullah Kakakhel <zubair@resin.io>

# d9fc9077 09-Jul-2018 Quentin Schulz <quentin.schulz@bootlin.com>

hashtable: do not recreate whole hash table if vars are passed to himport_r

When vars are passed to the himport_r function with H_NOCLEAR flag,
those vars will be overridden in the current environment and if one of
those vars is not in the imported environment, it'll be deleted in the
current environment whatever the flag passed to himport_r.

The H_NOCLEAR flag is used to clear the whole environment whether vars
are passed to the function or not.

This leads to incoherent behaviour. If one passes vars to himport_r
with the H_NOCLEAR flag, if a var in vars is not in the imported env,
that var will be removed from the current env.

If one passes vars to himport_r without the H_NOCLEAR flag, the whole
environment will be removed and vars will be imported from the
environment in RAM.

It makes more sense to keep the variable that is in the current
environment but not in the imported environment if the H_NOCLEAR flag is
set and remove only that variable if the H_NOCLEAR flag is not set.

Let's clear the whole environment only if H_NOCLEAR and vars are not
passed to himport_r.

Let's remove variables that are in the current environment but not in
the imported env only if the H_NOCLEAR flag is not passed.

Suggested-by: Wolfgang Denk <wd@denx.de>
Signed-off-by: Quentin Schulz <quentin.schulz@bootlin.com>

# 83d290c5 06-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert all of our single license tags to Linux Kernel style

When U-Boot started using SPDX tags we were among the early adopters and
there weren't a lot of other examples to borrow from. So we picked the
area of the file that usually had a full license text and replaced it
with an appropriate SPDX-License-Identifier: entry. Since then, the
Linux Kernel has adopted SPDX tags and they place it as the very first
line in a file (except where shebangs are used, then it's second line)
and with slightly different comment styles than us.

In part due to community overlap, in part due to better tag visibility
and in part for other minor reasons, switch over to that style.

This commit changes all instances where we have a single declared
license in the tag as both the before and after are identical in tag
contents. There's also a few places where I found we did not have a tag
and have introduced one.

Signed-off-by: Tom Rini <trini@konsulko.com>

# d2d9bdfc 28-Sep-2016 B, Ravi <ravibabu@ti.com>

spl: saveenv: adding saveenv support in SPL

By default saveenv option is not supported for SPL. This patch
enable the support for save environment variable for SPL build.

Enable save environment support in SPL after setenv. By default
the saveenv option is not provided in SPL, but some boards need
this support in 'Falcon' boot, where SPL need to boot from
different images based on environment variable set by OS. For
example OS may set "reboot_image" environment variable to
"recovery" inorder to boot recovery image by SPL. The SPL read
"reboot_image" and act accordingly and change the reboot_image
to default mode using setenv and save the environemnt.

Signed-off-by: Ravi Babu <ravibabu@ti.com>
Reviewed-by: Simon Glass <sig@chromium.org>

change in v1:
- dropped SUPPORT, use CONFIG_SPL_SAVEENV
- updates the comments in mmc_private.h

# fc0b5948 07-Sep-2016 Robert P. J. Day <rpjday@crashcourse.ca>

Various, accumulated typos collected from around the tree.

Fix various misspellings of:

* deprecated
* partition
* preceding,preceded
* preparation
* its versus it's
* export
* existing
* scenario
* redundant
* remaining
* value
* architecture

Signed-off-by: Robert P. J. Day <rpjday@crashcourse.ca>
Reviewed-by: Jagan Teki <jteki@openedev.com>
Reviewed-by: Stefan Roese <sr@denx.de>

# c55d02b2 22-Jul-2016 Simon Glass <sjg@chromium.org>

hashtable: Fix compiler warning on 32-bit sandbox

This fixes a mismatch between the %zu format and the type used on sandbox.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@konsulko.com>

# 62a3b7dd 15-Jul-2016 Robert P. J. Day <rpjday@crashcourse.ca>

Various, unrelated tree-wide typo fixes.

Fix a number of typos, including:

* "compatble" -> "compatible"
* "eanbeld" -> "enabled"
* "envrionment" -> "environment"
* "FTD" -> "FDT" (for "flattened device tree")
* "ommitted" -> "omitted"
* "overriden" -> "overridden"
* "partiton" -> "partition"
* "propogate" -> "propagate"
* "resourse" -> "resource"
* "rest in piece" -> "rest in peace"
* "suport" -> "support"
* "varible" -> "variable"

Signed-off-by: Robert P. J. Day <rpjday@crashcourse.ca>

# 0226d878 13-Sep-2015 Lukasz Majewski <l.majewski@majess.pl>

env: import: hashtable: Free memory allocated before exiting from himport_r()

ithout this patch memory is not released on early exit.

Signed-off-by: Lukasz Majewski <l.majewski@majess.pl>

# 817e48d8 13-Sep-2015 Lukasz Majewski <l.majewski@majess.pl>

env: import: hashtable: Prevent buffer overrun when importing environment from file

Lets consider following scenario:
- One uses echo -n "key=value" to define environment variable in a file (single variable)
- The file content is "key=value" without any terminating byte (e.g. 0x0a or
0x0d).
- The file is loaded to u-boot non zero'ed RAM buffer (with load command).
- Then "env import -t -r $loadaddr $filesize" is executed.
- Due to lack of proper termination byte we have classical example of buffer
overrun.

This patch prevents from this by allocating one extra byte than size and
explicitly null terminate it.

There should be no change for normal env import operation after applying
this patch.

Signed-off-by: Lukasz Majewski <l.majewski@majess.pl>

# ecd1446f 14-Jul-2014 Alexander Holler <holler@ahsoftware.de>

Add option -r to env import to allow import of text files with CRLF as line endings

When this option is enabled, CRLF is treated like LF when importing environments
from text files, which means CRs ('\r') in front of LFs ('\n') are just ignored.

Drawback of enabling this option is that (maybe exported) variables which have
a trailing CR in their content will get imported without that CR. But this
drawback is very unlikely and the big advantage of letting Windows user create
a *working* uEnv.txt too is likely more welcome.

Signed-off-by: Alexander Holler <holler@ahsoftware.de>

# 9a832331 08-Oct-2013 Pierre Aubert <p.aubert@staubli.com>

env: fix the env export varname

The env export command doesn't export the first variable of the list
since commit 5a31ea04c9ee5544fbb70ad7597ea4b294840eab
"env grep" - reimplement command using hexport_r()

Signed-off-by: Pierre Aubert <p.aubert@staubli.com>

# 1bce2aeb 16-Sep-2013 Robert P. J. Day <rpjday@crashcourse.ca>

Cosmetic: Fix a number of typos, no functional changes.

Fix various misspellings of things like "environment", "kernel",
"default" and "volatile", and throw in a couple grammar fixes.

Signed-off-by: Robert P. J. Day <rpjday@crashcourse.ca>

# eee479cf 08-Jul-2013 Wolfgang Denk <wd@denx.de>

Add LGPL-2.1+ SPDX-License-Identifier to source files

Signed-off-by: Wolfgang Denk <wd@denx.de>

# e4fdcadd 28-Apr-2013 Lucian Cojocar <cojocar@gmail.com>

env: throw an error when an empty key is used

If the environment contains an entry like "=value" "\0" we should throw
an error when parsing the environment. Otherwise, U-Boot will enter in
an infinite loop.

Signed-off-by: Lucian Cojocar <cojocar@gmail.com>

# be29df6a 23-Mar-2013 Wolfgang Denk <wd@denx.de>

"env grep" - add support for regular expression matches

When CONFIG_REGEX is enabled, the new option "-e" becomes available
which causes regular expression matches to be used. This allows for
example things like these:

- print all MAC addresses:

=> env grep -e eth.*addr
eth1addr=00:10:ec:80:c5:15
ethaddr=00:10:ec:00:c5:15

- print all variables that have at least 2 colons in their value:

=> env grep -v -e :.*:
addip=setenv bootargs ${bootargs} ip=${ipaddr}:${serverip}:${gatewayip}:${netmask}:${hostname}:${netdev}:off
panic=1
eth1addr=00:10:ec:80:c5:15
ethaddr=00:10:ec:00:c5:15
ver=U-Boot 2013.04-rc1-00289-g497746b-dirty (Mar 22 2013 - 12:50:25)

etc.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# 5a31ea04 23-Mar-2013 Wolfgang Denk <wd@denx.de>

"env grep" - reimplement command using hexport_r()

Also drop hstrstr_r() which is not needed any more.
The new code is way more flexible.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# ea009d47 23-Mar-2013 Wolfgang Denk <wd@denx.de>

hashtable: preparations to use hexport_r() for "env grep"

The output of "env grep" is unsorted, and printing is done by a
private implementation to parse the hash table. We have all the
needed code in place in hexport_r() alsready, so let's use this
instead. Here we prepare the code for this, without any functional
changes yet.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# 2598090b 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Add environment variable flags

Currently just validates variable types as decimal, hexidecimal,
boolean, ip address, and mac address.

If the entry is not found in the env ".flags", then look in the static
one. This allows the env to override the static definitions, but prevents
the need to have every definition in the environment distracting you.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# 170ab110 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Add support for callbacks to environment vars

Add support for per-variable callbacks to the "hashtable" functions.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

!!!fix comment in callback

# be11235a 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Hide '.' variables in env print by default

When printing all variables with env print, don't print variables that
begin with '.'. If env print is called with a '-a' switch, then
include variables that begin with '.' (just like the ls command).

Variables printed explicitly will be printed even without the -a.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# 7afcf3a5 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Refactor apply into change_ok

Move the read of the old value to inside the check function. In some
cases it can be avoided all together and at the least the code is only
called from one place.

Also name the function and the callback to more clearly describe what
it does.

Pass the ENTRY instead of just the name for direct access to the whole
data structure.

Pass an enum to the callback that specifies the operation being approved.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# 3d3b52f2 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Consolidate common code in hsearch_r()

The same chunk of code was replicated in two places and the following
changes will make that chunk grow a bit, so combine into a static func.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# c4e0057f 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Refactor do_apply to a flag

Use a flag in hsearch_r for insert mode passed from import to allow the
behavior be different based on use.

Now that "do_check" is called for all imports, ensure console init is
complete before updating the console on relocation import

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# 7ac2fe2d 17-Sep-2012 Ilya Yanok <ilya.yanok@cogentembedded.com>

OMAP: networking support for SPL

This patch adds support for networking in SPL. Some devices are
capable of loading SPL via network so it makes sense to load the
main U-Boot binary via network too. This patch tries to use
existing network code as much as possible. Unfortunately, it depends
on environment which in turn depends on other code so SPL size
is increased significantly. No effort was done to decouple network
code and environment so far.

Signed-off-by: Ilya Yanok <ilya.yanok@cogentembedded.com>
Acked-by: Joe Hershberger <joe.hershberger@ni.com>
Signed-off-by: Tom Rini <trini@ti.com>

# d5370feb 26-Aug-2012 Gerlando Falauto <gerlando.falauto@keymile.com>

env: delete selected vars not present in imported env

When variables explicitly specified on the command line are not present
in the imported env, delete them from the running env.
If the variable is also missing from the running env, issue a warning.

Signed-off-by: Gerlando Falauto <gerlando.falauto@keymile.com>
Reviewed-by: Marek Vasut <marex@denx.de>

# 152874b6 23-Aug-2012 Gerlando Falauto <gerlando.falauto@keymile.com>

env: check and apply changes on delete/destroy

Signed-off-by: Gerlando Falauto <gerlando.falauto@keymile.com>
Reviewed-by: Marek Vasut <marex@denx.de>

# c5983592 23-Aug-2012 Gerlando Falauto <gerlando.falauto@keymile.com>

env: add check/apply logic to himport_r()

Change hashtable so that a callback function will decide whether a
variable can be overwritten, and possibly apply the changes.

So add a new field to struct hsearch_data:

o "apply" callback function to check whether a variable can be
overwritten, and possibly immediately apply the changes;
when NULL, no check is performed.

And a new argument to himport_r():
o "do_apply": whether to call the apply callback function

NOTE: This patch does not change the current behavior.

Signed-off-by: Gerlando Falauto <gerlando.falauto@keymile.com>
Reviewed-by: Marek Vasut <marex@denx.de>

# 348b1f1c 23-Aug-2012 Gerlando Falauto <gerlando.falauto@keymile.com>

env: make himport_r() selective on variables

Add 2 new arguments to himport_r():

o "nvars", "vars": number and list of variables to take into account
(0 means ALL)

NOTE: This patch does not change the current behaviour.

Signed-off-by: Gerlando Falauto <gerlando.falauto@keymile.com>
Reviewed-by: Marek Vasut <marex@denx.de>

# ff856286 21-Nov-2011 Simon Glass <sjg@chromium.org>

sandbox: Fix warnings in hashtable.c

This fixes a few printf() strings for size_t which are missing the 'z'
modifier.

Signed-off-by: Simon Glass <sjg@chromium.org>
Acked-by: Mike Frysinger <vapier@gentoo.org>

# 37f2fe74 06-Nov-2011 Wolfgang Denk <wd@denx.de>

env: allow to export only selected variables

New syntax:
env export [-t | -b | -c] [-s size] addr [var ...]

With this change it is possible to provide a list of variables names
that shall be exported. Whenno arguments are given, the whole
environment gets exported.

NOTE: The new handling of the "size" argument means a change to the
user API.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# 4d91a6ec 23-Aug-2011 Jason Hobbs <jason.hobbs@calxeda.com>

Replace space and tab checks with isblank

These are various places I found that checked for conditions equivalent
to isblank.

Signed-off-by: Jason Hobbs <jason.hobbs@calxeda.com>

# 84b5e802 29-Jul-2011 Wolfgang Denk <wd@denx.de>

Constify getenv(), setenv() and hash code functions

This is needed to get rid of build warnings like

main.c:311: warning: passing argument 2 of 'setenv' discards qualifiers from pointer target type

which result from commit 09c2e90 "unify version_string".

Signed-off-by: Wolfgang Denk <wd@denx.de>
Cc: Andreas Bie�mann <andreas.devel@googlemail.com>

# a000b795 05-Apr-2011 Kim Phillips <kim.phillips@freescale.com>

common: add a grepenv command

u-boot environments, esp. when boards are shared across multiple
users, can get pretty large and time consuming to visually parse.
The grepenv command this patch adds can be used in lieu of printenv
to facilitate searching. grepenv works like printenv but limits
its output only to environment strings (variable name and value
pairs) that match the user specified substring.

the following examples are on a board with a 5313 byte environment
that spans multiple screen pages:

Example 1: summarize ethernet configuration:

=> grepenv eth TSEC
etact=FM1@DTSEC2
eth=FM1@DTSEC4
ethact=FM1@DTSEC2
eth1addr=00:E0:0C:00:8b:01
eth2addr=00:E0:0C:00:8b:02
eth3addr=00:E0:0C:00:8b:03
eth4addr=00:E0:0C:00:8b:04
eth5addr=00:E0:0C:00:8b:05
eth6addr=00:E0:0C:00:8b:06
eth7addr=00:E0:0C:00:8b:07
eth8addr=00:E0:0C:00:8b:08
eth9addr=00:E0:0C:00:8b:09
ethaddr=00:E0:0C:00:8b:00
netdev=eth0
uprcw=setenv ethact $eth;setenv filename p4080ds/R_PPSXX_0xe/rcw_0xe_2sgmii_rev2_high.bin;setenv start 0xe8000000;protect off all;run upimage;protect on all
upuboot=setenv ethact $eth;setenv filename u-boot.bin;setenv start eff80000;protect off all;run upimage;protect on all
upucode=setenv ethact $eth;setenv filename fsl_fman_ucode_P4080_101_6.bin;setenv start 0xef000000;protect off all;run upimage;protect on all
usdboot=setenv ethact $eth;tftp 1000000 $dir/$bootfile;tftp 2000000 $dir/initramfs.cpio.gz.uboot;tftp c00000 $dir/p4080ds-usdpaa.dtb;setenv bootargs root=/dev/ram rw console=ttyS0,115200 $othbootargs;bootm 1000000 2000000 c00000;
=>

Example 2: detect unused env vars:

=> grepenv etact
etact=FM1@DTSEC2
=>

Example 3: reveal hardcoded variables; e.g., for fdtaddr:

=> grepenv fdtaddr
fdtaddr=c00000
nfsboot=setenv bootargs root=/dev/nfs rw nfsroot=$serverip:$rootpath ip=$ipaddr:$serverip:$gatewayip:$netmask:$hostname:$netdev:off console=$consoledev,$baudrate $othbootargs;tftp $loadaddr $bootfile;tftp $fdtaddr $fdtfile;bootm $loadaddr - $fdtaddr
ramboot=setenv bootargs root=/dev/ram rw console=$consoledev,$baudrate $othbootargs;tftp $ramdiskaddr $ramdiskfile;tftp $loadaddr $bootfile;tftp $fdtaddr $fdtfile;bootm $loadaddr $ramdiskaddr $fdtaddr
=> grep $fdtaddr
fdtaddr=c00000
my_boot=bootm 0x40000000 0x41000000 0x00c00000
my_dtb=tftp 0x00c00000 $prefix/p4080ds.dtb
nohvboot=tftp 1000000 $dir/$bootfile;tftp 2000000 $dir/$ramdiskfile;tftp c00000 $dir/$fdtfile;setenv bootargs root=/dev/ram rw ramdisk_size=0x10000000 console=ttyS0,115200;bootm 1000000 2000000 c00000;
=>

This patch also enables the grepenv command by default on
corenet_ds based boards (and repositions the DHCP command
entry to keep the list sorted).

Signed-off-by: Kim Phillips <kim.phillips@freescale.com>
Cc: Kumar Gala <kumar.gala@freescale.com>
Cc: Andy Fleming <afleming@freescale.com>

# af4d9074 04-Apr-2011 Kim Phillips <kim.phillips@freescale.com>

env: fix env var autocompletion

commit 560d424b6d7cd4205b062ad95f1b104bd4f8bcc3 "env: re-add
support for auto-completion" fell short of its description -
the 'used' logic in hmatch_r was reversed - 'used' is 0 if
the hash table entry is not used, or -1 if deleted. This
patch makes hmatch_r actually match on valid ('used') entries,
instead of skipping them and failing to match anything.

typing 'printenv tft' and hitting 'tab' now displays valid
choices for variable names.

Signed-off-by: Kim Phillips <kim.phillips@freescale.com>
Cc: Mike Frysinger <vapier@gentoo.org>
Tested-by: Mike Frysinger <vapier@gentoo.org>

# c81c1222 21-Mar-2011 Peter Barada <peter.barada@logicpd.com>

Fix hash table deletion to prevent lost entries

Use negative used value to mark deleted entry. Search keeps probing
past deleted entries. Adding an entry uses first deleted entry when
it hits end of probe chain.

Initially found that "ramdiskimage" and "preboot" collide modulus 347,
causing "preboot" to be inserted at idx 190, "ramdiskimage" at idx 191.
Previous to this fix when "preboot" is deleted, "ramdiskimage" is
orphaned.

Signed-off-by: Peter Barada <peter.barada@logicpd.com>
Tested-by: Wolfgang Denk <wd@denx.de>

# 560d424b 17-Dec-2010 Mike Frysinger <vapier@gentoo.org>

env: re-add support for auto-completion

Currently, only basic completion is supported (no globs), but this is
what we had previously.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 2eb1573f 08-Dec-2010 Mike Frysinger <vapier@gentoo.org>

hashtable: drop all non-reentrant versions

The non-reentrant versions of the hashtable functions operate on a single
shared hashtable. So if two different people try using these funcs for
two different purposes, they'll cause problems for the other.

Avoid this by converting all existing hashtable consumers over to the
reentrant versions and then punting the non-reentrant ones.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 071bc923 27-Oct-2010 Wolfgang Denk <wd@denx.de>

Coding Style cleanup

Signed-off-by: Wolfgang Denk <wd@denx.de>

# fc5fc76b 01-Oct-2010 Andreas Bießmann <andreas.devel@googlemail.com>

lib/hashtable.c: add CONFIG_ENV_MIN_ENTRIES

This patch adds a new config parameter for adjusting the calculation of
hash table size when importing a buffer.

When importing a extremely small buffer (e.g. the default_environment)
the old calculation generated a hash table which could hold at most the
buffer content but no more entires.

The new calculation add a fixed number of entries to the result to fit
better for small import buffers. This amount may be configured by the
user in board file to adjust the behaviour.

Signed-off-by: Andreas Bie�mann <andreas.devel@googlemail.com>

# ea882baf 20-Jun-2010 Wolfgang Denk <wd@denx.de>

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>

# a6826fbc 20-Jun-2010 Wolfgang Denk <wd@denx.de>

Add hash table support as base for new environment code

This implementation is based on code from uClibc-0.9.30.3 but was
modified and extended for use within U-Boot.

Major modifications and extensions:

* hsearch() [modified / extended]:
- While the standard version does not make any assumptions about
the type of the stored data objects at all, this implementation
works with NUL terminated strings only.
- Instead of storing just pointers to the original objects, we
create local copies so the caller does not need to care about the
data any more.
- The standard implementation does not provide a way to update an
existing entry. This version will create a new entry or update an
existing one when both "action == ENTER" and "item.data != NULL".
- hsearch_r(): Instead of returning 1 on success, we return the
index into the internal hash table, which is also guaranteed to be
positive. This allows us direct access to the found hash table
slot for example for functions like hdelete().
* hdelete() [added]:
- The standard implementation of hsearch(3) does not provide any way
to delete any entries from the hash table. We extend the code to
do that.
* hexport() [added]:
- Export the data stored in the hash table in linearized form:
Entries are exported as "name=value" strings, separated by an
arbitrary (non-NUL, of course) separator character. This allows to
use this function both when formatting the U-Boot environment for
external storage (using '\0' as separator), but also when using it
for the "printenv" command to print all variables, simply by using
as '\n" as separator. This can also be used for new features like
exporting the environment data as text file, including the option
for later re-import.
- The entries in the result list will be sorted by ascending key
values.
* himport() [added]:
- Import linearized data into hash table. This is the inverse
function to hexport(): it takes a linear list of "name=value"
pairs and creates hash table entries from it.
- Entries without "value", i. e. consisting of only "name" or
"name=", will cause this entry to be deleted from the hash table.
- The "flag" argument can be used to control the behaviour: when
the H_NOCLEAR bit is set, then an existing hash table will kept,
i. e. new data will be added to an existing hash table;
otherwise, old data will be discarded and a new hash table will
be created.
- The separator character for the "name=value" pairs can be
selected, so we both support importing from externally stored
environment data (separated by NUL characters) and from plain text
files (entries separated by newline characters).
- To allow for nicely formatted text input, leading white space
(sequences of SPACE and TAB chars) is ignored, and entries
starting (after removal of any leading white space) with a '#'
character are considered comments and ignored.
- NOTE: this means that a variable name cannot start with a '#'
character.
- When using a non-NUL separator character, backslash is used as
escape character in the value part, allowing for example fo
multi-line values.
- In theory, arbitrary separator characters can be used, but only
'\0' and '\n' have really been tested.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# f7ae49fc 10-May-2020 Simon Glass <sjg@chromium.org>

common: Drop log.h from common header

Move this header out of the common header.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 09140113 10-May-2020 Simon Glass <sjg@chromium.org>

command: Remove the cmd_tbl_t typedef

We should not use typedefs in U-Boot. They cannot be used as forward
declarations which means that header files must include the full header to
access them.

Drop the typedef and rename the struct to remove the _s suffix which is
now not useful.

This requires quite a few header-file additions.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 080019b8 27-Feb-2020 Rasmus Villemoes <rasmus.villemoes@prevas.dk>

make env_entry::callback conditional on !CONFIG_SPL_BUILD

The callback member of struct env_entry is always NULL for an SPL
build. Removing it thus saves a bit of run-time memory in the
SPL (when CONFIG_SPL_ENV_SUPPORT=y) since struct env_entry is embedded
in struct env_entry_node - i.e. about 2KB for the normal case of
512+change hash table entries.

Two small fixups are needed for this, all other references to the
callback member are already under !CONFIG_SPL_BUILD: Don't initialize
.callback in set_flags() - hsearch_r doesn't use that value
anyway. And make env_callback_init() initialize ->callback to NULL for
a new entry instead of relying on an unused or deleted entry having
NULL in ->callback.

Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 34284970 27-Feb-2020 Rasmus Villemoes <rasmus.villemoes@prevas.dk>

lib/hashtable.c: don't test ->callback in SPL

In SPL, environment callbacks are not supported, so e->callback is
always NULL. Removing this makes the SPL a little smaller (about 400
bytes in my ppc build) with no functional change.

Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 7f529f65 27-Feb-2020 Rasmus Villemoes <rasmus.villemoes@prevas.dk>

lib/hashtable.c: create helper for calling env_entry::callback

This is preparation for compiling out the "call the callback" code and
associated error handling for SPL, where ->callback is always NULL.

Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 8bef79bf 14-Nov-2019 Simon Glass <sjg@chromium.org>

common: Move sorting functions to their own header file

These don't need to be in common.h so move them out into a new header.
Also add some missing comments.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@konsulko.com>

# 3f0d6807 01-Aug-2019 Simon Glass <sjg@chromium.org>

env: Drop the ACTION typedef

Avoid using a typedef here which is unnecessary. Add an 'env_' prefix to
both the enum and its members to make it clear that these are related to
the environment.

Add an ENV prefix to these two flags so that it is clear what they relate
to. Also move them to env.h since they are part of the public API. Use an
enum rather than a #define to tie them together.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 25e51e90 02-Aug-2019 Simon Glass <sjg@chromium.org>

env: Drop _ENTRY

This typedef does not need to be defined in the search.h header since it
is only used in one file (hashtable.c). Remove it from the header and
change it to a struct.

Signed-off-by: Simon Glass <sjg@chromium.org>

# dd2408ca 02-Aug-2019 Simon Glass <sjg@chromium.org>

env: Drop the ENTRY typedef

U-Boot is not supposed to use typedef for structs anymore. Also this name
is the same as the ENTRY() macro used in assembler files, and 'entry'
itself is widely used in U-Boot (>8k matches).

Drop the typedef and rename the struct to env_entry to reduce confusion.

Signed-off-by: Simon Glass <sjg@chromium.org>
Acked-by: Joe Hershberger <joe.hershberger@ni.com>

# 9dfdbd9f 30-Jan-2019 Roman Kapl <rka@sysgo.com>

hashtable: fix environment variable corruption

Only first previously deleted entry was recognized, leading hsearch_r
to think that there was no previously deleted entry. It then conluded
that a free entry was found, even if there were no free entries and it
overwrote a random entry.

This patch makes sure all deleted or free entries are always found and
also introduces constants for the 0 and -1 numbers. Unit tests to excersise a
simple hash table usage and catch the corruption were added.

To trash your environment, simply run this loop:

setenv i 0
while true; do
setenv v_$i $i
setenv v_$i
setexpr i $i + 1
done

Signed-off-by: Roman Kapl <rka@sysgo.com>

# 320194ae 23-Jan-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

hashtable: remove caps buffer

slre_match() checks if caps == NULL. In this case it does not try to
update it. So there is no need to create a buffer caps which we do not
evaluate.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 4bca3249 14-Dec-2018 AKASHI Takahiro <takahiro.akashi@linaro.org>

hashtable: fix length calculation in hexport_r

The commit below incorrectly fixed hexport_r();
> size = totlen + 1;
One extra byte is necessary to NULL-terminate a whole buffer, "resp."

Fixes: f1b20acb4a03 ("hashtable: Fix length calculation in hexport_r")
Signed-off-by: AKASHI Takahiro <takahiro.akashi@linaro.org>

# f1b20acb 17-Jul-2018 Zubair Lutfullah Kakakhel <zubair@resin.io>

hashtable: Fix length calculation in hexport_r

The length returned by hexport_r has a few redundant characters.
This appears as NULL characters at the end so seems harmless.

Remove the surplus counts in two places

totlen += strlen(ep->key) + 2;
I'm guessing the +2 here is for = and sep char. But there is another
totlen += 2; line that does that.

size = totletn + 1;
Doesn't make sense and isn't justified with any comment.

Signed-off-by: Zubair Lutfullah Kakakhel <zubair@resin.io>

# d9fc9077 09-Jul-2018 Quentin Schulz <quentin.schulz@bootlin.com>

hashtable: do not recreate whole hash table if vars are passed to himport_r

When vars are passed to the himport_r function with H_NOCLEAR flag,
those vars will be overridden in the current environment and if one of
those vars is not in the imported environment, it'll be deleted in the
current environment whatever the flag passed to himport_r.

The H_NOCLEAR flag is used to clear the whole environment whether vars
are passed to the function or not.

This leads to incoherent behaviour. If one passes vars to himport_r
with the H_NOCLEAR flag, if a var in vars is not in the imported env,
that var will be removed from the current env.

If one passes vars to himport_r without the H_NOCLEAR flag, the whole
environment will be removed and vars will be imported from the
environment in RAM.

It makes more sense to keep the variable that is in the current
environment but not in the imported environment if the H_NOCLEAR flag is
set and remove only that variable if the H_NOCLEAR flag is not set.

Let's clear the whole environment only if H_NOCLEAR and vars are not
passed to himport_r.

Let's remove variables that are in the current environment but not in
the imported env only if the H_NOCLEAR flag is not passed.

Suggested-by: Wolfgang Denk <wd@denx.de>
Signed-off-by: Quentin Schulz <quentin.schulz@bootlin.com>

# 83d290c5 06-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert all of our single license tags to Linux Kernel style

When U-Boot started using SPDX tags we were among the early adopters and
there weren't a lot of other examples to borrow from. So we picked the
area of the file that usually had a full license text and replaced it
with an appropriate SPDX-License-Identifier: entry. Since then, the
Linux Kernel has adopted SPDX tags and they place it as the very first
line in a file (except where shebangs are used, then it's second line)
and with slightly different comment styles than us.

In part due to community overlap, in part due to better tag visibility
and in part for other minor reasons, switch over to that style.

This commit changes all instances where we have a single declared
license in the tag as both the before and after are identical in tag
contents. There's also a few places where I found we did not have a tag
and have introduced one.

Signed-off-by: Tom Rini <trini@konsulko.com>

# d2d9bdfc 28-Sep-2016 B, Ravi <ravibabu@ti.com>

spl: saveenv: adding saveenv support in SPL

By default saveenv option is not supported for SPL. This patch
enable the support for save environment variable for SPL build.

Enable save environment support in SPL after setenv. By default
the saveenv option is not provided in SPL, but some boards need
this support in 'Falcon' boot, where SPL need to boot from
different images based on environment variable set by OS. For
example OS may set "reboot_image" environment variable to
"recovery" inorder to boot recovery image by SPL. The SPL read
"reboot_image" and act accordingly and change the reboot_image
to default mode using setenv and save the environemnt.

Signed-off-by: Ravi Babu <ravibabu@ti.com>
Reviewed-by: Simon Glass <sig@chromium.org>

change in v1:
- dropped SUPPORT, use CONFIG_SPL_SAVEENV
- updates the comments in mmc_private.h

# fc0b5948 07-Sep-2016 Robert P. J. Day <rpjday@crashcourse.ca>

Various, accumulated typos collected from around the tree.

Fix various misspellings of:

* deprecated
* partition
* preceding,preceded
* preparation
* its versus it's
* export
* existing
* scenario
* redundant
* remaining
* value
* architecture

Signed-off-by: Robert P. J. Day <rpjday@crashcourse.ca>
Reviewed-by: Jagan Teki <jteki@openedev.com>
Reviewed-by: Stefan Roese <sr@denx.de>

# c55d02b2 22-Jul-2016 Simon Glass <sjg@chromium.org>

hashtable: Fix compiler warning on 32-bit sandbox

This fixes a mismatch between the %zu format and the type used on sandbox.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@konsulko.com>

# 62a3b7dd 15-Jul-2016 Robert P. J. Day <rpjday@crashcourse.ca>

Various, unrelated tree-wide typo fixes.

Fix a number of typos, including:

* "compatble" -> "compatible"
* "eanbeld" -> "enabled"
* "envrionment" -> "environment"
* "FTD" -> "FDT" (for "flattened device tree")
* "ommitted" -> "omitted"
* "overriden" -> "overridden"
* "partiton" -> "partition"
* "propogate" -> "propagate"
* "resourse" -> "resource"
* "rest in piece" -> "rest in peace"
* "suport" -> "support"
* "varible" -> "variable"

Signed-off-by: Robert P. J. Day <rpjday@crashcourse.ca>

# 0226d878 13-Sep-2015 Lukasz Majewski <l.majewski@majess.pl>

env: import: hashtable: Free memory allocated before exiting from himport_r()

ithout this patch memory is not released on early exit.

Signed-off-by: Lukasz Majewski <l.majewski@majess.pl>

# 817e48d8 13-Sep-2015 Lukasz Majewski <l.majewski@majess.pl>

env: import: hashtable: Prevent buffer overrun when importing environment from file

Lets consider following scenario:
- One uses echo -n "key=value" to define environment variable in a file (single variable)
- The file content is "key=value" without any terminating byte (e.g. 0x0a or
0x0d).
- The file is loaded to u-boot non zero'ed RAM buffer (with load command).
- Then "env import -t -r $loadaddr $filesize" is executed.
- Due to lack of proper termination byte we have classical example of buffer
overrun.

This patch prevents from this by allocating one extra byte than size and
explicitly null terminate it.

There should be no change for normal env import operation after applying
this patch.

Signed-off-by: Lukasz Majewski <l.majewski@majess.pl>

# ecd1446f 14-Jul-2014 Alexander Holler <holler@ahsoftware.de>

Add option -r to env import to allow import of text files with CRLF as line endings

When this option is enabled, CRLF is treated like LF when importing environments
from text files, which means CRs ('\r') in front of LFs ('\n') are just ignored.

Drawback of enabling this option is that (maybe exported) variables which have
a trailing CR in their content will get imported without that CR. But this
drawback is very unlikely and the big advantage of letting Windows user create
a *working* uEnv.txt too is likely more welcome.

Signed-off-by: Alexander Holler <holler@ahsoftware.de>

# 9a832331 08-Oct-2013 Pierre Aubert <p.aubert@staubli.com>

env: fix the env export varname

The env export command doesn't export the first variable of the list
since commit 5a31ea04c9ee5544fbb70ad7597ea4b294840eab
"env grep" - reimplement command using hexport_r()

Signed-off-by: Pierre Aubert <p.aubert@staubli.com>

# 1bce2aeb 16-Sep-2013 Robert P. J. Day <rpjday@crashcourse.ca>

Cosmetic: Fix a number of typos, no functional changes.

Fix various misspellings of things like "environment", "kernel",
"default" and "volatile", and throw in a couple grammar fixes.

Signed-off-by: Robert P. J. Day <rpjday@crashcourse.ca>

# eee479cf 08-Jul-2013 Wolfgang Denk <wd@denx.de>

Add LGPL-2.1+ SPDX-License-Identifier to source files

Signed-off-by: Wolfgang Denk <wd@denx.de>

# e4fdcadd 28-Apr-2013 Lucian Cojocar <cojocar@gmail.com>

env: throw an error when an empty key is used

If the environment contains an entry like "=value" "\0" we should throw
an error when parsing the environment. Otherwise, U-Boot will enter in
an infinite loop.

Signed-off-by: Lucian Cojocar <cojocar@gmail.com>

# be29df6a 23-Mar-2013 Wolfgang Denk <wd@denx.de>

"env grep" - add support for regular expression matches

When CONFIG_REGEX is enabled, the new option "-e" becomes available
which causes regular expression matches to be used. This allows for
example things like these:

- print all MAC addresses:

=> env grep -e eth.*addr
eth1addr=00:10:ec:80:c5:15
ethaddr=00:10:ec:00:c5:15

- print all variables that have at least 2 colons in their value:

=> env grep -v -e :.*:
addip=setenv bootargs ${bootargs} ip=${ipaddr}:${serverip}:${gatewayip}:${netmask}:${hostname}:${netdev}:off
panic=1
eth1addr=00:10:ec:80:c5:15
ethaddr=00:10:ec:00:c5:15
ver=U-Boot 2013.04-rc1-00289-g497746b-dirty (Mar 22 2013 - 12:50:25)

etc.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# 5a31ea04 23-Mar-2013 Wolfgang Denk <wd@denx.de>

"env grep" - reimplement command using hexport_r()

Also drop hstrstr_r() which is not needed any more.
The new code is way more flexible.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# ea009d47 23-Mar-2013 Wolfgang Denk <wd@denx.de>

hashtable: preparations to use hexport_r() for "env grep"

The output of "env grep" is unsorted, and printing is done by a
private implementation to parse the hash table. We have all the
needed code in place in hexport_r() alsready, so let's use this
instead. Here we prepare the code for this, without any functional
changes yet.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# 2598090b 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Add environment variable flags

Currently just validates variable types as decimal, hexidecimal,
boolean, ip address, and mac address.

If the entry is not found in the env ".flags", then look in the static
one. This allows the env to override the static definitions, but prevents
the need to have every definition in the environment distracting you.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# 170ab110 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Add support for callbacks to environment vars

Add support for per-variable callbacks to the "hashtable" functions.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

!!!fix comment in callback

# be11235a 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Hide '.' variables in env print by default

When printing all variables with env print, don't print variables that
begin with '.'. If env print is called with a '-a' switch, then
include variables that begin with '.' (just like the ls command).

Variables printed explicitly will be printed even without the -a.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# 7afcf3a5 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Refactor apply into change_ok

Move the read of the old value to inside the check function. In some
cases it can be avoided all together and at the least the code is only
called from one place.

Also name the function and the callback to more clearly describe what
it does.

Pass the ENTRY instead of just the name for direct access to the whole
data structure.

Pass an enum to the callback that specifies the operation being approved.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# 3d3b52f2 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Consolidate common code in hsearch_r()

The same chunk of code was replicated in two places and the following
changes will make that chunk grow a bit, so combine into a static func.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# c4e0057f 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Refactor do_apply to a flag

Use a flag in hsearch_r for insert mode passed from import to allow the
behavior be different based on use.

Now that "do_check" is called for all imports, ensure console init is
complete before updating the console on relocation import

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# 7ac2fe2d 17-Sep-2012 Ilya Yanok <ilya.yanok@cogentembedded.com>

OMAP: networking support for SPL

This patch adds support for networking in SPL. Some devices are
capable of loading SPL via network so it makes sense to load the
main U-Boot binary via network too. This patch tries to use
existing network code as much as possible. Unfortunately, it depends
on environment which in turn depends on other code so SPL size
is increased significantly. No effort was done to decouple network
code and environment so far.

Signed-off-by: Ilya Yanok <ilya.yanok@cogentembedded.com>
Acked-by: Joe Hershberger <joe.hershberger@ni.com>
Signed-off-by: Tom Rini <trini@ti.com>

# d5370feb 26-Aug-2012 Gerlando Falauto <gerlando.falauto@keymile.com>

env: delete selected vars not present in imported env

When variables explicitly specified on the command line are not present
in the imported env, delete them from the running env.
If the variable is also missing from the running env, issue a warning.

Signed-off-by: Gerlando Falauto <gerlando.falauto@keymile.com>
Reviewed-by: Marek Vasut <marex@denx.de>

# 152874b6 23-Aug-2012 Gerlando Falauto <gerlando.falauto@keymile.com>

env: check and apply changes on delete/destroy

Signed-off-by: Gerlando Falauto <gerlando.falauto@keymile.com>
Reviewed-by: Marek Vasut <marex@denx.de>

# c5983592 23-Aug-2012 Gerlando Falauto <gerlando.falauto@keymile.com>

env: add check/apply logic to himport_r()

Change hashtable so that a callback function will decide whether a
variable can be overwritten, and possibly apply the changes.

So add a new field to struct hsearch_data:

o "apply" callback function to check whether a variable can be
overwritten, and possibly immediately apply the changes;
when NULL, no check is performed.

And a new argument to himport_r():
o "do_apply": whether to call the apply callback function

NOTE: This patch does not change the current behavior.

Signed-off-by: Gerlando Falauto <gerlando.falauto@keymile.com>
Reviewed-by: Marek Vasut <marex@denx.de>

# 348b1f1c 23-Aug-2012 Gerlando Falauto <gerlando.falauto@keymile.com>

env: make himport_r() selective on variables

Add 2 new arguments to himport_r():

o "nvars", "vars": number and list of variables to take into account
(0 means ALL)

NOTE: This patch does not change the current behaviour.

Signed-off-by: Gerlando Falauto <gerlando.falauto@keymile.com>
Reviewed-by: Marek Vasut <marex@denx.de>

# ff856286 21-Nov-2011 Simon Glass <sjg@chromium.org>

sandbox: Fix warnings in hashtable.c

This fixes a few printf() strings for size_t which are missing the 'z'
modifier.

Signed-off-by: Simon Glass <sjg@chromium.org>
Acked-by: Mike Frysinger <vapier@gentoo.org>

# 37f2fe74 06-Nov-2011 Wolfgang Denk <wd@denx.de>

env: allow to export only selected variables

New syntax:
env export [-t | -b | -c] [-s size] addr [var ...]

With this change it is possible to provide a list of variables names
that shall be exported. Whenno arguments are given, the whole
environment gets exported.

NOTE: The new handling of the "size" argument means a change to the
user API.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# 4d91a6ec 23-Aug-2011 Jason Hobbs <jason.hobbs@calxeda.com>

Replace space and tab checks with isblank

These are various places I found that checked for conditions equivalent
to isblank.

Signed-off-by: Jason Hobbs <jason.hobbs@calxeda.com>

# 84b5e802 29-Jul-2011 Wolfgang Denk <wd@denx.de>

Constify getenv(), setenv() and hash code functions

This is needed to get rid of build warnings like

main.c:311: warning: passing argument 2 of 'setenv' discards qualifiers from pointer target type

which result from commit 09c2e90 "unify version_string".

Signed-off-by: Wolfgang Denk <wd@denx.de>
Cc: Andreas Bie�mann <andreas.devel@googlemail.com>

# a000b795 05-Apr-2011 Kim Phillips <kim.phillips@freescale.com>

common: add a grepenv command

u-boot environments, esp. when boards are shared across multiple
users, can get pretty large and time consuming to visually parse.
The grepenv command this patch adds can be used in lieu of printenv
to facilitate searching. grepenv works like printenv but limits
its output only to environment strings (variable name and value
pairs) that match the user specified substring.

the following examples are on a board with a 5313 byte environment
that spans multiple screen pages:

Example 1: summarize ethernet configuration:

=> grepenv eth TSEC
etact=FM1@DTSEC2
eth=FM1@DTSEC4
ethact=FM1@DTSEC2
eth1addr=00:E0:0C:00:8b:01
eth2addr=00:E0:0C:00:8b:02
eth3addr=00:E0:0C:00:8b:03
eth4addr=00:E0:0C:00:8b:04
eth5addr=00:E0:0C:00:8b:05
eth6addr=00:E0:0C:00:8b:06
eth7addr=00:E0:0C:00:8b:07
eth8addr=00:E0:0C:00:8b:08
eth9addr=00:E0:0C:00:8b:09
ethaddr=00:E0:0C:00:8b:00
netdev=eth0
uprcw=setenv ethact $eth;setenv filename p4080ds/R_PPSXX_0xe/rcw_0xe_2sgmii_rev2_high.bin;setenv start 0xe8000000;protect off all;run upimage;protect on all
upuboot=setenv ethact $eth;setenv filename u-boot.bin;setenv start eff80000;protect off all;run upimage;protect on all
upucode=setenv ethact $eth;setenv filename fsl_fman_ucode_P4080_101_6.bin;setenv start 0xef000000;protect off all;run upimage;protect on all
usdboot=setenv ethact $eth;tftp 1000000 $dir/$bootfile;tftp 2000000 $dir/initramfs.cpio.gz.uboot;tftp c00000 $dir/p4080ds-usdpaa.dtb;setenv bootargs root=/dev/ram rw console=ttyS0,115200 $othbootargs;bootm 1000000 2000000 c00000;
=>

Example 2: detect unused env vars:

=> grepenv etact
etact=FM1@DTSEC2
=>

Example 3: reveal hardcoded variables; e.g., for fdtaddr:

=> grepenv fdtaddr
fdtaddr=c00000
nfsboot=setenv bootargs root=/dev/nfs rw nfsroot=$serverip:$rootpath ip=$ipaddr:$serverip:$gatewayip:$netmask:$hostname:$netdev:off console=$consoledev,$baudrate $othbootargs;tftp $loadaddr $bootfile;tftp $fdtaddr $fdtfile;bootm $loadaddr - $fdtaddr
ramboot=setenv bootargs root=/dev/ram rw console=$consoledev,$baudrate $othbootargs;tftp $ramdiskaddr $ramdiskfile;tftp $loadaddr $bootfile;tftp $fdtaddr $fdtfile;bootm $loadaddr $ramdiskaddr $fdtaddr
=> grep $fdtaddr
fdtaddr=c00000
my_boot=bootm 0x40000000 0x41000000 0x00c00000
my_dtb=tftp 0x00c00000 $prefix/p4080ds.dtb
nohvboot=tftp 1000000 $dir/$bootfile;tftp 2000000 $dir/$ramdiskfile;tftp c00000 $dir/$fdtfile;setenv bootargs root=/dev/ram rw ramdisk_size=0x10000000 console=ttyS0,115200;bootm 1000000 2000000 c00000;
=>

This patch also enables the grepenv command by default on
corenet_ds based boards (and repositions the DHCP command
entry to keep the list sorted).

Signed-off-by: Kim Phillips <kim.phillips@freescale.com>
Cc: Kumar Gala <kumar.gala@freescale.com>
Cc: Andy Fleming <afleming@freescale.com>

# af4d9074 04-Apr-2011 Kim Phillips <kim.phillips@freescale.com>

env: fix env var autocompletion

commit 560d424b6d7cd4205b062ad95f1b104bd4f8bcc3 "env: re-add
support for auto-completion" fell short of its description -
the 'used' logic in hmatch_r was reversed - 'used' is 0 if
the hash table entry is not used, or -1 if deleted. This
patch makes hmatch_r actually match on valid ('used') entries,
instead of skipping them and failing to match anything.

typing 'printenv tft' and hitting 'tab' now displays valid
choices for variable names.

Signed-off-by: Kim Phillips <kim.phillips@freescale.com>
Cc: Mike Frysinger <vapier@gentoo.org>
Tested-by: Mike Frysinger <vapier@gentoo.org>

# c81c1222 21-Mar-2011 Peter Barada <peter.barada@logicpd.com>

Fix hash table deletion to prevent lost entries

Use negative used value to mark deleted entry. Search keeps probing
past deleted entries. Adding an entry uses first deleted entry when
it hits end of probe chain.

Initially found that "ramdiskimage" and "preboot" collide modulus 347,
causing "preboot" to be inserted at idx 190, "ramdiskimage" at idx 191.
Previous to this fix when "preboot" is deleted, "ramdiskimage" is
orphaned.

Signed-off-by: Peter Barada <peter.barada@logicpd.com>
Tested-by: Wolfgang Denk <wd@denx.de>

# 560d424b 17-Dec-2010 Mike Frysinger <vapier@gentoo.org>

env: re-add support for auto-completion

Currently, only basic completion is supported (no globs), but this is
what we had previously.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 2eb1573f 08-Dec-2010 Mike Frysinger <vapier@gentoo.org>

hashtable: drop all non-reentrant versions

The non-reentrant versions of the hashtable functions operate on a single
shared hashtable. So if two different people try using these funcs for
two different purposes, they'll cause problems for the other.

Avoid this by converting all existing hashtable consumers over to the
reentrant versions and then punting the non-reentrant ones.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 071bc923 27-Oct-2010 Wolfgang Denk <wd@denx.de>

Coding Style cleanup

Signed-off-by: Wolfgang Denk <wd@denx.de>

# fc5fc76b 01-Oct-2010 Andreas Bießmann <andreas.devel@googlemail.com>

lib/hashtable.c: add CONFIG_ENV_MIN_ENTRIES

This patch adds a new config parameter for adjusting the calculation of
hash table size when importing a buffer.

When importing a extremely small buffer (e.g. the default_environment)
the old calculation generated a hash table which could hold at most the
buffer content but no more entires.

The new calculation add a fixed number of entries to the result to fit
better for small import buffers. This amount may be configured by the
user in board file to adjust the behaviour.

Signed-off-by: Andreas Bie�mann <andreas.devel@googlemail.com>

# ea882baf 20-Jun-2010 Wolfgang Denk <wd@denx.de>

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>

# a6826fbc 20-Jun-2010 Wolfgang Denk <wd@denx.de>

Add hash table support as base for new environment code

This implementation is based on code from uClibc-0.9.30.3 but was
modified and extended for use within U-Boot.

Major modifications and extensions:

* hsearch() [modified / extended]:
- While the standard version does not make any assumptions about
the type of the stored data objects at all, this implementation
works with NUL terminated strings only.
- Instead of storing just pointers to the original objects, we
create local copies so the caller does not need to care about the
data any more.
- The standard implementation does not provide a way to update an
existing entry. This version will create a new entry or update an
existing one when both "action == ENTER" and "item.data != NULL".
- hsearch_r(): Instead of returning 1 on success, we return the
index into the internal hash table, which is also guaranteed to be
positive. This allows us direct access to the found hash table
slot for example for functions like hdelete().
* hdelete() [added]:
- The standard implementation of hsearch(3) does not provide any way
to delete any entries from the hash table. We extend the code to
do that.
* hexport() [added]:
- Export the data stored in the hash table in linearized form:
Entries are exported as "name=value" strings, separated by an
arbitrary (non-NUL, of course) separator character. This allows to
use this function both when formatting the U-Boot environment for
external storage (using '\0' as separator), but also when using it
for the "printenv" command to print all variables, simply by using
as '\n" as separator. This can also be used for new features like
exporting the environment data as text file, including the option
for later re-import.
- The entries in the result list will be sorted by ascending key
values.
* himport() [added]:
- Import linearized data into hash table. This is the inverse
function to hexport(): it takes a linear list of "name=value"
pairs and creates hash table entries from it.
- Entries without "value", i. e. consisting of only "name" or
"name=", will cause this entry to be deleted from the hash table.
- The "flag" argument can be used to control the behaviour: when
the H_NOCLEAR bit is set, then an existing hash table will kept,
i. e. new data will be added to an existing hash table;
otherwise, old data will be discarded and a new hash table will
be created.
- The separator character for the "name=value" pairs can be
selected, so we both support importing from externally stored
environment data (separated by NUL characters) and from plain text
files (entries separated by newline characters).
- To allow for nicely formatted text input, leading white space
(sequences of SPACE and TAB chars) is ignored, and entries
starting (after removal of any leading white space) with a '#'
character are considered comments and ignored.
- NOTE: this means that a variable name cannot start with a '#'
character.
- When using a non-NUL separator character, backslash is used as
escape character in the value part, allowing for example fo
multi-line values.
- In theory, arbitrary separator characters can be used, but only
'\0' and '\n' have really been tested.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# 080019b8 27-Feb-2020 Rasmus Villemoes <rasmus.villemoes@prevas.dk>

make env_entry::callback conditional on !CONFIG_SPL_BUILD

The callback member of struct env_entry is always NULL for an SPL
build. Removing it thus saves a bit of run-time memory in the
SPL (when CONFIG_SPL_ENV_SUPPORT=y) since struct env_entry is embedded
in struct env_entry_node - i.e. about 2KB for the normal case of
512+change hash table entries.

Two small fixups are needed for this, all other references to the
callback member are already under !CONFIG_SPL_BUILD: Don't initialize
.callback in set_flags() - hsearch_r doesn't use that value
anyway. And make env_callback_init() initialize ->callback to NULL for
a new entry instead of relying on an unused or deleted entry having
NULL in ->callback.

Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 34284970 27-Feb-2020 Rasmus Villemoes <rasmus.villemoes@prevas.dk>

lib/hashtable.c: don't test ->callback in SPL

In SPL, environment callbacks are not supported, so e->callback is
always NULL. Removing this makes the SPL a little smaller (about 400
bytes in my ppc build) with no functional change.

Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 7f529f65 27-Feb-2020 Rasmus Villemoes <rasmus.villemoes@prevas.dk>

lib/hashtable.c: create helper for calling env_entry::callback

This is preparation for compiling out the "call the callback" code and
associated error handling for SPL, where ->callback is always NULL.

Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
Reviewed-by: Simon Glass <sjg@chromium.org>

# 8bef79bf 14-Nov-2019 Simon Glass <sjg@chromium.org>

common: Move sorting functions to their own header file

These don't need to be in common.h so move them out into a new header.
Also add some missing comments.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@konsulko.com>

# 3f0d6807 01-Aug-2019 Simon Glass <sjg@chromium.org>

env: Drop the ACTION typedef

Avoid using a typedef here which is unnecessary. Add an 'env_' prefix to
both the enum and its members to make it clear that these are related to
the environment.

Add an ENV prefix to these two flags so that it is clear what they relate
to. Also move them to env.h since they are part of the public API. Use an
enum rather than a #define to tie them together.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 25e51e90 02-Aug-2019 Simon Glass <sjg@chromium.org>

env: Drop _ENTRY

This typedef does not need to be defined in the search.h header since it
is only used in one file (hashtable.c). Remove it from the header and
change it to a struct.

Signed-off-by: Simon Glass <sjg@chromium.org>

# dd2408ca 02-Aug-2019 Simon Glass <sjg@chromium.org>

env: Drop the ENTRY typedef

U-Boot is not supposed to use typedef for structs anymore. Also this name
is the same as the ENTRY() macro used in assembler files, and 'entry'
itself is widely used in U-Boot (>8k matches).

Drop the typedef and rename the struct to env_entry to reduce confusion.

Signed-off-by: Simon Glass <sjg@chromium.org>
Acked-by: Joe Hershberger <joe.hershberger@ni.com>

# 9dfdbd9f 30-Jan-2019 Roman Kapl <rka@sysgo.com>

hashtable: fix environment variable corruption

Only first previously deleted entry was recognized, leading hsearch_r
to think that there was no previously deleted entry. It then conluded
that a free entry was found, even if there were no free entries and it
overwrote a random entry.

This patch makes sure all deleted or free entries are always found and
also introduces constants for the 0 and -1 numbers. Unit tests to excersise a
simple hash table usage and catch the corruption were added.

To trash your environment, simply run this loop:

setenv i 0
while true; do
setenv v_$i $i
setenv v_$i
setexpr i $i + 1
done

Signed-off-by: Roman Kapl <rka@sysgo.com>

# 320194ae 23-Jan-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

hashtable: remove caps buffer

slre_match() checks if caps == NULL. In this case it does not try to
update it. So there is no need to create a buffer caps which we do not
evaluate.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 4bca3249 14-Dec-2018 AKASHI Takahiro <takahiro.akashi@linaro.org>

hashtable: fix length calculation in hexport_r

The commit below incorrectly fixed hexport_r();
> size = totlen + 1;
One extra byte is necessary to NULL-terminate a whole buffer, "resp."

Fixes: f1b20acb4a03 ("hashtable: Fix length calculation in hexport_r")
Signed-off-by: AKASHI Takahiro <takahiro.akashi@linaro.org>

# f1b20acb 17-Jul-2018 Zubair Lutfullah Kakakhel <zubair@resin.io>

hashtable: Fix length calculation in hexport_r

The length returned by hexport_r has a few redundant characters.
This appears as NULL characters at the end so seems harmless.

Remove the surplus counts in two places

totlen += strlen(ep->key) + 2;
I'm guessing the +2 here is for = and sep char. But there is another
totlen += 2; line that does that.

size = totletn + 1;
Doesn't make sense and isn't justified with any comment.

Signed-off-by: Zubair Lutfullah Kakakhel <zubair@resin.io>

# d9fc9077 09-Jul-2018 Quentin Schulz <quentin.schulz@bootlin.com>

hashtable: do not recreate whole hash table if vars are passed to himport_r

When vars are passed to the himport_r function with H_NOCLEAR flag,
those vars will be overridden in the current environment and if one of
those vars is not in the imported environment, it'll be deleted in the
current environment whatever the flag passed to himport_r.

The H_NOCLEAR flag is used to clear the whole environment whether vars
are passed to the function or not.

This leads to incoherent behaviour. If one passes vars to himport_r
with the H_NOCLEAR flag, if a var in vars is not in the imported env,
that var will be removed from the current env.

If one passes vars to himport_r without the H_NOCLEAR flag, the whole
environment will be removed and vars will be imported from the
environment in RAM.

It makes more sense to keep the variable that is in the current
environment but not in the imported environment if the H_NOCLEAR flag is
set and remove only that variable if the H_NOCLEAR flag is not set.

Let's clear the whole environment only if H_NOCLEAR and vars are not
passed to himport_r.

Let's remove variables that are in the current environment but not in
the imported env only if the H_NOCLEAR flag is not passed.

Suggested-by: Wolfgang Denk <wd@denx.de>
Signed-off-by: Quentin Schulz <quentin.schulz@bootlin.com>

# 83d290c5 06-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert all of our single license tags to Linux Kernel style

When U-Boot started using SPDX tags we were among the early adopters and
there weren't a lot of other examples to borrow from. So we picked the
area of the file that usually had a full license text and replaced it
with an appropriate SPDX-License-Identifier: entry. Since then, the
Linux Kernel has adopted SPDX tags and they place it as the very first
line in a file (except where shebangs are used, then it's second line)
and with slightly different comment styles than us.

In part due to community overlap, in part due to better tag visibility
and in part for other minor reasons, switch over to that style.

This commit changes all instances where we have a single declared
license in the tag as both the before and after are identical in tag
contents. There's also a few places where I found we did not have a tag
and have introduced one.

Signed-off-by: Tom Rini <trini@konsulko.com>

# d2d9bdfc 28-Sep-2016 B, Ravi <ravibabu@ti.com>

spl: saveenv: adding saveenv support in SPL

By default saveenv option is not supported for SPL. This patch
enable the support for save environment variable for SPL build.

Enable save environment support in SPL after setenv. By default
the saveenv option is not provided in SPL, but some boards need
this support in 'Falcon' boot, where SPL need to boot from
different images based on environment variable set by OS. For
example OS may set "reboot_image" environment variable to
"recovery" inorder to boot recovery image by SPL. The SPL read
"reboot_image" and act accordingly and change the reboot_image
to default mode using setenv and save the environemnt.

Signed-off-by: Ravi Babu <ravibabu@ti.com>
Reviewed-by: Simon Glass <sig@chromium.org>

change in v1:
- dropped SUPPORT, use CONFIG_SPL_SAVEENV
- updates the comments in mmc_private.h

# fc0b5948 07-Sep-2016 Robert P. J. Day <rpjday@crashcourse.ca>

Various, accumulated typos collected from around the tree.

Fix various misspellings of:

* deprecated
* partition
* preceding,preceded
* preparation
* its versus it's
* export
* existing
* scenario
* redundant
* remaining
* value
* architecture

Signed-off-by: Robert P. J. Day <rpjday@crashcourse.ca>
Reviewed-by: Jagan Teki <jteki@openedev.com>
Reviewed-by: Stefan Roese <sr@denx.de>

# c55d02b2 22-Jul-2016 Simon Glass <sjg@chromium.org>

hashtable: Fix compiler warning on 32-bit sandbox

This fixes a mismatch between the %zu format and the type used on sandbox.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@konsulko.com>

# 62a3b7dd 15-Jul-2016 Robert P. J. Day <rpjday@crashcourse.ca>

Various, unrelated tree-wide typo fixes.

Fix a number of typos, including:

* "compatble" -> "compatible"
* "eanbeld" -> "enabled"
* "envrionment" -> "environment"
* "FTD" -> "FDT" (for "flattened device tree")
* "ommitted" -> "omitted"
* "overriden" -> "overridden"
* "partiton" -> "partition"
* "propogate" -> "propagate"
* "resourse" -> "resource"
* "rest in piece" -> "rest in peace"
* "suport" -> "support"
* "varible" -> "variable"

Signed-off-by: Robert P. J. Day <rpjday@crashcourse.ca>

# 0226d878 13-Sep-2015 Lukasz Majewski <l.majewski@majess.pl>

env: import: hashtable: Free memory allocated before exiting from himport_r()

ithout this patch memory is not released on early exit.

Signed-off-by: Lukasz Majewski <l.majewski@majess.pl>

# 817e48d8 13-Sep-2015 Lukasz Majewski <l.majewski@majess.pl>

env: import: hashtable: Prevent buffer overrun when importing environment from file

Lets consider following scenario:
- One uses echo -n "key=value" to define environment variable in a file (single variable)
- The file content is "key=value" without any terminating byte (e.g. 0x0a or
0x0d).
- The file is loaded to u-boot non zero'ed RAM buffer (with load command).
- Then "env import -t -r $loadaddr $filesize" is executed.
- Due to lack of proper termination byte we have classical example of buffer
overrun.

This patch prevents from this by allocating one extra byte than size and
explicitly null terminate it.

There should be no change for normal env import operation after applying
this patch.

Signed-off-by: Lukasz Majewski <l.majewski@majess.pl>

# ecd1446f 14-Jul-2014 Alexander Holler <holler@ahsoftware.de>

Add option -r to env import to allow import of text files with CRLF as line endings

When this option is enabled, CRLF is treated like LF when importing environments
from text files, which means CRs ('\r') in front of LFs ('\n') are just ignored.

Drawback of enabling this option is that (maybe exported) variables which have
a trailing CR in their content will get imported without that CR. But this
drawback is very unlikely and the big advantage of letting Windows user create
a *working* uEnv.txt too is likely more welcome.

Signed-off-by: Alexander Holler <holler@ahsoftware.de>

# 9a832331 08-Oct-2013 Pierre Aubert <p.aubert@staubli.com>

env: fix the env export varname

The env export command doesn't export the first variable of the list
since commit 5a31ea04c9ee5544fbb70ad7597ea4b294840eab
"env grep" - reimplement command using hexport_r()

Signed-off-by: Pierre Aubert <p.aubert@staubli.com>

# 1bce2aeb 16-Sep-2013 Robert P. J. Day <rpjday@crashcourse.ca>

Cosmetic: Fix a number of typos, no functional changes.

Fix various misspellings of things like "environment", "kernel",
"default" and "volatile", and throw in a couple grammar fixes.

Signed-off-by: Robert P. J. Day <rpjday@crashcourse.ca>

# eee479cf 08-Jul-2013 Wolfgang Denk <wd@denx.de>

Add LGPL-2.1+ SPDX-License-Identifier to source files

Signed-off-by: Wolfgang Denk <wd@denx.de>

# e4fdcadd 28-Apr-2013 Lucian Cojocar <cojocar@gmail.com>

env: throw an error when an empty key is used

If the environment contains an entry like "=value" "\0" we should throw
an error when parsing the environment. Otherwise, U-Boot will enter in
an infinite loop.

Signed-off-by: Lucian Cojocar <cojocar@gmail.com>

# be29df6a 23-Mar-2013 Wolfgang Denk <wd@denx.de>

"env grep" - add support for regular expression matches

When CONFIG_REGEX is enabled, the new option "-e" becomes available
which causes regular expression matches to be used. This allows for
example things like these:

- print all MAC addresses:

=> env grep -e eth.*addr
eth1addr=00:10:ec:80:c5:15
ethaddr=00:10:ec:00:c5:15

- print all variables that have at least 2 colons in their value:

=> env grep -v -e :.*:
addip=setenv bootargs ${bootargs} ip=${ipaddr}:${serverip}:${gatewayip}:${netmask}:${hostname}:${netdev}:off
panic=1
eth1addr=00:10:ec:80:c5:15
ethaddr=00:10:ec:00:c5:15
ver=U-Boot 2013.04-rc1-00289-g497746b-dirty (Mar 22 2013 - 12:50:25)

etc.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# 5a31ea04 23-Mar-2013 Wolfgang Denk <wd@denx.de>

"env grep" - reimplement command using hexport_r()

Also drop hstrstr_r() which is not needed any more.
The new code is way more flexible.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# ea009d47 23-Mar-2013 Wolfgang Denk <wd@denx.de>

hashtable: preparations to use hexport_r() for "env grep"

The output of "env grep" is unsorted, and printing is done by a
private implementation to parse the hash table. We have all the
needed code in place in hexport_r() alsready, so let's use this
instead. Here we prepare the code for this, without any functional
changes yet.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# 2598090b 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Add environment variable flags

Currently just validates variable types as decimal, hexidecimal,
boolean, ip address, and mac address.

If the entry is not found in the env ".flags", then look in the static
one. This allows the env to override the static definitions, but prevents
the need to have every definition in the environment distracting you.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# 170ab110 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Add support for callbacks to environment vars

Add support for per-variable callbacks to the "hashtable" functions.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

!!!fix comment in callback

# be11235a 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Hide '.' variables in env print by default

When printing all variables with env print, don't print variables that
begin with '.'. If env print is called with a '-a' switch, then
include variables that begin with '.' (just like the ls command).

Variables printed explicitly will be printed even without the -a.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# 7afcf3a5 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Refactor apply into change_ok

Move the read of the old value to inside the check function. In some
cases it can be avoided all together and at the least the code is only
called from one place.

Also name the function and the callback to more clearly describe what
it does.

Pass the ENTRY instead of just the name for direct access to the whole
data structure.

Pass an enum to the callback that specifies the operation being approved.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# 3d3b52f2 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Consolidate common code in hsearch_r()

The same chunk of code was replicated in two places and the following
changes will make that chunk grow a bit, so combine into a static func.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# c4e0057f 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Refactor do_apply to a flag

Use a flag in hsearch_r for insert mode passed from import to allow the
behavior be different based on use.

Now that "do_check" is called for all imports, ensure console init is
complete before updating the console on relocation import

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# 7ac2fe2d 17-Sep-2012 Ilya Yanok <ilya.yanok@cogentembedded.com>

OMAP: networking support for SPL

This patch adds support for networking in SPL. Some devices are
capable of loading SPL via network so it makes sense to load the
main U-Boot binary via network too. This patch tries to use
existing network code as much as possible. Unfortunately, it depends
on environment which in turn depends on other code so SPL size
is increased significantly. No effort was done to decouple network
code and environment so far.

Signed-off-by: Ilya Yanok <ilya.yanok@cogentembedded.com>
Acked-by: Joe Hershberger <joe.hershberger@ni.com>
Signed-off-by: Tom Rini <trini@ti.com>

# d5370feb 26-Aug-2012 Gerlando Falauto <gerlando.falauto@keymile.com>

env: delete selected vars not present in imported env

When variables explicitly specified on the command line are not present
in the imported env, delete them from the running env.
If the variable is also missing from the running env, issue a warning.

Signed-off-by: Gerlando Falauto <gerlando.falauto@keymile.com>
Reviewed-by: Marek Vasut <marex@denx.de>

# 152874b6 23-Aug-2012 Gerlando Falauto <gerlando.falauto@keymile.com>

env: check and apply changes on delete/destroy

Signed-off-by: Gerlando Falauto <gerlando.falauto@keymile.com>
Reviewed-by: Marek Vasut <marex@denx.de>

# c5983592 23-Aug-2012 Gerlando Falauto <gerlando.falauto@keymile.com>

env: add check/apply logic to himport_r()

Change hashtable so that a callback function will decide whether a
variable can be overwritten, and possibly apply the changes.

So add a new field to struct hsearch_data:

o "apply" callback function to check whether a variable can be
overwritten, and possibly immediately apply the changes;
when NULL, no check is performed.

And a new argument to himport_r():
o "do_apply": whether to call the apply callback function

NOTE: This patch does not change the current behavior.

Signed-off-by: Gerlando Falauto <gerlando.falauto@keymile.com>
Reviewed-by: Marek Vasut <marex@denx.de>

# 348b1f1c 23-Aug-2012 Gerlando Falauto <gerlando.falauto@keymile.com>

env: make himport_r() selective on variables

Add 2 new arguments to himport_r():

o "nvars", "vars": number and list of variables to take into account
(0 means ALL)

NOTE: This patch does not change the current behaviour.

Signed-off-by: Gerlando Falauto <gerlando.falauto@keymile.com>
Reviewed-by: Marek Vasut <marex@denx.de>

# ff856286 21-Nov-2011 Simon Glass <sjg@chromium.org>

sandbox: Fix warnings in hashtable.c

This fixes a few printf() strings for size_t which are missing the 'z'
modifier.

Signed-off-by: Simon Glass <sjg@chromium.org>
Acked-by: Mike Frysinger <vapier@gentoo.org>

# 37f2fe74 06-Nov-2011 Wolfgang Denk <wd@denx.de>

env: allow to export only selected variables

New syntax:
env export [-t | -b | -c] [-s size] addr [var ...]

With this change it is possible to provide a list of variables names
that shall be exported. Whenno arguments are given, the whole
environment gets exported.

NOTE: The new handling of the "size" argument means a change to the
user API.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# 4d91a6ec 23-Aug-2011 Jason Hobbs <jason.hobbs@calxeda.com>

Replace space and tab checks with isblank

These are various places I found that checked for conditions equivalent
to isblank.

Signed-off-by: Jason Hobbs <jason.hobbs@calxeda.com>

# 84b5e802 29-Jul-2011 Wolfgang Denk <wd@denx.de>

Constify getenv(), setenv() and hash code functions

This is needed to get rid of build warnings like

main.c:311: warning: passing argument 2 of 'setenv' discards qualifiers from pointer target type

which result from commit 09c2e90 "unify version_string".

Signed-off-by: Wolfgang Denk <wd@denx.de>
Cc: Andreas Bie�mann <andreas.devel@googlemail.com>

# a000b795 05-Apr-2011 Kim Phillips <kim.phillips@freescale.com>

common: add a grepenv command

u-boot environments, esp. when boards are shared across multiple
users, can get pretty large and time consuming to visually parse.
The grepenv command this patch adds can be used in lieu of printenv
to facilitate searching. grepenv works like printenv but limits
its output only to environment strings (variable name and value
pairs) that match the user specified substring.

the following examples are on a board with a 5313 byte environment
that spans multiple screen pages:

Example 1: summarize ethernet configuration:

=> grepenv eth TSEC
etact=FM1@DTSEC2
eth=FM1@DTSEC4
ethact=FM1@DTSEC2
eth1addr=00:E0:0C:00:8b:01
eth2addr=00:E0:0C:00:8b:02
eth3addr=00:E0:0C:00:8b:03
eth4addr=00:E0:0C:00:8b:04
eth5addr=00:E0:0C:00:8b:05
eth6addr=00:E0:0C:00:8b:06
eth7addr=00:E0:0C:00:8b:07
eth8addr=00:E0:0C:00:8b:08
eth9addr=00:E0:0C:00:8b:09
ethaddr=00:E0:0C:00:8b:00
netdev=eth0
uprcw=setenv ethact $eth;setenv filename p4080ds/R_PPSXX_0xe/rcw_0xe_2sgmii_rev2_high.bin;setenv start 0xe8000000;protect off all;run upimage;protect on all
upuboot=setenv ethact $eth;setenv filename u-boot.bin;setenv start eff80000;protect off all;run upimage;protect on all
upucode=setenv ethact $eth;setenv filename fsl_fman_ucode_P4080_101_6.bin;setenv start 0xef000000;protect off all;run upimage;protect on all
usdboot=setenv ethact $eth;tftp 1000000 $dir/$bootfile;tftp 2000000 $dir/initramfs.cpio.gz.uboot;tftp c00000 $dir/p4080ds-usdpaa.dtb;setenv bootargs root=/dev/ram rw console=ttyS0,115200 $othbootargs;bootm 1000000 2000000 c00000;
=>

Example 2: detect unused env vars:

=> grepenv etact
etact=FM1@DTSEC2
=>

Example 3: reveal hardcoded variables; e.g., for fdtaddr:

=> grepenv fdtaddr
fdtaddr=c00000
nfsboot=setenv bootargs root=/dev/nfs rw nfsroot=$serverip:$rootpath ip=$ipaddr:$serverip:$gatewayip:$netmask:$hostname:$netdev:off console=$consoledev,$baudrate $othbootargs;tftp $loadaddr $bootfile;tftp $fdtaddr $fdtfile;bootm $loadaddr - $fdtaddr
ramboot=setenv bootargs root=/dev/ram rw console=$consoledev,$baudrate $othbootargs;tftp $ramdiskaddr $ramdiskfile;tftp $loadaddr $bootfile;tftp $fdtaddr $fdtfile;bootm $loadaddr $ramdiskaddr $fdtaddr
=> grep $fdtaddr
fdtaddr=c00000
my_boot=bootm 0x40000000 0x41000000 0x00c00000
my_dtb=tftp 0x00c00000 $prefix/p4080ds.dtb
nohvboot=tftp 1000000 $dir/$bootfile;tftp 2000000 $dir/$ramdiskfile;tftp c00000 $dir/$fdtfile;setenv bootargs root=/dev/ram rw ramdisk_size=0x10000000 console=ttyS0,115200;bootm 1000000 2000000 c00000;
=>

This patch also enables the grepenv command by default on
corenet_ds based boards (and repositions the DHCP command
entry to keep the list sorted).

Signed-off-by: Kim Phillips <kim.phillips@freescale.com>
Cc: Kumar Gala <kumar.gala@freescale.com>
Cc: Andy Fleming <afleming@freescale.com>

# af4d9074 04-Apr-2011 Kim Phillips <kim.phillips@freescale.com>

env: fix env var autocompletion

commit 560d424b6d7cd4205b062ad95f1b104bd4f8bcc3 "env: re-add
support for auto-completion" fell short of its description -
the 'used' logic in hmatch_r was reversed - 'used' is 0 if
the hash table entry is not used, or -1 if deleted. This
patch makes hmatch_r actually match on valid ('used') entries,
instead of skipping them and failing to match anything.

typing 'printenv tft' and hitting 'tab' now displays valid
choices for variable names.

Signed-off-by: Kim Phillips <kim.phillips@freescale.com>
Cc: Mike Frysinger <vapier@gentoo.org>
Tested-by: Mike Frysinger <vapier@gentoo.org>

# c81c1222 21-Mar-2011 Peter Barada <peter.barada@logicpd.com>

Fix hash table deletion to prevent lost entries

Use negative used value to mark deleted entry. Search keeps probing
past deleted entries. Adding an entry uses first deleted entry when
it hits end of probe chain.

Initially found that "ramdiskimage" and "preboot" collide modulus 347,
causing "preboot" to be inserted at idx 190, "ramdiskimage" at idx 191.
Previous to this fix when "preboot" is deleted, "ramdiskimage" is
orphaned.

Signed-off-by: Peter Barada <peter.barada@logicpd.com>
Tested-by: Wolfgang Denk <wd@denx.de>

# 560d424b 17-Dec-2010 Mike Frysinger <vapier@gentoo.org>

env: re-add support for auto-completion

Currently, only basic completion is supported (no globs), but this is
what we had previously.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 2eb1573f 08-Dec-2010 Mike Frysinger <vapier@gentoo.org>

hashtable: drop all non-reentrant versions

The non-reentrant versions of the hashtable functions operate on a single
shared hashtable. So if two different people try using these funcs for
two different purposes, they'll cause problems for the other.

Avoid this by converting all existing hashtable consumers over to the
reentrant versions and then punting the non-reentrant ones.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 071bc923 27-Oct-2010 Wolfgang Denk <wd@denx.de>

Coding Style cleanup

Signed-off-by: Wolfgang Denk <wd@denx.de>

# fc5fc76b 01-Oct-2010 Andreas Bießmann <andreas.devel@googlemail.com>

lib/hashtable.c: add CONFIG_ENV_MIN_ENTRIES

This patch adds a new config parameter for adjusting the calculation of
hash table size when importing a buffer.

When importing a extremely small buffer (e.g. the default_environment)
the old calculation generated a hash table which could hold at most the
buffer content but no more entires.

The new calculation add a fixed number of entries to the result to fit
better for small import buffers. This amount may be configured by the
user in board file to adjust the behaviour.

Signed-off-by: Andreas Bie�mann <andreas.devel@googlemail.com>

# ea882baf 20-Jun-2010 Wolfgang Denk <wd@denx.de>

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>

# a6826fbc 20-Jun-2010 Wolfgang Denk <wd@denx.de>

Add hash table support as base for new environment code

This implementation is based on code from uClibc-0.9.30.3 but was
modified and extended for use within U-Boot.

Major modifications and extensions:

* hsearch() [modified / extended]:
- While the standard version does not make any assumptions about
the type of the stored data objects at all, this implementation
works with NUL terminated strings only.
- Instead of storing just pointers to the original objects, we
create local copies so the caller does not need to care about the
data any more.
- The standard implementation does not provide a way to update an
existing entry. This version will create a new entry or update an
existing one when both "action == ENTER" and "item.data != NULL".
- hsearch_r(): Instead of returning 1 on success, we return the
index into the internal hash table, which is also guaranteed to be
positive. This allows us direct access to the found hash table
slot for example for functions like hdelete().
* hdelete() [added]:
- The standard implementation of hsearch(3) does not provide any way
to delete any entries from the hash table. We extend the code to
do that.
* hexport() [added]:
- Export the data stored in the hash table in linearized form:
Entries are exported as "name=value" strings, separated by an
arbitrary (non-NUL, of course) separator character. This allows to
use this function both when formatting the U-Boot environment for
external storage (using '\0' as separator), but also when using it
for the "printenv" command to print all variables, simply by using
as '\n" as separator. This can also be used for new features like
exporting the environment data as text file, including the option
for later re-import.
- The entries in the result list will be sorted by ascending key
values.
* himport() [added]:
- Import linearized data into hash table. This is the inverse
function to hexport(): it takes a linear list of "name=value"
pairs and creates hash table entries from it.
- Entries without "value", i. e. consisting of only "name" or
"name=", will cause this entry to be deleted from the hash table.
- The "flag" argument can be used to control the behaviour: when
the H_NOCLEAR bit is set, then an existing hash table will kept,
i. e. new data will be added to an existing hash table;
otherwise, old data will be discarded and a new hash table will
be created.
- The separator character for the "name=value" pairs can be
selected, so we both support importing from externally stored
environment data (separated by NUL characters) and from plain text
files (entries separated by newline characters).
- To allow for nicely formatted text input, leading white space
(sequences of SPACE and TAB chars) is ignored, and entries
starting (after removal of any leading white space) with a '#'
character are considered comments and ignored.
- NOTE: this means that a variable name cannot start with a '#'
character.
- When using a non-NUL separator character, backslash is used as
escape character in the value part, allowing for example fo
multi-line values.
- In theory, arbitrary separator characters can be used, but only
'\0' and '\n' have really been tested.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# 8bef79bf 14-Nov-2019 Simon Glass <sjg@chromium.org>

common: Move sorting functions to their own header file

These don't need to be in common.h so move them out into a new header.
Also add some missing comments.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@konsulko.com>

# 3f0d6807 01-Aug-2019 Simon Glass <sjg@chromium.org>

env: Drop the ACTION typedef

Avoid using a typedef here which is unnecessary. Add an 'env_' prefix to
both the enum and its members to make it clear that these are related to
the environment.

Add an ENV prefix to these two flags so that it is clear what they relate
to. Also move them to env.h since they are part of the public API. Use an
enum rather than a #define to tie them together.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 25e51e90 02-Aug-2019 Simon Glass <sjg@chromium.org>

env: Drop _ENTRY

This typedef does not need to be defined in the search.h header since it
is only used in one file (hashtable.c). Remove it from the header and
change it to a struct.

Signed-off-by: Simon Glass <sjg@chromium.org>

# dd2408ca 02-Aug-2019 Simon Glass <sjg@chromium.org>

env: Drop the ENTRY typedef

U-Boot is not supposed to use typedef for structs anymore. Also this name
is the same as the ENTRY() macro used in assembler files, and 'entry'
itself is widely used in U-Boot (>8k matches).

Drop the typedef and rename the struct to env_entry to reduce confusion.

Signed-off-by: Simon Glass <sjg@chromium.org>
Acked-by: Joe Hershberger <joe.hershberger@ni.com>

# 9dfdbd9f 30-Jan-2019 Roman Kapl <rka@sysgo.com>

hashtable: fix environment variable corruption

Only first previously deleted entry was recognized, leading hsearch_r
to think that there was no previously deleted entry. It then conluded
that a free entry was found, even if there were no free entries and it
overwrote a random entry.

This patch makes sure all deleted or free entries are always found and
also introduces constants for the 0 and -1 numbers. Unit tests to excersise a
simple hash table usage and catch the corruption were added.

To trash your environment, simply run this loop:

setenv i 0
while true; do
setenv v_$i $i
setenv v_$i
setexpr i $i + 1
done

Signed-off-by: Roman Kapl <rka@sysgo.com>

# 320194ae 23-Jan-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

hashtable: remove caps buffer

slre_match() checks if caps == NULL. In this case it does not try to
update it. So there is no need to create a buffer caps which we do not
evaluate.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 4bca3249 14-Dec-2018 AKASHI Takahiro <takahiro.akashi@linaro.org>

hashtable: fix length calculation in hexport_r

The commit below incorrectly fixed hexport_r();
> size = totlen + 1;
One extra byte is necessary to NULL-terminate a whole buffer, "resp."

Fixes: f1b20acb4a03 ("hashtable: Fix length calculation in hexport_r")
Signed-off-by: AKASHI Takahiro <takahiro.akashi@linaro.org>

# f1b20acb 17-Jul-2018 Zubair Lutfullah Kakakhel <zubair@resin.io>

hashtable: Fix length calculation in hexport_r

The length returned by hexport_r has a few redundant characters.
This appears as NULL characters at the end so seems harmless.

Remove the surplus counts in two places

totlen += strlen(ep->key) + 2;
I'm guessing the +2 here is for = and sep char. But there is another
totlen += 2; line that does that.

size = totletn + 1;
Doesn't make sense and isn't justified with any comment.

Signed-off-by: Zubair Lutfullah Kakakhel <zubair@resin.io>

# d9fc9077 09-Jul-2018 Quentin Schulz <quentin.schulz@bootlin.com>

hashtable: do not recreate whole hash table if vars are passed to himport_r

When vars are passed to the himport_r function with H_NOCLEAR flag,
those vars will be overridden in the current environment and if one of
those vars is not in the imported environment, it'll be deleted in the
current environment whatever the flag passed to himport_r.

The H_NOCLEAR flag is used to clear the whole environment whether vars
are passed to the function or not.

This leads to incoherent behaviour. If one passes vars to himport_r
with the H_NOCLEAR flag, if a var in vars is not in the imported env,
that var will be removed from the current env.

If one passes vars to himport_r without the H_NOCLEAR flag, the whole
environment will be removed and vars will be imported from the
environment in RAM.

It makes more sense to keep the variable that is in the current
environment but not in the imported environment if the H_NOCLEAR flag is
set and remove only that variable if the H_NOCLEAR flag is not set.

Let's clear the whole environment only if H_NOCLEAR and vars are not
passed to himport_r.

Let's remove variables that are in the current environment but not in
the imported env only if the H_NOCLEAR flag is not passed.

Suggested-by: Wolfgang Denk <wd@denx.de>
Signed-off-by: Quentin Schulz <quentin.schulz@bootlin.com>

# 83d290c5 06-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert all of our single license tags to Linux Kernel style

When U-Boot started using SPDX tags we were among the early adopters and
there weren't a lot of other examples to borrow from. So we picked the
area of the file that usually had a full license text and replaced it
with an appropriate SPDX-License-Identifier: entry. Since then, the
Linux Kernel has adopted SPDX tags and they place it as the very first
line in a file (except where shebangs are used, then it's second line)
and with slightly different comment styles than us.

In part due to community overlap, in part due to better tag visibility
and in part for other minor reasons, switch over to that style.

This commit changes all instances where we have a single declared
license in the tag as both the before and after are identical in tag
contents. There's also a few places where I found we did not have a tag
and have introduced one.

Signed-off-by: Tom Rini <trini@konsulko.com>

# d2d9bdfc 28-Sep-2016 B, Ravi <ravibabu@ti.com>

spl: saveenv: adding saveenv support in SPL

By default saveenv option is not supported for SPL. This patch
enable the support for save environment variable for SPL build.

Enable save environment support in SPL after setenv. By default
the saveenv option is not provided in SPL, but some boards need
this support in 'Falcon' boot, where SPL need to boot from
different images based on environment variable set by OS. For
example OS may set "reboot_image" environment variable to
"recovery" inorder to boot recovery image by SPL. The SPL read
"reboot_image" and act accordingly and change the reboot_image
to default mode using setenv and save the environemnt.

Signed-off-by: Ravi Babu <ravibabu@ti.com>
Reviewed-by: Simon Glass <sig@chromium.org>

change in v1:
- dropped SUPPORT, use CONFIG_SPL_SAVEENV
- updates the comments in mmc_private.h

# fc0b5948 07-Sep-2016 Robert P. J. Day <rpjday@crashcourse.ca>

Various, accumulated typos collected from around the tree.

Fix various misspellings of:

* deprecated
* partition
* preceding,preceded
* preparation
* its versus it's
* export
* existing
* scenario
* redundant
* remaining
* value
* architecture

Signed-off-by: Robert P. J. Day <rpjday@crashcourse.ca>
Reviewed-by: Jagan Teki <jteki@openedev.com>
Reviewed-by: Stefan Roese <sr@denx.de>

# c55d02b2 22-Jul-2016 Simon Glass <sjg@chromium.org>

hashtable: Fix compiler warning on 32-bit sandbox

This fixes a mismatch between the %zu format and the type used on sandbox.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@konsulko.com>

# 62a3b7dd 15-Jul-2016 Robert P. J. Day <rpjday@crashcourse.ca>

Various, unrelated tree-wide typo fixes.

Fix a number of typos, including:

* "compatble" -> "compatible"
* "eanbeld" -> "enabled"
* "envrionment" -> "environment"
* "FTD" -> "FDT" (for "flattened device tree")
* "ommitted" -> "omitted"
* "overriden" -> "overridden"
* "partiton" -> "partition"
* "propogate" -> "propagate"
* "resourse" -> "resource"
* "rest in piece" -> "rest in peace"
* "suport" -> "support"
* "varible" -> "variable"

Signed-off-by: Robert P. J. Day <rpjday@crashcourse.ca>

# 0226d878 13-Sep-2015 Lukasz Majewski <l.majewski@majess.pl>

env: import: hashtable: Free memory allocated before exiting from himport_r()

ithout this patch memory is not released on early exit.

Signed-off-by: Lukasz Majewski <l.majewski@majess.pl>

# 817e48d8 13-Sep-2015 Lukasz Majewski <l.majewski@majess.pl>

env: import: hashtable: Prevent buffer overrun when importing environment from file

Lets consider following scenario:
- One uses echo -n "key=value" to define environment variable in a file (single variable)
- The file content is "key=value" without any terminating byte (e.g. 0x0a or
0x0d).
- The file is loaded to u-boot non zero'ed RAM buffer (with load command).
- Then "env import -t -r $loadaddr $filesize" is executed.
- Due to lack of proper termination byte we have classical example of buffer
overrun.

This patch prevents from this by allocating one extra byte than size and
explicitly null terminate it.

There should be no change for normal env import operation after applying
this patch.

Signed-off-by: Lukasz Majewski <l.majewski@majess.pl>

# ecd1446f 14-Jul-2014 Alexander Holler <holler@ahsoftware.de>

Add option -r to env import to allow import of text files with CRLF as line endings

When this option is enabled, CRLF is treated like LF when importing environments
from text files, which means CRs ('\r') in front of LFs ('\n') are just ignored.

Drawback of enabling this option is that (maybe exported) variables which have
a trailing CR in their content will get imported without that CR. But this
drawback is very unlikely and the big advantage of letting Windows user create
a *working* uEnv.txt too is likely more welcome.

Signed-off-by: Alexander Holler <holler@ahsoftware.de>

# 9a832331 08-Oct-2013 Pierre Aubert <p.aubert@staubli.com>

env: fix the env export varname

The env export command doesn't export the first variable of the list
since commit 5a31ea04c9ee5544fbb70ad7597ea4b294840eab
"env grep" - reimplement command using hexport_r()

Signed-off-by: Pierre Aubert <p.aubert@staubli.com>

# 1bce2aeb 16-Sep-2013 Robert P. J. Day <rpjday@crashcourse.ca>

Cosmetic: Fix a number of typos, no functional changes.

Fix various misspellings of things like "environment", "kernel",
"default" and "volatile", and throw in a couple grammar fixes.

Signed-off-by: Robert P. J. Day <rpjday@crashcourse.ca>

# eee479cf 08-Jul-2013 Wolfgang Denk <wd@denx.de>

Add LGPL-2.1+ SPDX-License-Identifier to source files

Signed-off-by: Wolfgang Denk <wd@denx.de>

# e4fdcadd 28-Apr-2013 Lucian Cojocar <cojocar@gmail.com>

env: throw an error when an empty key is used

If the environment contains an entry like "=value" "\0" we should throw
an error when parsing the environment. Otherwise, U-Boot will enter in
an infinite loop.

Signed-off-by: Lucian Cojocar <cojocar@gmail.com>

# be29df6a 23-Mar-2013 Wolfgang Denk <wd@denx.de>

"env grep" - add support for regular expression matches

When CONFIG_REGEX is enabled, the new option "-e" becomes available
which causes regular expression matches to be used. This allows for
example things like these:

- print all MAC addresses:

=> env grep -e eth.*addr
eth1addr=00:10:ec:80:c5:15
ethaddr=00:10:ec:00:c5:15

- print all variables that have at least 2 colons in their value:

=> env grep -v -e :.*:
addip=setenv bootargs ${bootargs} ip=${ipaddr}:${serverip}:${gatewayip}:${netmask}:${hostname}:${netdev}:off
panic=1
eth1addr=00:10:ec:80:c5:15
ethaddr=00:10:ec:00:c5:15
ver=U-Boot 2013.04-rc1-00289-g497746b-dirty (Mar 22 2013 - 12:50:25)

etc.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# 5a31ea04 23-Mar-2013 Wolfgang Denk <wd@denx.de>

"env grep" - reimplement command using hexport_r()

Also drop hstrstr_r() which is not needed any more.
The new code is way more flexible.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# ea009d47 23-Mar-2013 Wolfgang Denk <wd@denx.de>

hashtable: preparations to use hexport_r() for "env grep"

The output of "env grep" is unsorted, and printing is done by a
private implementation to parse the hash table. We have all the
needed code in place in hexport_r() alsready, so let's use this
instead. Here we prepare the code for this, without any functional
changes yet.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# 2598090b 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Add environment variable flags

Currently just validates variable types as decimal, hexidecimal,
boolean, ip address, and mac address.

If the entry is not found in the env ".flags", then look in the static
one. This allows the env to override the static definitions, but prevents
the need to have every definition in the environment distracting you.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# 170ab110 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Add support for callbacks to environment vars

Add support for per-variable callbacks to the "hashtable" functions.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

!!!fix comment in callback

# be11235a 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Hide '.' variables in env print by default

When printing all variables with env print, don't print variables that
begin with '.'. If env print is called with a '-a' switch, then
include variables that begin with '.' (just like the ls command).

Variables printed explicitly will be printed even without the -a.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# 7afcf3a5 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Refactor apply into change_ok

Move the read of the old value to inside the check function. In some
cases it can be avoided all together and at the least the code is only
called from one place.

Also name the function and the callback to more clearly describe what
it does.

Pass the ENTRY instead of just the name for direct access to the whole
data structure.

Pass an enum to the callback that specifies the operation being approved.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# 3d3b52f2 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Consolidate common code in hsearch_r()

The same chunk of code was replicated in two places and the following
changes will make that chunk grow a bit, so combine into a static func.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# c4e0057f 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Refactor do_apply to a flag

Use a flag in hsearch_r for insert mode passed from import to allow the
behavior be different based on use.

Now that "do_check" is called for all imports, ensure console init is
complete before updating the console on relocation import

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# 7ac2fe2d 17-Sep-2012 Ilya Yanok <ilya.yanok@cogentembedded.com>

OMAP: networking support for SPL

This patch adds support for networking in SPL. Some devices are
capable of loading SPL via network so it makes sense to load the
main U-Boot binary via network too. This patch tries to use
existing network code as much as possible. Unfortunately, it depends
on environment which in turn depends on other code so SPL size
is increased significantly. No effort was done to decouple network
code and environment so far.

Signed-off-by: Ilya Yanok <ilya.yanok@cogentembedded.com>
Acked-by: Joe Hershberger <joe.hershberger@ni.com>
Signed-off-by: Tom Rini <trini@ti.com>

# d5370feb 26-Aug-2012 Gerlando Falauto <gerlando.falauto@keymile.com>

env: delete selected vars not present in imported env

When variables explicitly specified on the command line are not present
in the imported env, delete them from the running env.
If the variable is also missing from the running env, issue a warning.

Signed-off-by: Gerlando Falauto <gerlando.falauto@keymile.com>
Reviewed-by: Marek Vasut <marex@denx.de>

# 152874b6 23-Aug-2012 Gerlando Falauto <gerlando.falauto@keymile.com>

env: check and apply changes on delete/destroy

Signed-off-by: Gerlando Falauto <gerlando.falauto@keymile.com>
Reviewed-by: Marek Vasut <marex@denx.de>

# c5983592 23-Aug-2012 Gerlando Falauto <gerlando.falauto@keymile.com>

env: add check/apply logic to himport_r()

Change hashtable so that a callback function will decide whether a
variable can be overwritten, and possibly apply the changes.

So add a new field to struct hsearch_data:

o "apply" callback function to check whether a variable can be
overwritten, and possibly immediately apply the changes;
when NULL, no check is performed.

And a new argument to himport_r():
o "do_apply": whether to call the apply callback function

NOTE: This patch does not change the current behavior.

Signed-off-by: Gerlando Falauto <gerlando.falauto@keymile.com>
Reviewed-by: Marek Vasut <marex@denx.de>

# 348b1f1c 23-Aug-2012 Gerlando Falauto <gerlando.falauto@keymile.com>

env: make himport_r() selective on variables

Add 2 new arguments to himport_r():

o "nvars", "vars": number and list of variables to take into account
(0 means ALL)

NOTE: This patch does not change the current behaviour.

Signed-off-by: Gerlando Falauto <gerlando.falauto@keymile.com>
Reviewed-by: Marek Vasut <marex@denx.de>

# ff856286 21-Nov-2011 Simon Glass <sjg@chromium.org>

sandbox: Fix warnings in hashtable.c

This fixes a few printf() strings for size_t which are missing the 'z'
modifier.

Signed-off-by: Simon Glass <sjg@chromium.org>
Acked-by: Mike Frysinger <vapier@gentoo.org>

# 37f2fe74 06-Nov-2011 Wolfgang Denk <wd@denx.de>

env: allow to export only selected variables

New syntax:
env export [-t | -b | -c] [-s size] addr [var ...]

With this change it is possible to provide a list of variables names
that shall be exported. Whenno arguments are given, the whole
environment gets exported.

NOTE: The new handling of the "size" argument means a change to the
user API.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# 4d91a6ec 23-Aug-2011 Jason Hobbs <jason.hobbs@calxeda.com>

Replace space and tab checks with isblank

These are various places I found that checked for conditions equivalent
to isblank.

Signed-off-by: Jason Hobbs <jason.hobbs@calxeda.com>

# 84b5e802 29-Jul-2011 Wolfgang Denk <wd@denx.de>

Constify getenv(), setenv() and hash code functions

This is needed to get rid of build warnings like

main.c:311: warning: passing argument 2 of 'setenv' discards qualifiers from pointer target type

which result from commit 09c2e90 "unify version_string".

Signed-off-by: Wolfgang Denk <wd@denx.de>
Cc: Andreas Bie�mann <andreas.devel@googlemail.com>

# a000b795 05-Apr-2011 Kim Phillips <kim.phillips@freescale.com>

common: add a grepenv command

u-boot environments, esp. when boards are shared across multiple
users, can get pretty large and time consuming to visually parse.
The grepenv command this patch adds can be used in lieu of printenv
to facilitate searching. grepenv works like printenv but limits
its output only to environment strings (variable name and value
pairs) that match the user specified substring.

the following examples are on a board with a 5313 byte environment
that spans multiple screen pages:

Example 1: summarize ethernet configuration:

=> grepenv eth TSEC
etact=FM1@DTSEC2
eth=FM1@DTSEC4
ethact=FM1@DTSEC2
eth1addr=00:E0:0C:00:8b:01
eth2addr=00:E0:0C:00:8b:02
eth3addr=00:E0:0C:00:8b:03
eth4addr=00:E0:0C:00:8b:04
eth5addr=00:E0:0C:00:8b:05
eth6addr=00:E0:0C:00:8b:06
eth7addr=00:E0:0C:00:8b:07
eth8addr=00:E0:0C:00:8b:08
eth9addr=00:E0:0C:00:8b:09
ethaddr=00:E0:0C:00:8b:00
netdev=eth0
uprcw=setenv ethact $eth;setenv filename p4080ds/R_PPSXX_0xe/rcw_0xe_2sgmii_rev2_high.bin;setenv start 0xe8000000;protect off all;run upimage;protect on all
upuboot=setenv ethact $eth;setenv filename u-boot.bin;setenv start eff80000;protect off all;run upimage;protect on all
upucode=setenv ethact $eth;setenv filename fsl_fman_ucode_P4080_101_6.bin;setenv start 0xef000000;protect off all;run upimage;protect on all
usdboot=setenv ethact $eth;tftp 1000000 $dir/$bootfile;tftp 2000000 $dir/initramfs.cpio.gz.uboot;tftp c00000 $dir/p4080ds-usdpaa.dtb;setenv bootargs root=/dev/ram rw console=ttyS0,115200 $othbootargs;bootm 1000000 2000000 c00000;
=>

Example 2: detect unused env vars:

=> grepenv etact
etact=FM1@DTSEC2
=>

Example 3: reveal hardcoded variables; e.g., for fdtaddr:

=> grepenv fdtaddr
fdtaddr=c00000
nfsboot=setenv bootargs root=/dev/nfs rw nfsroot=$serverip:$rootpath ip=$ipaddr:$serverip:$gatewayip:$netmask:$hostname:$netdev:off console=$consoledev,$baudrate $othbootargs;tftp $loadaddr $bootfile;tftp $fdtaddr $fdtfile;bootm $loadaddr - $fdtaddr
ramboot=setenv bootargs root=/dev/ram rw console=$consoledev,$baudrate $othbootargs;tftp $ramdiskaddr $ramdiskfile;tftp $loadaddr $bootfile;tftp $fdtaddr $fdtfile;bootm $loadaddr $ramdiskaddr $fdtaddr
=> grep $fdtaddr
fdtaddr=c00000
my_boot=bootm 0x40000000 0x41000000 0x00c00000
my_dtb=tftp 0x00c00000 $prefix/p4080ds.dtb
nohvboot=tftp 1000000 $dir/$bootfile;tftp 2000000 $dir/$ramdiskfile;tftp c00000 $dir/$fdtfile;setenv bootargs root=/dev/ram rw ramdisk_size=0x10000000 console=ttyS0,115200;bootm 1000000 2000000 c00000;
=>

This patch also enables the grepenv command by default on
corenet_ds based boards (and repositions the DHCP command
entry to keep the list sorted).

Signed-off-by: Kim Phillips <kim.phillips@freescale.com>
Cc: Kumar Gala <kumar.gala@freescale.com>
Cc: Andy Fleming <afleming@freescale.com>

# af4d9074 04-Apr-2011 Kim Phillips <kim.phillips@freescale.com>

env: fix env var autocompletion

commit 560d424b6d7cd4205b062ad95f1b104bd4f8bcc3 "env: re-add
support for auto-completion" fell short of its description -
the 'used' logic in hmatch_r was reversed - 'used' is 0 if
the hash table entry is not used, or -1 if deleted. This
patch makes hmatch_r actually match on valid ('used') entries,
instead of skipping them and failing to match anything.

typing 'printenv tft' and hitting 'tab' now displays valid
choices for variable names.

Signed-off-by: Kim Phillips <kim.phillips@freescale.com>
Cc: Mike Frysinger <vapier@gentoo.org>
Tested-by: Mike Frysinger <vapier@gentoo.org>

# c81c1222 21-Mar-2011 Peter Barada <peter.barada@logicpd.com>

Fix hash table deletion to prevent lost entries

Use negative used value to mark deleted entry. Search keeps probing
past deleted entries. Adding an entry uses first deleted entry when
it hits end of probe chain.

Initially found that "ramdiskimage" and "preboot" collide modulus 347,
causing "preboot" to be inserted at idx 190, "ramdiskimage" at idx 191.
Previous to this fix when "preboot" is deleted, "ramdiskimage" is
orphaned.

Signed-off-by: Peter Barada <peter.barada@logicpd.com>
Tested-by: Wolfgang Denk <wd@denx.de>

# 560d424b 17-Dec-2010 Mike Frysinger <vapier@gentoo.org>

env: re-add support for auto-completion

Currently, only basic completion is supported (no globs), but this is
what we had previously.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 2eb1573f 08-Dec-2010 Mike Frysinger <vapier@gentoo.org>

hashtable: drop all non-reentrant versions

The non-reentrant versions of the hashtable functions operate on a single
shared hashtable. So if two different people try using these funcs for
two different purposes, they'll cause problems for the other.

Avoid this by converting all existing hashtable consumers over to the
reentrant versions and then punting the non-reentrant ones.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 071bc923 27-Oct-2010 Wolfgang Denk <wd@denx.de>

Coding Style cleanup

Signed-off-by: Wolfgang Denk <wd@denx.de>

# fc5fc76b 01-Oct-2010 Andreas Bießmann <andreas.devel@googlemail.com>

lib/hashtable.c: add CONFIG_ENV_MIN_ENTRIES

This patch adds a new config parameter for adjusting the calculation of
hash table size when importing a buffer.

When importing a extremely small buffer (e.g. the default_environment)
the old calculation generated a hash table which could hold at most the
buffer content but no more entires.

The new calculation add a fixed number of entries to the result to fit
better for small import buffers. This amount may be configured by the
user in board file to adjust the behaviour.

Signed-off-by: Andreas Bie�mann <andreas.devel@googlemail.com>

# ea882baf 20-Jun-2010 Wolfgang Denk <wd@denx.de>

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>

# a6826fbc 20-Jun-2010 Wolfgang Denk <wd@denx.de>

Add hash table support as base for new environment code

This implementation is based on code from uClibc-0.9.30.3 but was
modified and extended for use within U-Boot.

Major modifications and extensions:

* hsearch() [modified / extended]:
- While the standard version does not make any assumptions about
the type of the stored data objects at all, this implementation
works with NUL terminated strings only.
- Instead of storing just pointers to the original objects, we
create local copies so the caller does not need to care about the
data any more.
- The standard implementation does not provide a way to update an
existing entry. This version will create a new entry or update an
existing one when both "action == ENTER" and "item.data != NULL".
- hsearch_r(): Instead of returning 1 on success, we return the
index into the internal hash table, which is also guaranteed to be
positive. This allows us direct access to the found hash table
slot for example for functions like hdelete().
* hdelete() [added]:
- The standard implementation of hsearch(3) does not provide any way
to delete any entries from the hash table. We extend the code to
do that.
* hexport() [added]:
- Export the data stored in the hash table in linearized form:
Entries are exported as "name=value" strings, separated by an
arbitrary (non-NUL, of course) separator character. This allows to
use this function both when formatting the U-Boot environment for
external storage (using '\0' as separator), but also when using it
for the "printenv" command to print all variables, simply by using
as '\n" as separator. This can also be used for new features like
exporting the environment data as text file, including the option
for later re-import.
- The entries in the result list will be sorted by ascending key
values.
* himport() [added]:
- Import linearized data into hash table. This is the inverse
function to hexport(): it takes a linear list of "name=value"
pairs and creates hash table entries from it.
- Entries without "value", i. e. consisting of only "name" or
"name=", will cause this entry to be deleted from the hash table.
- The "flag" argument can be used to control the behaviour: when
the H_NOCLEAR bit is set, then an existing hash table will kept,
i. e. new data will be added to an existing hash table;
otherwise, old data will be discarded and a new hash table will
be created.
- The separator character for the "name=value" pairs can be
selected, so we both support importing from externally stored
environment data (separated by NUL characters) and from plain text
files (entries separated by newline characters).
- To allow for nicely formatted text input, leading white space
(sequences of SPACE and TAB chars) is ignored, and entries
starting (after removal of any leading white space) with a '#'
character are considered comments and ignored.
- NOTE: this means that a variable name cannot start with a '#'
character.
- When using a non-NUL separator character, backslash is used as
escape character in the value part, allowing for example fo
multi-line values.
- In theory, arbitrary separator characters can be used, but only
'\0' and '\n' have really been tested.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# 3f0d6807 01-Aug-2019 Simon Glass <sjg@chromium.org>

env: Drop the ACTION typedef

Avoid using a typedef here which is unnecessary. Add an 'env_' prefix to
both the enum and its members to make it clear that these are related to
the environment.

Add an ENV prefix to these two flags so that it is clear what they relate
to. Also move them to env.h since they are part of the public API. Use an
enum rather than a #define to tie them together.

Signed-off-by: Simon Glass <sjg@chromium.org>

# 25e51e90 02-Aug-2019 Simon Glass <sjg@chromium.org>

env: Drop _ENTRY

This typedef does not need to be defined in the search.h header since it
is only used in one file (hashtable.c). Remove it from the header and
change it to a struct.

Signed-off-by: Simon Glass <sjg@chromium.org>

# dd2408ca 02-Aug-2019 Simon Glass <sjg@chromium.org>

env: Drop the ENTRY typedef

U-Boot is not supposed to use typedef for structs anymore. Also this name
is the same as the ENTRY() macro used in assembler files, and 'entry'
itself is widely used in U-Boot (>8k matches).

Drop the typedef and rename the struct to env_entry to reduce confusion.

Signed-off-by: Simon Glass <sjg@chromium.org>
Acked-by: Joe Hershberger <joe.hershberger@ni.com>

# 9dfdbd9f 30-Jan-2019 Roman Kapl <rka@sysgo.com>

hashtable: fix environment variable corruption

Only first previously deleted entry was recognized, leading hsearch_r
to think that there was no previously deleted entry. It then conluded
that a free entry was found, even if there were no free entries and it
overwrote a random entry.

This patch makes sure all deleted or free entries are always found and
also introduces constants for the 0 and -1 numbers. Unit tests to excersise a
simple hash table usage and catch the corruption were added.

To trash your environment, simply run this loop:

setenv i 0
while true; do
setenv v_$i $i
setenv v_$i
setexpr i $i + 1
done

Signed-off-by: Roman Kapl <rka@sysgo.com>

# 320194ae 23-Jan-2019 Heinrich Schuchardt <xypron.glpk@gmx.de>

hashtable: remove caps buffer

slre_match() checks if caps == NULL. In this case it does not try to
update it. So there is no need to create a buffer caps which we do not
evaluate.

Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de>

# 4bca3249 14-Dec-2018 AKASHI Takahiro <takahiro.akashi@linaro.org>

hashtable: fix length calculation in hexport_r

The commit below incorrectly fixed hexport_r();
> size = totlen + 1;
One extra byte is necessary to NULL-terminate a whole buffer, "resp."

Fixes: f1b20acb4a03 ("hashtable: Fix length calculation in hexport_r")
Signed-off-by: AKASHI Takahiro <takahiro.akashi@linaro.org>

# f1b20acb 17-Jul-2018 Zubair Lutfullah Kakakhel <zubair@resin.io>

hashtable: Fix length calculation in hexport_r

The length returned by hexport_r has a few redundant characters.
This appears as NULL characters at the end so seems harmless.

Remove the surplus counts in two places

totlen += strlen(ep->key) + 2;
I'm guessing the +2 here is for = and sep char. But there is another
totlen += 2; line that does that.

size = totletn + 1;
Doesn't make sense and isn't justified with any comment.

Signed-off-by: Zubair Lutfullah Kakakhel <zubair@resin.io>

# d9fc9077 09-Jul-2018 Quentin Schulz <quentin.schulz@bootlin.com>

hashtable: do not recreate whole hash table if vars are passed to himport_r

When vars are passed to the himport_r function with H_NOCLEAR flag,
those vars will be overridden in the current environment and if one of
those vars is not in the imported environment, it'll be deleted in the
current environment whatever the flag passed to himport_r.

The H_NOCLEAR flag is used to clear the whole environment whether vars
are passed to the function or not.

This leads to incoherent behaviour. If one passes vars to himport_r
with the H_NOCLEAR flag, if a var in vars is not in the imported env,
that var will be removed from the current env.

If one passes vars to himport_r without the H_NOCLEAR flag, the whole
environment will be removed and vars will be imported from the
environment in RAM.

It makes more sense to keep the variable that is in the current
environment but not in the imported environment if the H_NOCLEAR flag is
set and remove only that variable if the H_NOCLEAR flag is not set.

Let's clear the whole environment only if H_NOCLEAR and vars are not
passed to himport_r.

Let's remove variables that are in the current environment but not in
the imported env only if the H_NOCLEAR flag is not passed.

Suggested-by: Wolfgang Denk <wd@denx.de>
Signed-off-by: Quentin Schulz <quentin.schulz@bootlin.com>

# 83d290c5 06-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert all of our single license tags to Linux Kernel style

When U-Boot started using SPDX tags we were among the early adopters and
there weren't a lot of other examples to borrow from. So we picked the
area of the file that usually had a full license text and replaced it
with an appropriate SPDX-License-Identifier: entry. Since then, the
Linux Kernel has adopted SPDX tags and they place it as the very first
line in a file (except where shebangs are used, then it's second line)
and with slightly different comment styles than us.

In part due to community overlap, in part due to better tag visibility
and in part for other minor reasons, switch over to that style.

This commit changes all instances where we have a single declared
license in the tag as both the before and after are identical in tag
contents. There's also a few places where I found we did not have a tag
and have introduced one.

Signed-off-by: Tom Rini <trini@konsulko.com>

# d2d9bdfc 28-Sep-2016 B, Ravi <ravibabu@ti.com>

spl: saveenv: adding saveenv support in SPL

By default saveenv option is not supported for SPL. This patch
enable the support for save environment variable for SPL build.

Enable save environment support in SPL after setenv. By default
the saveenv option is not provided in SPL, but some boards need
this support in 'Falcon' boot, where SPL need to boot from
different images based on environment variable set by OS. For
example OS may set "reboot_image" environment variable to
"recovery" inorder to boot recovery image by SPL. The SPL read
"reboot_image" and act accordingly and change the reboot_image
to default mode using setenv and save the environemnt.

Signed-off-by: Ravi Babu <ravibabu@ti.com>
Reviewed-by: Simon Glass <sig@chromium.org>

change in v1:
- dropped SUPPORT, use CONFIG_SPL_SAVEENV
- updates the comments in mmc_private.h

# fc0b5948 07-Sep-2016 Robert P. J. Day <rpjday@crashcourse.ca>

Various, accumulated typos collected from around the tree.

Fix various misspellings of:

* deprecated
* partition
* preceding,preceded
* preparation
* its versus it's
* export
* existing
* scenario
* redundant
* remaining
* value
* architecture

Signed-off-by: Robert P. J. Day <rpjday@crashcourse.ca>
Reviewed-by: Jagan Teki <jteki@openedev.com>
Reviewed-by: Stefan Roese <sr@denx.de>

# c55d02b2 22-Jul-2016 Simon Glass <sjg@chromium.org>

hashtable: Fix compiler warning on 32-bit sandbox

This fixes a mismatch between the %zu format and the type used on sandbox.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@konsulko.com>

# 62a3b7dd 15-Jul-2016 Robert P. J. Day <rpjday@crashcourse.ca>

Various, unrelated tree-wide typo fixes.

Fix a number of typos, including:

* "compatble" -> "compatible"
* "eanbeld" -> "enabled"
* "envrionment" -> "environment"
* "FTD" -> "FDT" (for "flattened device tree")
* "ommitted" -> "omitted"
* "overriden" -> "overridden"
* "partiton" -> "partition"
* "propogate" -> "propagate"
* "resourse" -> "resource"
* "rest in piece" -> "rest in peace"
* "suport" -> "support"
* "varible" -> "variable"

Signed-off-by: Robert P. J. Day <rpjday@crashcourse.ca>

# 0226d878 13-Sep-2015 Lukasz Majewski <l.majewski@majess.pl>

env: import: hashtable: Free memory allocated before exiting from himport_r()

ithout this patch memory is not released on early exit.

Signed-off-by: Lukasz Majewski <l.majewski@majess.pl>

# 817e48d8 13-Sep-2015 Lukasz Majewski <l.majewski@majess.pl>

env: import: hashtable: Prevent buffer overrun when importing environment from file

Lets consider following scenario:
- One uses echo -n "key=value" to define environment variable in a file (single variable)
- The file content is "key=value" without any terminating byte (e.g. 0x0a or
0x0d).
- The file is loaded to u-boot non zero'ed RAM buffer (with load command).
- Then "env import -t -r $loadaddr $filesize" is executed.
- Due to lack of proper termination byte we have classical example of buffer
overrun.

This patch prevents from this by allocating one extra byte than size and
explicitly null terminate it.

There should be no change for normal env import operation after applying
this patch.

Signed-off-by: Lukasz Majewski <l.majewski@majess.pl>

# ecd1446f 14-Jul-2014 Alexander Holler <holler@ahsoftware.de>

Add option -r to env import to allow import of text files with CRLF as line endings

When this option is enabled, CRLF is treated like LF when importing environments
from text files, which means CRs ('\r') in front of LFs ('\n') are just ignored.

Drawback of enabling this option is that (maybe exported) variables which have
a trailing CR in their content will get imported without that CR. But this
drawback is very unlikely and the big advantage of letting Windows user create
a *working* uEnv.txt too is likely more welcome.

Signed-off-by: Alexander Holler <holler@ahsoftware.de>

# 9a832331 08-Oct-2013 Pierre Aubert <p.aubert@staubli.com>

env: fix the env export varname

The env export command doesn't export the first variable of the list
since commit 5a31ea04c9ee5544fbb70ad7597ea4b294840eab
"env grep" - reimplement command using hexport_r()

Signed-off-by: Pierre Aubert <p.aubert@staubli.com>

# 1bce2aeb 16-Sep-2013 Robert P. J. Day <rpjday@crashcourse.ca>

Cosmetic: Fix a number of typos, no functional changes.

Fix various misspellings of things like "environment", "kernel",
"default" and "volatile", and throw in a couple grammar fixes.

Signed-off-by: Robert P. J. Day <rpjday@crashcourse.ca>

# eee479cf 08-Jul-2013 Wolfgang Denk <wd@denx.de>

Add LGPL-2.1+ SPDX-License-Identifier to source files

Signed-off-by: Wolfgang Denk <wd@denx.de>

# e4fdcadd 28-Apr-2013 Lucian Cojocar <cojocar@gmail.com>

env: throw an error when an empty key is used

If the environment contains an entry like "=value" "\0" we should throw
an error when parsing the environment. Otherwise, U-Boot will enter in
an infinite loop.

Signed-off-by: Lucian Cojocar <cojocar@gmail.com>

# be29df6a 23-Mar-2013 Wolfgang Denk <wd@denx.de>

"env grep" - add support for regular expression matches

When CONFIG_REGEX is enabled, the new option "-e" becomes available
which causes regular expression matches to be used. This allows for
example things like these:

- print all MAC addresses:

=> env grep -e eth.*addr
eth1addr=00:10:ec:80:c5:15
ethaddr=00:10:ec:00:c5:15

- print all variables that have at least 2 colons in their value:

=> env grep -v -e :.*:
addip=setenv bootargs ${bootargs} ip=${ipaddr}:${serverip}:${gatewayip}:${netmask}:${hostname}:${netdev}:off
panic=1
eth1addr=00:10:ec:80:c5:15
ethaddr=00:10:ec:00:c5:15
ver=U-Boot 2013.04-rc1-00289-g497746b-dirty (Mar 22 2013 - 12:50:25)

etc.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# 5a31ea04 23-Mar-2013 Wolfgang Denk <wd@denx.de>

"env grep" - reimplement command using hexport_r()

Also drop hstrstr_r() which is not needed any more.
The new code is way more flexible.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# ea009d47 23-Mar-2013 Wolfgang Denk <wd@denx.de>

hashtable: preparations to use hexport_r() for "env grep"

The output of "env grep" is unsorted, and printing is done by a
private implementation to parse the hash table. We have all the
needed code in place in hexport_r() alsready, so let's use this
instead. Here we prepare the code for this, without any functional
changes yet.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# 2598090b 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Add environment variable flags

Currently just validates variable types as decimal, hexidecimal,
boolean, ip address, and mac address.

If the entry is not found in the env ".flags", then look in the static
one. This allows the env to override the static definitions, but prevents
the need to have every definition in the environment distracting you.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# 170ab110 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Add support for callbacks to environment vars

Add support for per-variable callbacks to the "hashtable" functions.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

!!!fix comment in callback

# be11235a 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Hide '.' variables in env print by default

When printing all variables with env print, don't print variables that
begin with '.'. If env print is called with a '-a' switch, then
include variables that begin with '.' (just like the ls command).

Variables printed explicitly will be printed even without the -a.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# 7afcf3a5 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Refactor apply into change_ok

Move the read of the old value to inside the check function. In some
cases it can be avoided all together and at the least the code is only
called from one place.

Also name the function and the callback to more clearly describe what
it does.

Pass the ENTRY instead of just the name for direct access to the whole
data structure.

Pass an enum to the callback that specifies the operation being approved.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# 3d3b52f2 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Consolidate common code in hsearch_r()

The same chunk of code was replicated in two places and the following
changes will make that chunk grow a bit, so combine into a static func.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# c4e0057f 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Refactor do_apply to a flag

Use a flag in hsearch_r for insert mode passed from import to allow the
behavior be different based on use.

Now that "do_check" is called for all imports, ensure console init is
complete before updating the console on relocation import

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

# 7ac2fe2d 17-Sep-2012 Ilya Yanok <ilya.yanok@cogentembedded.com>

OMAP: networking support for SPL

This patch adds support for networking in SPL. Some devices are
capable of loading SPL via network so it makes sense to load the
main U-Boot binary via network too. This patch tries to use
existing network code as much as possible. Unfortunately, it depends
on environment which in turn depends on other code so SPL size
is increased significantly. No effort was done to decouple network
code and environment so far.

Signed-off-by: Ilya Yanok <ilya.yanok@cogentembedded.com>
Acked-by: Joe Hershberger <joe.hershberger@ni.com>
Signed-off-by: Tom Rini <trini@ti.com>

# d5370feb 26-Aug-2012 Gerlando Falauto <gerlando.falauto@keymile.com>

env: delete selected vars not present in imported env

When variables explicitly specified on the command line are not present
in the imported env, delete them from the running env.
If the variable is also missing from the running env, issue a warning.

Signed-off-by: Gerlando Falauto <gerlando.falauto@keymile.com>
Reviewed-by: Marek Vasut <marex@denx.de>

# 152874b6 23-Aug-2012 Gerlando Falauto <gerlando.falauto@keymile.com>

env: check and apply changes on delete/destroy

Signed-off-by: Gerlando Falauto <gerlando.falauto@keymile.com>
Reviewed-by: Marek Vasut <marex@denx.de>

# c5983592 23-Aug-2012 Gerlando Falauto <gerlando.falauto@keymile.com>

env: add check/apply logic to himport_r()

Change hashtable so that a callback function will decide whether a
variable can be overwritten, and possibly apply the changes.

So add a new field to struct hsearch_data:

o "apply" callback function to check whether a variable can be
overwritten, and possibly immediately apply the changes;
when NULL, no check is performed.

And a new argument to himport_r():
o "do_apply": whether to call the apply callback function

NOTE: This patch does not change the current behavior.

Signed-off-by: Gerlando Falauto <gerlando.falauto@keymile.com>
Reviewed-by: Marek Vasut <marex@denx.de>

# 348b1f1c 23-Aug-2012 Gerlando Falauto <gerlando.falauto@keymile.com>

env: make himport_r() selective on variables

Add 2 new arguments to himport_r():

o "nvars", "vars": number and list of variables to take into account
(0 means ALL)

NOTE: This patch does not change the current behaviour.

Signed-off-by: Gerlando Falauto <gerlando.falauto@keymile.com>
Reviewed-by: Marek Vasut <marex@denx.de>

# ff856286 21-Nov-2011 Simon Glass <sjg@chromium.org>

sandbox: Fix warnings in hashtable.c

This fixes a few printf() strings for size_t which are missing the 'z'
modifier.

Signed-off-by: Simon Glass <sjg@chromium.org>
Acked-by: Mike Frysinger <vapier@gentoo.org>

# 37f2fe74 06-Nov-2011 Wolfgang Denk <wd@denx.de>

env: allow to export only selected variables

New syntax:
env export [-t | -b | -c] [-s size] addr [var ...]

With this change it is possible to provide a list of variables names
that shall be exported. Whenno arguments are given, the whole
environment gets exported.

NOTE: The new handling of the "size" argument means a change to the
user API.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# 4d91a6ec 23-Aug-2011 Jason Hobbs <jason.hobbs@calxeda.com>

Replace space and tab checks with isblank

These are various places I found that checked for conditions equivalent
to isblank.

Signed-off-by: Jason Hobbs <jason.hobbs@calxeda.com>

# 84b5e802 29-Jul-2011 Wolfgang Denk <wd@denx.de>

Constify getenv(), setenv() and hash code functions

This is needed to get rid of build warnings like

main.c:311: warning: passing argument 2 of 'setenv' discards qualifiers from pointer target type

which result from commit 09c2e90 "unify version_string".

Signed-off-by: Wolfgang Denk <wd@denx.de>
Cc: Andreas Bie�mann <andreas.devel@googlemail.com>

# a000b795 05-Apr-2011 Kim Phillips <kim.phillips@freescale.com>

common: add a grepenv command

u-boot environments, esp. when boards are shared across multiple
users, can get pretty large and time consuming to visually parse.
The grepenv command this patch adds can be used in lieu of printenv
to facilitate searching. grepenv works like printenv but limits
its output only to environment strings (variable name and value
pairs) that match the user specified substring.

the following examples are on a board with a 5313 byte environment
that spans multiple screen pages:

Example 1: summarize ethernet configuration:

=> grepenv eth TSEC
etact=FM1@DTSEC2
eth=FM1@DTSEC4
ethact=FM1@DTSEC2
eth1addr=00:E0:0C:00:8b:01
eth2addr=00:E0:0C:00:8b:02
eth3addr=00:E0:0C:00:8b:03
eth4addr=00:E0:0C:00:8b:04
eth5addr=00:E0:0C:00:8b:05
eth6addr=00:E0:0C:00:8b:06
eth7addr=00:E0:0C:00:8b:07
eth8addr=00:E0:0C:00:8b:08
eth9addr=00:E0:0C:00:8b:09
ethaddr=00:E0:0C:00:8b:00
netdev=eth0
uprcw=setenv ethact $eth;setenv filename p4080ds/R_PPSXX_0xe/rcw_0xe_2sgmii_rev2_high.bin;setenv start 0xe8000000;protect off all;run upimage;protect on all
upuboot=setenv ethact $eth;setenv filename u-boot.bin;setenv start eff80000;protect off all;run upimage;protect on all
upucode=setenv ethact $eth;setenv filename fsl_fman_ucode_P4080_101_6.bin;setenv start 0xef000000;protect off all;run upimage;protect on all
usdboot=setenv ethact $eth;tftp 1000000 $dir/$bootfile;tftp 2000000 $dir/initramfs.cpio.gz.uboot;tftp c00000 $dir/p4080ds-usdpaa.dtb;setenv bootargs root=/dev/ram rw console=ttyS0,115200 $othbootargs;bootm 1000000 2000000 c00000;
=>

Example 2: detect unused env vars:

=> grepenv etact
etact=FM1@DTSEC2
=>

Example 3: reveal hardcoded variables; e.g., for fdtaddr:

=> grepenv fdtaddr
fdtaddr=c00000
nfsboot=setenv bootargs root=/dev/nfs rw nfsroot=$serverip:$rootpath ip=$ipaddr:$serverip:$gatewayip:$netmask:$hostname:$netdev:off console=$consoledev,$baudrate $othbootargs;tftp $loadaddr $bootfile;tftp $fdtaddr $fdtfile;bootm $loadaddr - $fdtaddr
ramboot=setenv bootargs root=/dev/ram rw console=$consoledev,$baudrate $othbootargs;tftp $ramdiskaddr $ramdiskfile;tftp $loadaddr $bootfile;tftp $fdtaddr $fdtfile;bootm $loadaddr $ramdiskaddr $fdtaddr
=> grep $fdtaddr
fdtaddr=c00000
my_boot=bootm 0x40000000 0x41000000 0x00c00000
my_dtb=tftp 0x00c00000 $prefix/p4080ds.dtb
nohvboot=tftp 1000000 $dir/$bootfile;tftp 2000000 $dir/$ramdiskfile;tftp c00000 $dir/$fdtfile;setenv bootargs root=/dev/ram rw ramdisk_size=0x10000000 console=ttyS0,115200;bootm 1000000 2000000 c00000;
=>

This patch also enables the grepenv command by default on
corenet_ds based boards (and repositions the DHCP command
entry to keep the list sorted).

Signed-off-by: Kim Phillips <kim.phillips@freescale.com>
Cc: Kumar Gala <kumar.gala@freescale.com>
Cc: Andy Fleming <afleming@freescale.com>

# af4d9074 04-Apr-2011 Kim Phillips <kim.phillips@freescale.com>

env: fix env var autocompletion

commit 560d424b6d7cd4205b062ad95f1b104bd4f8bcc3 "env: re-add
support for auto-completion" fell short of its description -
the 'used' logic in hmatch_r was reversed - 'used' is 0 if
the hash table entry is not used, or -1 if deleted. This
patch makes hmatch_r actually match on valid ('used') entries,
instead of skipping them and failing to match anything.

typing 'printenv tft' and hitting 'tab' now displays valid
choices for variable names.

Signed-off-by: Kim Phillips <kim.phillips@freescale.com>
Cc: Mike Frysinger <vapier@gentoo.org>
Tested-by: Mike Frysinger <vapier@gentoo.org>

# c81c1222 21-Mar-2011 Peter Barada <peter.barada@logicpd.com>

Fix hash table deletion to prevent lost entries

Use negative used value to mark deleted entry. Search keeps probing
past deleted entries. Adding an entry uses first deleted entry when
it hits end of probe chain.

Initially found that "ramdiskimage" and "preboot" collide modulus 347,
causing "preboot" to be inserted at idx 190, "ramdiskimage" at idx 191.
Previous to this fix when "preboot" is deleted, "ramdiskimage" is
orphaned.

Signed-off-by: Peter Barada <peter.barada@logicpd.com>
Tested-by: Wolfgang Denk <wd@denx.de>

# 560d424b 17-Dec-2010 Mike Frysinger <vapier@gentoo.org>

env: re-add support for auto-completion

Currently, only basic completion is supported (no globs), but this is
what we had previously.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 2eb1573f 08-Dec-2010 Mike Frysinger <vapier@gentoo.org>

hashtable: drop all non-reentrant versions

The non-reentrant versions of the hashtable functions operate on a single
shared hashtable. So if two different people try using these funcs for
two different purposes, they'll cause problems for the other.

Avoid this by converting all existing hashtable consumers over to the
reentrant versions and then punting the non-reentrant ones.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>

# 071bc923 27-Oct-2010 Wolfgang Denk <wd@denx.de>

Coding Style cleanup

Signed-off-by: Wolfgang Denk <wd@denx.de>

# fc5fc76b 01-Oct-2010 Andreas Bießmann <andreas.devel@googlemail.com>

lib/hashtable.c: add CONFIG_ENV_MIN_ENTRIES

This patch adds a new config parameter for adjusting the calculation of
hash table size when importing a buffer.

When importing a extremely small buffer (e.g. the default_environment)
the old calculation generated a hash table which could hold at most the
buffer content but no more entires.

The new calculation add a fixed number of entries to the result to fit
better for small import buffers. This amount may be configured by the
user in board file to adjust the behaviour.

Signed-off-by: Andreas Bie�mann <andreas.devel@googlemail.com>

# ea882baf 20-Jun-2010 Wolfgang Denk <wd@denx.de>

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>

# a6826fbc 20-Jun-2010 Wolfgang Denk <wd@denx.de>

Add hash table support as base for new environment code

This implementation is based on code from uClibc-0.9.30.3 but was
modified and extended for use within U-Boot.

Major modifications and extensions:

* hsearch() [modified / extended]:
- While the standard version does not make any assumptions about
the type of the stored data objects at all, this implementation
works with NUL terminated strings only.
- Instead of storing just pointers to the original objects, we
create local copies so the caller does not need to care about the
data any more.
- The standard implementation does not provide a way to update an
existing entry. This version will create a new entry or update an
existing one when both "action == ENTER" and "item.data != NULL".
- hsearch_r(): Instead of returning 1 on success, we return the
index into the internal hash table, which is also guaranteed to be
positive. This allows us direct access to the found hash table
slot for example for functions like hdelete().
* hdelete() [added]:
- The standard implementation of hsearch(3) does not provide any way
to delete any entries from the hash table. We extend the code to
do that.
* hexport() [added]:
- Export the data stored in the hash table in linearized form:
Entries are exported as "name=value" strings, separated by an
arbitrary (non-NUL, of course) separator character. This allows to
use this function both when formatting the U-Boot environment for
external storage (using '\0' as separator), but also when using it
for the "printenv" command to print all variables, simply by using
as '\n" as separator. This can also be used for new features like
exporting the environment data as text file, including the option
for later re-import.
- The entries in the result list will be sorted by ascending key
values.
* himport() [added]:
- Import linearized data into hash table. This is the inverse
function to hexport(): it takes a linear list of "name=value"
pairs and creates hash table entries from it.
- Entries without "value", i. e. consisting of only "name" or
"name=", will cause this entry to be deleted from the hash table.
- The "flag" argument can be used to control the behaviour: when
the H_NOCLEAR bit is set, then an existing hash table will kept,
i. e. new data will be added to an existing hash table;
otherwise, old data will be discarded and a new hash table will
be created.
- The separator character for the "name=value" pairs can be
selected, so we both support importing from externally stored
environment data (separated by NUL characters) and from plain text
files (entries separated by newline characters).
- To allow for nicely formatted text input, leading white space
(sequences of SPACE and TAB chars) is ignored, and entries
starting (after removal of any leading white space) with a '#'
character are considered comments and ignored.
- NOTE: this means that a variable name cannot start with a '#'
character.
- When using a non-NUL separator character, backslash is used as
escape character in the value part, allowing for example fo
multi-line values.
- In theory, arbitrary separator characters can be used, but only
'\0' and '\n' have really been tested.

Signed-off-by: Wolfgang Denk <wd@denx.de>

# f1b20acb 17-Jul-2018 Zubair Lutfullah Kakakhel <zubair@resin.io>

hashtable: Fix length calculation in hexport_r

The length returned by hexport_r has a few redundant characters.
This appears as NULL characters at the end so seems harmless.

Remove the surplus counts in two places

totlen += strlen(ep->key) + 2;
I'm guessing the +2 here is for = and sep char. But there is another
totlen += 2; line that does that.

size = totletn + 1;
Doesn't make sense and isn't justified with any comment.

Signed-off-by: Zubair Lutfullah Kakakhel <zubair@resin.io>


# d9fc9077 09-Jul-2018 Quentin Schulz <quentin.schulz@bootlin.com>

hashtable: do not recreate whole hash table if vars are passed to himport_r

When vars are passed to the himport_r function with H_NOCLEAR flag,
those vars will be overridden in the current environment and if one of
those vars is not in the imported environment, it'll be deleted in the
current environment whatever the flag passed to himport_r.

The H_NOCLEAR flag is used to clear the whole environment whether vars
are passed to the function or not.

This leads to incoherent behaviour. If one passes vars to himport_r
with the H_NOCLEAR flag, if a var in vars is not in the imported env,
that var will be removed from the current env.

If one passes vars to himport_r without the H_NOCLEAR flag, the whole
environment will be removed and vars will be imported from the
environment in RAM.

It makes more sense to keep the variable that is in the current
environment but not in the imported environment if the H_NOCLEAR flag is
set and remove only that variable if the H_NOCLEAR flag is not set.

Let's clear the whole environment only if H_NOCLEAR and vars are not
passed to himport_r.

Let's remove variables that are in the current environment but not in
the imported env only if the H_NOCLEAR flag is not passed.

Suggested-by: Wolfgang Denk <wd@denx.de>
Signed-off-by: Quentin Schulz <quentin.schulz@bootlin.com>


# 83d290c5 06-May-2018 Tom Rini <trini@konsulko.com>

SPDX: Convert all of our single license tags to Linux Kernel style

When U-Boot started using SPDX tags we were among the early adopters and
there weren't a lot of other examples to borrow from. So we picked the
area of the file that usually had a full license text and replaced it
with an appropriate SPDX-License-Identifier: entry. Since then, the
Linux Kernel has adopted SPDX tags and they place it as the very first
line in a file (except where shebangs are used, then it's second line)
and with slightly different comment styles than us.

In part due to community overlap, in part due to better tag visibility
and in part for other minor reasons, switch over to that style.

This commit changes all instances where we have a single declared
license in the tag as both the before and after are identical in tag
contents. There's also a few places where I found we did not have a tag
and have introduced one.

Signed-off-by: Tom Rini <trini@konsulko.com>


# d2d9bdfc 28-Sep-2016 B, Ravi <ravibabu@ti.com>

spl: saveenv: adding saveenv support in SPL

By default saveenv option is not supported for SPL. This patch
enable the support for save environment variable for SPL build.

Enable save environment support in SPL after setenv. By default
the saveenv option is not provided in SPL, but some boards need
this support in 'Falcon' boot, where SPL need to boot from
different images based on environment variable set by OS. For
example OS may set "reboot_image" environment variable to
"recovery" inorder to boot recovery image by SPL. The SPL read
"reboot_image" and act accordingly and change the reboot_image
to default mode using setenv and save the environemnt.

Signed-off-by: Ravi Babu <ravibabu@ti.com>
Reviewed-by: Simon Glass <sig@chromium.org>

change in v1:
- dropped SUPPORT, use CONFIG_SPL_SAVEENV
- updates the comments in mmc_private.h


# fc0b5948 07-Sep-2016 Robert P. J. Day <rpjday@crashcourse.ca>

Various, accumulated typos collected from around the tree.

Fix various misspellings of:

* deprecated
* partition
* preceding,preceded
* preparation
* its versus it's
* export
* existing
* scenario
* redundant
* remaining
* value
* architecture

Signed-off-by: Robert P. J. Day <rpjday@crashcourse.ca>
Reviewed-by: Jagan Teki <jteki@openedev.com>
Reviewed-by: Stefan Roese <sr@denx.de>


# c55d02b2 22-Jul-2016 Simon Glass <sjg@chromium.org>

hashtable: Fix compiler warning on 32-bit sandbox

This fixes a mismatch between the %zu format and the type used on sandbox.

Signed-off-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Tom Rini <trini@konsulko.com>


# 62a3b7dd 15-Jul-2016 Robert P. J. Day <rpjday@crashcourse.ca>

Various, unrelated tree-wide typo fixes.

Fix a number of typos, including:

* "compatble" -> "compatible"
* "eanbeld" -> "enabled"
* "envrionment" -> "environment"
* "FTD" -> "FDT" (for "flattened device tree")
* "ommitted" -> "omitted"
* "overriden" -> "overridden"
* "partiton" -> "partition"
* "propogate" -> "propagate"
* "resourse" -> "resource"
* "rest in piece" -> "rest in peace"
* "suport" -> "support"
* "varible" -> "variable"

Signed-off-by: Robert P. J. Day <rpjday@crashcourse.ca>


# 0226d878 13-Sep-2015 Lukasz Majewski <l.majewski@majess.pl>

env: import: hashtable: Free memory allocated before exiting from himport_r()

ithout this patch memory is not released on early exit.

Signed-off-by: Lukasz Majewski <l.majewski@majess.pl>


# 817e48d8 13-Sep-2015 Lukasz Majewski <l.majewski@majess.pl>

env: import: hashtable: Prevent buffer overrun when importing environment from file

Lets consider following scenario:
- One uses echo -n "key=value" to define environment variable in a file (single variable)
- The file content is "key=value" without any terminating byte (e.g. 0x0a or
0x0d).
- The file is loaded to u-boot non zero'ed RAM buffer (with load command).
- Then "env import -t -r $loadaddr $filesize" is executed.
- Due to lack of proper termination byte we have classical example of buffer
overrun.

This patch prevents from this by allocating one extra byte than size and
explicitly null terminate it.

There should be no change for normal env import operation after applying
this patch.

Signed-off-by: Lukasz Majewski <l.majewski@majess.pl>


# ecd1446f 14-Jul-2014 Alexander Holler <holler@ahsoftware.de>

Add option -r to env import to allow import of text files with CRLF as line endings

When this option is enabled, CRLF is treated like LF when importing environments
from text files, which means CRs ('\r') in front of LFs ('\n') are just ignored.

Drawback of enabling this option is that (maybe exported) variables which have
a trailing CR in their content will get imported without that CR. But this
drawback is very unlikely and the big advantage of letting Windows user create
a *working* uEnv.txt too is likely more welcome.

Signed-off-by: Alexander Holler <holler@ahsoftware.de>


# 9a832331 08-Oct-2013 Pierre Aubert <p.aubert@staubli.com>

env: fix the env export varname

The env export command doesn't export the first variable of the list
since commit 5a31ea04c9ee5544fbb70ad7597ea4b294840eab
"env grep" - reimplement command using hexport_r()

Signed-off-by: Pierre Aubert <p.aubert@staubli.com>


# 1bce2aeb 16-Sep-2013 Robert P. J. Day <rpjday@crashcourse.ca>

Cosmetic: Fix a number of typos, no functional changes.

Fix various misspellings of things like "environment", "kernel",
"default" and "volatile", and throw in a couple grammar fixes.

Signed-off-by: Robert P. J. Day <rpjday@crashcourse.ca>


# eee479cf 08-Jul-2013 Wolfgang Denk <wd@denx.de>

Add LGPL-2.1+ SPDX-License-Identifier to source files

Signed-off-by: Wolfgang Denk <wd@denx.de>


# e4fdcadd 28-Apr-2013 Lucian Cojocar <cojocar@gmail.com>

env: throw an error when an empty key is used

If the environment contains an entry like "=value" "\0" we should throw
an error when parsing the environment. Otherwise, U-Boot will enter in
an infinite loop.

Signed-off-by: Lucian Cojocar <cojocar@gmail.com>


# be29df6a 23-Mar-2013 Wolfgang Denk <wd@denx.de>

"env grep" - add support for regular expression matches

When CONFIG_REGEX is enabled, the new option "-e" becomes available
which causes regular expression matches to be used. This allows for
example things like these:

- print all MAC addresses:

=> env grep -e eth.*addr
eth1addr=00:10:ec:80:c5:15
ethaddr=00:10:ec:00:c5:15

- print all variables that have at least 2 colons in their value:

=> env grep -v -e :.*:
addip=setenv bootargs ${bootargs} ip=${ipaddr}:${serverip}:${gatewayip}:${netmask}:${hostname}:${netdev}:off
panic=1
eth1addr=00:10:ec:80:c5:15
ethaddr=00:10:ec:00:c5:15
ver=U-Boot 2013.04-rc1-00289-g497746b-dirty (Mar 22 2013 - 12:50:25)

etc.

Signed-off-by: Wolfgang Denk <wd@denx.de>


# 5a31ea04 23-Mar-2013 Wolfgang Denk <wd@denx.de>

"env grep" - reimplement command using hexport_r()

Also drop hstrstr_r() which is not needed any more.
The new code is way more flexible.

Signed-off-by: Wolfgang Denk <wd@denx.de>


# ea009d47 23-Mar-2013 Wolfgang Denk <wd@denx.de>

hashtable: preparations to use hexport_r() for "env grep"

The output of "env grep" is unsorted, and printing is done by a
private implementation to parse the hash table. We have all the
needed code in place in hexport_r() alsready, so let's use this
instead. Here we prepare the code for this, without any functional
changes yet.

Signed-off-by: Wolfgang Denk <wd@denx.de>


# 2598090b 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Add environment variable flags

Currently just validates variable types as decimal, hexidecimal,
boolean, ip address, and mac address.

If the entry is not found in the env ".flags", then look in the static
one. This allows the env to override the static definitions, but prevents
the need to have every definition in the environment distracting you.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>


# 170ab110 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Add support for callbacks to environment vars

Add support for per-variable callbacks to the "hashtable" functions.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>

!!!fix comment in callback


# be11235a 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Hide '.' variables in env print by default

When printing all variables with env print, don't print variables that
begin with '.'. If env print is called with a '-a' switch, then
include variables that begin with '.' (just like the ls command).

Variables printed explicitly will be printed even without the -a.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>


# 7afcf3a5 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Refactor apply into change_ok

Move the read of the old value to inside the check function. In some
cases it can be avoided all together and at the least the code is only
called from one place.

Also name the function and the callback to more clearly describe what
it does.

Pass the ENTRY instead of just the name for direct access to the whole
data structure.

Pass an enum to the callback that specifies the operation being approved.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>


# 3d3b52f2 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Consolidate common code in hsearch_r()

The same chunk of code was replicated in two places and the following
changes will make that chunk grow a bit, so combine into a static func.

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>


# c4e0057f 11-Dec-2012 Joe Hershberger <joe.hershberger@ni.com>

env: Refactor do_apply to a flag

Use a flag in hsearch_r for insert mode passed from import to allow the
behavior be different based on use.

Now that "do_check" is called for all imports, ensure console init is
complete before updating the console on relocation import

Signed-off-by: Joe Hershberger <joe.hershberger@ni.com>


# 7ac2fe2d 17-Sep-2012 Ilya Yanok <ilya.yanok@cogentembedded.com>

OMAP: networking support for SPL

This patch adds support for networking in SPL. Some devices are
capable of loading SPL via network so it makes sense to load the
main U-Boot binary via network too. This patch tries to use
existing network code as much as possible. Unfortunately, it depends
on environment which in turn depends on other code so SPL size
is increased significantly. No effort was done to decouple network
code and environment so far.

Signed-off-by: Ilya Yanok <ilya.yanok@cogentembedded.com>
Acked-by: Joe Hershberger <joe.hershberger@ni.com>
Signed-off-by: Tom Rini <trini@ti.com>


# d5370feb 26-Aug-2012 Gerlando Falauto <gerlando.falauto@keymile.com>

env: delete selected vars not present in imported env

When variables explicitly specified on the command line are not present
in the imported env, delete them from the running env.
If the variable is also missing from the running env, issue a warning.

Signed-off-by: Gerlando Falauto <gerlando.falauto@keymile.com>
Reviewed-by: Marek Vasut <marex@denx.de>


# 152874b6 23-Aug-2012 Gerlando Falauto <gerlando.falauto@keymile.com>

env: check and apply changes on delete/destroy

Signed-off-by: Gerlando Falauto <gerlando.falauto@keymile.com>
Reviewed-by: Marek Vasut <marex@denx.de>


# c5983592 23-Aug-2012 Gerlando Falauto <gerlando.falauto@keymile.com>

env: add check/apply logic to himport_r()

Change hashtable so that a callback function will decide whether a
variable can be overwritten, and possibly apply the changes.

So add a new field to struct hsearch_data:

o "apply" callback function to check whether a variable can be
overwritten, and possibly immediately apply the changes;
when NULL, no check is performed.

And a new argument to himport_r():
o "do_apply": whether to call the apply callback function

NOTE: This patch does not change the current behavior.

Signed-off-by: Gerlando Falauto <gerlando.falauto@keymile.com>
Reviewed-by: Marek Vasut <marex@denx.de>


# 348b1f1c 23-Aug-2012 Gerlando Falauto <gerlando.falauto@keymile.com>

env: make himport_r() selective on variables

Add 2 new arguments to himport_r():

o "nvars", "vars": number and list of variables to take into account
(0 means ALL)

NOTE: This patch does not change the current behaviour.

Signed-off-by: Gerlando Falauto <gerlando.falauto@keymile.com>
Reviewed-by: Marek Vasut <marex@denx.de>


# ff856286 21-Nov-2011 Simon Glass <sjg@chromium.org>

sandbox: Fix warnings in hashtable.c

This fixes a few printf() strings for size_t which are missing the 'z'
modifier.

Signed-off-by: Simon Glass <sjg@chromium.org>
Acked-by: Mike Frysinger <vapier@gentoo.org>


# 37f2fe74 06-Nov-2011 Wolfgang Denk <wd@denx.de>

env: allow to export only selected variables

New syntax:
env export [-t | -b | -c] [-s size] addr [var ...]

With this change it is possible to provide a list of variables names
that shall be exported. Whenno arguments are given, the whole
environment gets exported.

NOTE: The new handling of the "size" argument means a change to the
user API.

Signed-off-by: Wolfgang Denk <wd@denx.de>


# 4d91a6ec 23-Aug-2011 Jason Hobbs <jason.hobbs@calxeda.com>

Replace space and tab checks with isblank

These are various places I found that checked for conditions equivalent
to isblank.

Signed-off-by: Jason Hobbs <jason.hobbs@calxeda.com>


# 84b5e802 29-Jul-2011 Wolfgang Denk <wd@denx.de>

Constify getenv(), setenv() and hash code functions

This is needed to get rid of build warnings like

main.c:311: warning: passing argument 2 of 'setenv' discards qualifiers from pointer target type

which result from commit 09c2e90 "unify version_string".

Signed-off-by: Wolfgang Denk <wd@denx.de>
Cc: Andreas Bie�mann <andreas.devel@googlemail.com>


# a000b795 05-Apr-2011 Kim Phillips <kim.phillips@freescale.com>

common: add a grepenv command

u-boot environments, esp. when boards are shared across multiple
users, can get pretty large and time consuming to visually parse.
The grepenv command this patch adds can be used in lieu of printenv
to facilitate searching. grepenv works like printenv but limits
its output only to environment strings (variable name and value
pairs) that match the user specified substring.

the following examples are on a board with a 5313 byte environment
that spans multiple screen pages:

Example 1: summarize ethernet configuration:

=> grepenv eth TSEC
etact=FM1@DTSEC2
eth=FM1@DTSEC4
ethact=FM1@DTSEC2
eth1addr=00:E0:0C:00:8b:01
eth2addr=00:E0:0C:00:8b:02
eth3addr=00:E0:0C:00:8b:03
eth4addr=00:E0:0C:00:8b:04
eth5addr=00:E0:0C:00:8b:05
eth6addr=00:E0:0C:00:8b:06
eth7addr=00:E0:0C:00:8b:07
eth8addr=00:E0:0C:00:8b:08
eth9addr=00:E0:0C:00:8b:09
ethaddr=00:E0:0C:00:8b:00
netdev=eth0
uprcw=setenv ethact $eth;setenv filename p4080ds/R_PPSXX_0xe/rcw_0xe_2sgmii_rev2_high.bin;setenv start 0xe8000000;protect off all;run upimage;protect on all
upuboot=setenv ethact $eth;setenv filename u-boot.bin;setenv start eff80000;protect off all;run upimage;protect on all
upucode=setenv ethact $eth;setenv filename fsl_fman_ucode_P4080_101_6.bin;setenv start 0xef000000;protect off all;run upimage;protect on all
usdboot=setenv ethact $eth;tftp 1000000 $dir/$bootfile;tftp 2000000 $dir/initramfs.cpio.gz.uboot;tftp c00000 $dir/p4080ds-usdpaa.dtb;setenv bootargs root=/dev/ram rw console=ttyS0,115200 $othbootargs;bootm 1000000 2000000 c00000;
=>

Example 2: detect unused env vars:

=> grepenv etact
etact=FM1@DTSEC2
=>

Example 3: reveal hardcoded variables; e.g., for fdtaddr:

=> grepenv fdtaddr
fdtaddr=c00000
nfsboot=setenv bootargs root=/dev/nfs rw nfsroot=$serverip:$rootpath ip=$ipaddr:$serverip:$gatewayip:$netmask:$hostname:$netdev:off console=$consoledev,$baudrate $othbootargs;tftp $loadaddr $bootfile;tftp $fdtaddr $fdtfile;bootm $loadaddr - $fdtaddr
ramboot=setenv bootargs root=/dev/ram rw console=$consoledev,$baudrate $othbootargs;tftp $ramdiskaddr $ramdiskfile;tftp $loadaddr $bootfile;tftp $fdtaddr $fdtfile;bootm $loadaddr $ramdiskaddr $fdtaddr
=> grep $fdtaddr
fdtaddr=c00000
my_boot=bootm 0x40000000 0x41000000 0x00c00000
my_dtb=tftp 0x00c00000 $prefix/p4080ds.dtb
nohvboot=tftp 1000000 $dir/$bootfile;tftp 2000000 $dir/$ramdiskfile;tftp c00000 $dir/$fdtfile;setenv bootargs root=/dev/ram rw ramdisk_size=0x10000000 console=ttyS0,115200;bootm 1000000 2000000 c00000;
=>

This patch also enables the grepenv command by default on
corenet_ds based boards (and repositions the DHCP command
entry to keep the list sorted).

Signed-off-by: Kim Phillips <kim.phillips@freescale.com>
Cc: Kumar Gala <kumar.gala@freescale.com>
Cc: Andy Fleming <afleming@freescale.com>


# af4d9074 04-Apr-2011 Kim Phillips <kim.phillips@freescale.com>

env: fix env var autocompletion

commit 560d424b6d7cd4205b062ad95f1b104bd4f8bcc3 "env: re-add
support for auto-completion" fell short of its description -
the 'used' logic in hmatch_r was reversed - 'used' is 0 if
the hash table entry is not used, or -1 if deleted. This
patch makes hmatch_r actually match on valid ('used') entries,
instead of skipping them and failing to match anything.

typing 'printenv tft' and hitting 'tab' now displays valid
choices for variable names.

Signed-off-by: Kim Phillips <kim.phillips@freescale.com>
Cc: Mike Frysinger <vapier@gentoo.org>
Tested-by: Mike Frysinger <vapier@gentoo.org>


# c81c1222 21-Mar-2011 Peter Barada <peter.barada@logicpd.com>

Fix hash table deletion to prevent lost entries

Use negative used value to mark deleted entry. Search keeps probing
past deleted entries. Adding an entry uses first deleted entry when
it hits end of probe chain.

Initially found that "ramdiskimage" and "preboot" collide modulus 347,
causing "preboot" to be inserted at idx 190, "ramdiskimage" at idx 191.
Previous to this fix when "preboot" is deleted, "ramdiskimage" is
orphaned.

Signed-off-by: Peter Barada <peter.barada@logicpd.com>
Tested-by: Wolfgang Denk <wd@denx.de>


# 560d424b 17-Dec-2010 Mike Frysinger <vapier@gentoo.org>

env: re-add support for auto-completion

Currently, only basic completion is supported (no globs), but this is
what we had previously.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>


# 2eb1573f 08-Dec-2010 Mike Frysinger <vapier@gentoo.org>

hashtable: drop all non-reentrant versions

The non-reentrant versions of the hashtable functions operate on a single
shared hashtable. So if two different people try using these funcs for
two different purposes, they'll cause problems for the other.

Avoid this by converting all existing hashtable consumers over to the
reentrant versions and then punting the non-reentrant ones.

Signed-off-by: Mike Frysinger <vapier@gentoo.org>


# 071bc923 27-Oct-2010 Wolfgang Denk <wd@denx.de>

Coding Style cleanup

Signed-off-by: Wolfgang Denk <wd@denx.de>


# fc5fc76b 01-Oct-2010 Andreas Bießmann <andreas.devel@googlemail.com>

lib/hashtable.c: add CONFIG_ENV_MIN_ENTRIES

This patch adds a new config parameter for adjusting the calculation of
hash table size when importing a buffer.

When importing a extremely small buffer (e.g. the default_environment)
the old calculation generated a hash table which could hold at most the
buffer content but no more entires.

The new calculation add a fixed number of entries to the result to fit
better for small import buffers. This amount may be configured by the
user in board file to adjust the behaviour.

Signed-off-by: Andreas Bie�mann <andreas.devel@googlemail.com>


# ea882baf 20-Jun-2010 Wolfgang Denk <wd@denx.de>

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>


# a6826fbc 20-Jun-2010 Wolfgang Denk <wd@denx.de>

Add hash table support as base for new environment code

This implementation is based on code from uClibc-0.9.30.3 but was
modified and extended for use within U-Boot.

Major modifications and extensions:

* hsearch() [modified / extended]:
- While the standard version does not make any assumptions about
the type of the stored data objects at all, this implementation
works with NUL terminated strings only.
- Instead of storing just pointers to the original objects, we
create local copies so the caller does not need to care about the
data any more.
- The standard implementation does not provide a way to update an
existing entry. This version will create a new entry or update an
existing one when both "action == ENTER" and "item.data != NULL".
- hsearch_r(): Instead of returning 1 on success, we return the
index into the internal hash table, which is also guaranteed to be
positive. This allows us direct access to the found hash table
slot for example for functions like hdelete().
* hdelete() [added]:
- The standard implementation of hsearch(3) does not provide any way
to delete any entries from the hash table. We extend the code to
do that.
* hexport() [added]:
- Export the data stored in the hash table in linearized form:
Entries are exported as "name=value" strings, separated by an
arbitrary (non-NUL, of course) separator character. This allows to
use this function both when formatting the U-Boot environment for
external storage (using '\0' as separator), but also when using it
for the "printenv" command to print all variables, simply by using
as '\n" as separator. This can also be used for new features like
exporting the environment data as text file, including the option
for later re-import.
- The entries in the result list will be sorted by ascending key
values.
* himport() [added]:
- Import linearized data into hash table. This is the inverse
function to hexport(): it takes a linear list of "name=value"
pairs and creates hash table entries from it.
- Entries without "value", i. e. consisting of only "name" or
"name=", will cause this entry to be deleted from the hash table.
- The "flag" argument can be used to control the behaviour: when
the H_NOCLEAR bit is set, then an existing hash table will kept,
i. e. new data will be added to an existing hash table;
otherwise, old data will be discarded and a new hash table will
be created.
- The separator character for the "name=value" pairs can be
selected, so we both support importing from externally stored
environment data (separated by NUL characters) and from plain text
files (entries separated by newline characters).
- To allow for nicely formatted text input, leading white space
(sequences of SPACE and TAB chars) is ignored, and entries
starting (after removal of any leading white space) with a '#'
character are considered comments and ignored.
- NOTE: this means that a variable name cannot start with a '#'
character.
- When using a non-NUL separator character, backslash is used as
escape character in the value part, allowing for example fo
multi-line values.
- In theory, arbitrary separator characters can be used, but only
'\0' and '\n' have really been tested.

Signed-off-by: Wolfgang Denk <wd@denx.de>