u-boot-brain/env
Sam Protsenko 293a172b67 env: Fix saving environment to "bad CRC" location
In case when the environment on some location is malformed (CRC isn't
matching), there is a chance we won't be able to save the environment to
that location. For example, consider the case when we only have the
environment on eMMC, but it's zeroed out. In that case, we won't be able
to "env save" to it, because of "bad CRC" error. That's happening
because in env_load() function we consider malformed environment as
incorrect one, and  defaulting to the location with highest (0)
priority, which can be different from one we are dealing with right now
(e.g., highest priority can be ENV_FAT on SD card, which is not
inserted, but we want to use ENV_MMC on eMMC, where we were booted
from).

This issue began to reproduce after commit d30ba2315a ("u-boot: remove
driver lookup loop from env_save()") on BeagleBone Black, but that
commit didn't introduce the wrong logic, it just changed the behavior
for default location to use, merely revealing this issue.

To fix that, let's implement next logic in env_load():
  1. Try to find out correct environment; if found -- use it
  2. If working environment wasn't found, but we found malformed one
     (with bad CRC), let's use it for further "env save". But make sure
     to use malformed environment location with highest priority.
  3. If neither correct nor malformed environment was found, let's
     default to environment location with highest priority (0)

Steps to reproduce mentioned issue on BeagleBone Black (fixed in this
patch):

1. Boot from SD card and erase eMMC in U-Boot shell:
   => mmc dev 1
   => mmc erase 0 100000
   => gpt write mmc 1 $partitions
2. Write new SPL and U-Boot to eMMC; the rest of eMMC will stay filled
   with zeroes
3. Boot from eMMC; try to do:
   => env save
4. Observe the error (incorrect behavior). Correct behavior: environment
   should be stored correctly on eMMC, in spite of it has "bad CRC"

Fixes: d30ba2315a ("u-boot: remove driver lookup loop from env_save()")
Signed-off-by: Sam Protsenko <semen.protsenko@linaro.org>
Reviewed-by: Simon Goldschmidt <simon.k.r.goldschmidt@gmail.com>
2019-01-26 08:13:56 -05:00
..
attr.c SPDX: Convert all of our single license tags to Linux Kernel style 2018-05-07 09:34:12 -04:00
callback.c SPDX: Convert all of our single license tags to Linux Kernel style 2018-05-07 09:34:12 -04:00
common.c env: common: Return specific error code on bad CRC 2019-01-26 08:13:56 -05:00
eeprom.c SPDX: Convert all of our single license tags to Linux Kernel style 2018-05-07 09:34:12 -04:00
embedded.c SPDX: Convert all of our single license tags to Linux Kernel style 2018-05-07 09:34:12 -04:00
env.c env: Fix saving environment to "bad CRC" location 2019-01-26 08:13:56 -05:00
ext4.c env: common: accept flags on reset to default env 2018-07-19 16:17:58 -04:00
fat.c env: common: accept flags on reset to default env 2018-07-19 16:17:58 -04:00
flags.c SPDX: Convert all of our single license tags to Linux Kernel style 2018-05-07 09:34:12 -04:00
flash.c env: allow flash and nand env driver to compile together 2018-12-04 13:07:13 -08:00
Kconfig arm64: versal: Add support for new Xilinx Versal ACAPs 2018-10-16 16:53:21 +02:00
Makefile env: Simplify Makefile using $(SPL_TPL_) 2018-07-21 12:24:31 -04:00
mmc.c env: common: accept flags on reset to default env 2018-07-19 16:17:58 -04:00
nand.c env: allow flash and nand env driver to compile together 2018-12-04 13:07:13 -08:00
nowhere.c SPDX: Convert all of our single license tags to Linux Kernel style 2018-05-07 09:34:12 -04:00
nvram.c SPDX: Convert all of our single license tags to Linux Kernel style 2018-05-07 09:34:12 -04:00
onenand.c SPDX: Convert all of our single license tags to Linux Kernel style 2018-05-07 09:34:12 -04:00
remote.c SPDX: Convert all of our single license tags to Linux Kernel style 2018-05-07 09:34:12 -04:00
sata.c env: sata: Fix saveenv issue 2019-01-09 07:13:32 -05:00
sf.c Revert "env: sf: fix environment in SPI NOR" 2019-01-21 11:57:58 -05:00
ubi.c env: ubi: Add missing ENV_NAME 2018-08-24 16:00:48 -04:00