Checking for environment CRC in u-boot script

Viewed 26

When I bring up my fresh out of the press system, u-boot comes with default environment (that I fine-crafted when compiling u-boot. That is expected.

Loading Environment from MMC... *** Warning - bad CRC, using default environment

However, to automate deployment I would like to do saveenv to initialize the environment in MMC but would like to do it only when there is no valid environment in the storage.

I am looking for a way to determine whether the environment has a bad CRC (such as uninitialized) and initialize it with the default using savenev. Once I initialize the environment I can further automate settings (such as ethaddr) from my deployment shell script using fw_setenv.

I didn't find a way how to do it programmatically within the u-boot script.

1 Answers

This might not be the most elegant solution but at least I have a working workaround:

I added a script that is checking if a specific environment variable env_written serving as a flag is set. If not, the script creates (sets) the flag and performs saveenv. The flag variable makes sure the script runs only once and not at future boots.

setenv init_env "if test -n '"$env_written"'; then ; else setenv env_written 1; echo '"... inaugural saveenv"'; saveenv; fi"

Lastly I added an invocation of this script in the bootcmd script that is always executed.

setenv bootcmd "run init_env; echo '"bootcmd: "'; run loadfpga; bridge enable; run distro_bootcmd"

It does the job :-)

For reference - below is the encoding suitable for copy-pasting to your config_distro_bootcmd.h if you want to get this behavior out of the box:

"init_env="                               \
"if test -n \"$env_written\"; then ; "    \
"else "                                   \
   "setenv env_written 1; "               \
   "echo \"... inaugural saveenv\"; "     \
   "saveenv; "                            \
"fi;\0"

and

#define CONFIG_BOOTCOMMAND "run init_env; echo \"bootcmd: \"; run loadfpga; bridge enable; run distro_bootcmd"

Enjoy better scripting possibilities!

Related