aboutsummaryrefslogtreecommitdiff
path: root/tools/env/fw_env.c
AgeCommit message (Collapse)AuthorFilesLines
2022-12-08tools: env: fw_env: Fix unused-result warningJaehoon Chung1-1/+9
Fix unused-result warning about fread. tools/env/fw_env.c: In function ‘find_nvmem_device’: tools/env/fw_env.c:1751:3: warning: ignoring return value of ‘fread’, declared with attribute warn_unused_result [-Wunused-result] 1751 | fread(buf, sizeof(buf), 1, fp); | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Signed-off-by: Jaehoon Chung <jh80.chung@samsung.com>
2022-09-29tools: env: Fix missing closedir in ubi_get_volnum_by_nameMiaoqian Lin1-1/+4
The function calls opendir() but missing the corresponding closedir() before exit the function. Add missing closedir() to fix it. Signed-off-by: Miaoqian Lin <linmq006@gmail.com>
2022-07-08fw_env: add fallback to Linux's NVMEM based accessRafał Miłecki1-3/+67
A new DT binding for describing environment data block has been added in Linux's commit 5db1c2dbc04c ("dt-bindings: nvmem: add U-Boot environment variables binding"). Once we get a proper Linux NVMEM driver it'll be possible to use Linux's binary interface for user-space as documented in the: https://www.kernel.org/doc/html/latest/driver-api/nvmem.html This commits makes fw_env fallback to looking for a compatible NVMEM device in case config file isn't present. In a long term this may make config files redundant and avoid code (info) duplication. Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
2022-02-11fw_env: simplify logic & code paths in the fw_env_open()Rafał Miłecki1-46/+31
Environment variables can be stored in two formats: 1. Single entry with header containing CRC32 2. Two entries with extra flags field in each entry header For that reason fw_env_open() has two main code paths and there are pointers for CRC32/flags/data. Previous implementation was a bit hard to follow: 1. It was checking for used format twice (in reversed order each time) 2. It was setting "environment" global struct fields to some temporary values that required extra comments explaining it This change simplifies that code: 1. It introduces two clear code paths 2. It sets "environment" global struct fields values only once it really knows them To be fair there are *two* crc32() calls now and an extra pointer variable but that should be cheap enough and worth it. Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
2022-02-11fw_env: make flash_io() take buffer as an argumentRafał Miłecki1-16/+16
It's usually easier to understand code & follow it if all arguments are passed explicitly. Many coding styles also discourage using global variables. Behaviour of flash_io() was a bit unintuitive as it was writing to a buffer referenced in a global struct. That required developers to remember how it works and sometimes required hacking "environment" global struct variable to read data into a proper buffer. Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
2021-12-20fw_setenv: Unbreak fw_setenv caused by buggy MEMISLOCKED useJoakim Tjernlund1-7/+8
Commit "fw_setenv: lock the flash only if it was locked before" checks for Locked status with uninitialized erase data. Address by moving the test for MEMISLOCKED. Fixes: 8a726b852502 ("fw_setenv: lock the flash only if it was locked before") Signed-off-by: Joakim Tjernlund <joakim.tjernlund@infinera.com>
2021-09-02tools: env: Handle shorter read callsThibault Ferrante1-10/+12
On some cases, the actual number of bytes read can be shorter than what was requested. This can be handled gracefully by taking this difference into account instead of exiting. Signed-off-by: Thibault Ferrante <thibault.ferrante@gmail.com>
2021-01-22tools: env: return error if ubi_update_start() failsMartin Hundebøll1-1/+1
The UBI_IOCVOLUP ioctl can fail if exclusive access to the volume isn't obtained. If this happens, the flush operation doesn't return error, leaving the caller without knowledge of missing flush. Fix this by forwarding the error (-1) from ubi_update_start(). Fixes: 34255b92e6e ("tools: env: Add support for direct read/write UBI volumes") Signed-off-by: Martin Hundebøll <martin@geanix.com>
2020-08-05tools: env: Avoid an uninited warning with was_lockedSimon Glass1-1/+1
Set this variable to 0 to avoid a warning about an unused variable. This happens on gcc 7.5.0 for me. Signed-off-by: Simon Glass <sjg@chromium.org>
2020-08-05tools: env: Fix printf() warning in fw_envSimon Glass1-2/+2
The printf() string produces a warning about %d not matching size_t. Fix it and put the format string on one line to avoid a checkpatch warning. Signed-off-by: Simon Glass <sjg@chromium.org>
2020-07-31fw_setenv: lock the flash only if it was locked beforeIvan Mikhaylov1-5/+19
With current implementation of fw_setenv, it is always locks u-boot-env region if lock interface is implemented for such mtd device. You can not control lock of this region with fw_setenv, there is no option for it in config or in application itself. Because of this situation may happen problems like in this thread on xilinx forum: https://forums.xilinx.com/t5/Embedded-Linux/Flash-be-locked-after-use-fw-setenv-from-user-space /td-p/1027851 A short summary of that link is: some person has issue with some spi chip which has lock interface but doesn't locks properly which leads to lock of whole flash memory on lock of u-boot-env region. As resulted solution hack was added into spi-nor.c driver for this chip with lock disablement. Instead fix this problem by adding logic to fw_setenv only lock the flash if it was already locked when we attempted to use it. Signed-off-by: Ivan Mikhaylov <fr0st61te@gmail.com>
2020-06-02tools: fw_env: Fix warning when reading too littleHarald Seiler1-1/+7
When using CONFIG_ENV_IS_IN_FAT and the config-file specifies a size larger than what U-Boot wrote into the env-file, a confusing error message is shown: $ fw_printenv Read error on /boot/uboot.env: Success Fix this by showing a different error message when read returns too little data. Signed-off-by: Harald Seiler <hws@denx.de>
2020-04-27tools: fw_env: use erasesize from MEMGETINFO ioctlRasmus Villemoes1-0/+3
We have a board with several revisions. The older ones use a nor flash with 64k erase size, while the newer have a flash with 4k sectors. The environment size is 8k. Currently, we have to put a column containing 0x10000 (64k) in fw_env.config in order for it to work on the older boards. But that ends up wasting quite a lot of time on the newer boards that could just erase the 8k occupied by the environment - strace says the 64k erase takes 0.405 seconds. With this patch, as expected, that's about an 8-fold better, at 0.043 seconds. Having different fw_env.config files for the different revisions is highly impractical, and the correct information is already available right at our fingertips. So use the erasesize returned by the MEMGETINFO ioctl when the fourth and fifth columns (sector size and #sectors, respectively) are absent or contain 0, a case where the logic previously used to use the environment size as erase size (and consequently computed ENVSECTORS(dev) as 1). As I'm only testing this on a NOR flash, I'm only changing the logic for that case, though I think it should be possible for the other types as well. Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk>
2019-12-20fw_setenv: avoid writing environment when nothing has changedRasmus Villemoes1-1/+19
In the case where one deletes an already-non-existing variable, or sets a variable to the value it already has, there is no point in writing the environment back, thus reducing wear on the underlying storage device. In the case of redundant environments, if the two environments differ (e.g. because one is corrupt), make sure that any call of fw_setenv causes the two to become synchronized, even if the fw_setenv call does not change anything in the good copy. Signed-off-by: Rasmus Villemoes <rasmus.villemoes@prevas.dk> Acked-by: Joe Hershberger <joe.hershberger@ni.com>
2019-12-02crc32: Use the crc.h header for crc functionsSimon Glass1-0/+1
Drop inclusion of crc.h in common.h and use the correct header directly instead. With this we can drop the conflicting definition in fw_env.h and rely on the crc.h header, which is already included. Signed-off-by: Simon Glass <sjg@chromium.org> Reviewed-by: Tom Rini <trini@konsulko.com>
2019-09-24fw_env: fix build errorPierre-Jean Texier1-1/+2
The following error appears: tools/env/fw_env.c:1149:25: error: lvalue required as unary ‘&’ operand rc = write(fd, &ENV_REDUND_OBSOLETE, sizeof(ENV_REDUND_OBSOLETE)); Fixes: d3716dd ("env: Rename the redundancy flags") Signed-off-by: Pierre-Jean Texier <pjtexier@koncepto.io> Tested-by: Joris Offouga <offougajoris@gmail.com> Tested-by: Heiko Schocher <hs@denx.de> Suggested-by: Heiko Schocher <hs@denx.de> Acked-by: Joe Hershberger <joe.hershberger@ni.com>
2019-09-24fw_env: remove duplicated definitionsPierre-Jean Texier1-7/+0
Since commit d3716dd ("env: Rename the redundancy flags"), the definitions of ENV_REDUND_OBSOLETE & ENV_REDUND_ACTIVE was moved to env.h. Fixes: tools/env/fw_env.c:122:22: error: ‘ENV_REDUND_ACTIVE’ redeclared as different kind of symbol static unsigned char ENV_REDUND_ACTIVE = 1; ^~~~~~~~~~~~~~~~~ In file included from tools/env/fw_env.c:13: include/env.h:63:2: note: previous definition of ‘ENV_REDUND_ACTIVE’ was here ENV_REDUND_ACTIVE = 1, ^~~~~~~~~~~~~~~~~ tools/env/fw_env.c:127:22: error: ‘ENV_REDUND_OBSOLETE’ redeclared as different kind of symbol static unsigned char ENV_REDUND_OBSOLETE; ^~~~~~~~~~~~~~~~~~~ In file included from tools/env/fw_env.c:13: include/env.h:62:2: note: previous definition of ‘ENV_REDUND_OBSOLETE’ was here ENV_REDUND_OBSOLETE = 0, Signed-off-by: Pierre-Jean Texier <pjtexier@koncepto.io> Tested-by: Joris Offouga <offougajoris@gmail.com> Tested-by: Heiko Schocher <hs@denx.de> Acked-by: Joe Hershberger <joe.hershberger@ni.com>
2019-08-11env: Rename the redundancy flagsSimon Glass1-10/+13
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>
2019-08-11env: Move env_set() to env.hSimon Glass1-0/+1
Move env_set() over to the new header file. Acked-by: Joe Hershberger <joe.hershberger@ni.com> Signed-off-by: Simon Glass <sjg@chromium.org>
2019-06-20fw_env: Add missing write failure checkAlex Kiernan1-1/+1
If flash_write fails, whilst we propagate this up to our caller, we need to avoid swapping in the new file (if we're on a filesystem) in this case. Fixes: dbc34323796b ("tools: env: Implement atomic replace for filesystem") Signed-off-by: Alex Kiernan <alex.kiernan@gmail.com>
2019-05-05env: add missing newlinePhilip Molloy1-1/+1
Signed-off-by: Philip Molloy <philip@philipmolloy.com>
2019-04-26fw_env: Fix double freeBjörn Stenberg1-1/+1
addr0 is being freed twice, leading to segmentation fault Signed-off-by: Björn Stenberg <bjorn@haxx.se>
2018-07-01Revert "fw_printenv: Don't bail out directly after one env read error"Tom Rini1-15/+9
As pointed out by Wolfgang Denk, the problem with this fix is that while interactive users will see that we have found one part of the environment failed and are using the other, progmatic use will not see this and can lead to problems. Signed-off-by: Tom Rini <trini@konsulko.com>
2018-06-27fw_printenv: Don't bail out directly after one env read errorJoe Hershberger1-9/+15
When using a redundant environment a read error should simply mean to not use that copy instead of giving up completely. The other copy may be just fine. Signed-off-by: Joe Hershberger <joe.hershberger@ni.com> Signed-off-by: Ioan-Adrian Ratiu <adrian.ratiu@ni.com>
2018-06-13u-boot: Fix several typosShyam Saini1-2/+2
's/environemnt/environment/' and 's/Environemnt/Environment/' Signed-off-by: Shyam Saini <shyam@amarulasolutions.com>
2018-06-13tools: env: Use getline rather than fgets when reading config/scriptAlex Kiernan1-19/+21
When reading the config file, or a script file, use getline rather than fgets so line lengths aren't limited by the size of a compiled in buffer (128 characters for config, 1024 for scripts). Rename 'dump' to 'line' so it's clear we're working with a line of text. Signed-off-by: Alex Kiernan <alex.kiernan@gmail.com>
2018-05-07SPDX: Convert all of our single license tags to Linux Kernel styleTom Rini1-2/+1
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>
2018-03-19tools: env: Implement atomic replace for filesystemAlex Kiernan1-3/+80
If the U-Boot environment is stored in a regular file and redundant operation isn't set, then write to a temporary file and perform an atomic rename. Signed-off-by: Alex Kiernan <alex.kiernan@gmail.com>
2018-03-19tools: env: Refactor write path of flash_io()Alex Kiernan1-42/+50
Extract write path of flash_io() into a separate function. This patch should be a functional no-op. Signed-off-by: Alex Kiernan <alex.kiernan@gmail.com> Reviewed-by: Stefano Babic <sbabic@denx.de>
2018-03-19tools: env: Fix CamelCasing style violationAlex Kiernan1-9/+9
Replace HaveRedundEnv with have_redund_env to fix style violation. Signed-off-by: Alex Kiernan <alex.kiernan@gmail.com>
2018-03-19tools: env: Pass through indentAlex Kiernan1-176/+170
Pass tools/env/fw_env.c through indent to correct style violations. This commit consists of only one non-whitespace change: tools/env/fw_env.c:549: error: do not use assignment in if condition Signed-off-by: Alex Kiernan <alex.kiernan@gmail.com>
2018-03-05tools/env: allow equal sign as key value separationStefan Agner1-1/+1
Treat the first equal sign as a key/value separation too. This makes the script files compatible with mkenvimage input file format. It won't support variables with equal signs anymore, but this seems not really like a loss. Signed-off-by: Stefan Agner <stefan.agner@toradex.com>
2018-02-14Fix --noheader on fw_printenvAlex Kiernan1-1/+1
The single argument `--noheader' is expecting isn't taken from getopt parsing, but instead from the remaining argv arguments. Signed-off-by: Alex Kiernan <alex.kiernan@gmail.com>
2018-01-28tools: remove unused retJelle van der Waa1-2/+0
Remove unused ret from fw_env_flush. Signed-off-by: Jelle van der Waa <jelle@vdwaa.nl> Reviewed-by: Simon Glass <sjg@chromium.org>
2017-11-29tools: env: Add support for direct read/write UBI volumesS. Lockwood-Childs1-2/+253
Up to now we were able to read/write environment data from/to UBI volumes only indirectly by gluebi driver. This driver creates NAND MTD on top of UBI volumes, which is quite a workaroung for this use case. Add support for direct read/write UBI volumes in order to not use obsolete gluebi driver. Forward-ported from this patch: http://patchwork.ozlabs.org/patch/619305/ Original patch: Signed-off-by: Marcin Niestroj <m.niestroj@grinn-global.com> Forward port: Signed-off-by: S. Lockwood-Childs <sjl@vctlabs.com>
2017-11-21env: Remove CONFIG_ENV_AES supportTom Rini1-79/+0
This support has been deprecated since v2017.09 due to security issues. We now remove this support. Signed-off-by: Tom Rini <trini@konsulko.com>
2017-09-05env: Fix out of tree building of tools-allTom Rini1-1/+1
With the move of environment code from common/ to env/ a number of changes needed to be made to various make targets. We missed updating some of the files required for out of tree builds of the tools. Correct the 'environ' target to know that we need to work under tools/env/ still (not tools/environ/) and then update the wrappers in env_attr.c and env_flags.c to point to the new correct file. Reported-by: Peter Robinson <pbrobinson@gmail.com> Tested-by: Peter Robinson <pbrobinson@gmail.com> Signed-off-by: Tom Rini <trini@konsulko.com>
2017-09-03fix: fw_env: Prevent writing error message on special files, which don't ↵Lukasz Majewski1-2/+4
support fsync According to fsync specification [1] some special files (e.g., a pipe, FIFO, or socket) don't support synchronization and return either EROFS or EINVAL. On the linux side the sys_fsync -> do_fsync() checks if the requested file has f_op->fsync defined. If not it returns EINVAL [2]. This commit prevents writing error messages for files (devices), which do not support fsync(). [1] - http://man7.org/linux/man-pages/man2/fsync.2.html [2] - http://elixir.free-electrons.com/linux/v4.13-rc6/source/fs/sync.c#L183 Signed-off-by: Lukasz Majewski <lukma@denx.de> Acked-by: Michael Heimpold <mhei@heimpold.de>
2017-08-16env: Rename setenv() to env_set()Simon Glass1-1/+1
We are now using an env_ prefix for environment functions. Rename setenv() for consistency. Also add function comments in common.h. Suggested-by: Wolfgang Denk <wd@denx.de> Signed-off-by: Simon Glass <sjg@chromium.org>
2017-07-22tools/fw_env: use fsync to ensure that data is physically storedMichael Heimpold1-0/+12
Closing a file descriptor does not guarantee that the data has been successfully saved to disk, as the kernel might defer the write. Signed-off-by: Michael Heimpold <mhei@heimpold.de>
2017-04-18tools/env: avoid memory leak in fw_setenvxypron.glpk@gmx.de1-0/+3
If realloc fails we should release the old buffer. Signed-off-by: Heinrich Schuchardt <xypron.glpk@gmx.de> Reviewed-by: Tom Rini <trini@konsulko.com> Reviewed-by: Simon Glass <sjg@chromium.org>
2017-04-12env: fix memory leak in fw_env routinesStefano Babic1-16/+56
fw_env_open allocates buffers to store the environment, but these buffers are never freed. This becomes quite nasty using the fw_ tools as library, because each access to the environment (even just reading a variable) generates a memory leak equal to the size of the environment. Fix this renaming fw_env_close() as fw_env_flush(), because the function really flushes the environment from RAM to storage, and add a fw_env_close function to free the allocated resources. Signed-off-by: Stefano Babic <sbabic@denx.de>
2017-04-12env: split fw_env.h in public and private partsStefano Babic1-0/+1
Move U-Boot private data into a separate file. This lets export fw_env.h to be used by external programs that want to change the environment using the library built in tools/env. Signed-off-by: Stefano Babic <sbabic@denx.de>
2016-11-28tools/env: fix environment alignment tests for block devicesMax Krummenacher1-27/+33
commit 183923d3e412500bdc597d1745e2fb6f7f679ec7 enforces that the environment must start at an erase block boundary. For block devices the sample fw_env.config does not mandate a erase block size for block devices. A missing setting defaults to the full env size. Depending on the environment location the alignment check now errors out for perfectly legal settings. Fix this by defaulting to the standard blocksize of 0x200 for environments stored in a block device. That keeps the fw_env.config files for block devices working even with that new check. Signed-off-by: Max Krummenacher <max.krummenacher@toradex.com>
2016-10-06Suspected Spam: Do not open attachements![PATCH 4/6] tools/env: ↵Andreas Fenkart1-7/+8
flash_write_buf: enforce offset to be start of environment This allows to take advantage of the environment being block aligned. This is not a new constraint. Writes always start at the begin of the environment, since the header with CRC/length as there. Every environment modification requires updating the header Signed-off-by: Andreas Fenkart <andreas.fenkart@digitalstrom.com>
2016-10-06tools/env: lookup dev_type directly from flash_read_buf/flash_write_bufAndreas Fenkart1-15/+13
flash_write_buf already looks up size/offset/#sector from struct envdev_s. It can look up mtd_type as well. Same applies to flash_read_buf. Makes the interface simpler Signed-off-by: Andreas Fenkart <andreas.fenkart@digitalstrom.com>
2016-10-06tools/env: pass bad block offset by valueAndreas Fenkart1-5/+5
the offset is not modified by linux ioctl call see mtd_ioctl{drivers/mtd/mtdchar.c} Makes the interface less ambiguous, since the caller can now exclude a modification of blockstart Signed-off-by: Andreas Fenkart <andreas.fenkart@digitalstrom.com>
2016-10-06tools/env: factor out environment_end functionAndreas Fenkart1-17/+15
instead of adhoc computation of the environment end, use a function with a proper name Signed-off-by: Andreas Fenkart <andreas.fenkart@digitalstrom.com>
2016-08-20tools/env: soften warning about erase block alignmentAndreas Fenkart1-1/+1
addon 183923d3e MMC/SATA have no erase blocks, only blocks. Hence the warning about erase block alignment might be confusing in such environment. Signed-off-by: Andreas Fenkart <andreas.fenkart@digitalstrom.com> Reviewed-by: Tom Rini <trini@konsulko.com>
2016-08-20tools/env: return with error if redundant environments have unequal sizeAndreas Fenkart1-3/+2
For double buffering to work, the target buffer must always be big enough to hold all data. This can only be ensured if buffers are of equal size, otherwise one must be smaller and we risk data loss when copying from the bigger to the smaller buffer. Reviewed-by: Simon Glass <sjg@chromium.org> Signed-off-by: Andreas Fenkart <andreas.fenkart@digitalstrom.com> Reviewed-by: Tom Rini <trini@konsulko.com>