How u-boot bootloader reads/saves its environment Variables?

Thang Le picture Thang Le · Dec 11, 2013 · Viewed 22.9k times · Source
  • How u-boot bootloader reads/saves its environment Variables ?
  • How we declare address of u-boot environment Variable section in Flash ?

  • From description at here : The U-Boot environment is a block of memory that is kept on persistent storage and copied to RAM when U-Boot starts.

What's meaning of " copied to RAM" ?

U-boot will copy block of memory of environment variables to RAM ?

Thanks

Answer

Joe Kul picture Joe Kul · Dec 11, 2013

Yes, U-boot will copy block of memory of environment variables to RAM.

The persistent storage, where the block comes from, is platform-specific. Some common storage options (and source file handling that storage option):

NOR flash   env/flash.c
SPI flash   env/sf.c
MMC         env/mmc.c

CONFIG_ definitions in include/configs/yourboard.h will determine the details. For example, for SPI flash mapped at top of memory, maybe:

#define CONFIG_ENV_IS_IN_SPI_FLASH
#define CONFIG_ENV_SIZE    0x00001000
#define CONFIG_ENV_ADDR    0xFFFFF000

CONFIG_ENV_ADDR is address of u-boot environment Variable section in Flash.

Note that u-boot automatically creates a CRC32 over this section when writing the environment to persistent storage. That CRC is checked when environment is read on startup. If CRC check does not pass, the stored environment is not used; instead a new default environment hardcoded into the program code is used, that is a special case.

During U-Boot initialization, the environment variables are imported into a hash table. In operation, all read/write operations, and all "printenv" (display environment variable) and "setenv" (set environment variable) commands use those table entries. Any changes are unsaved until command "saveenv" is done, which writes to the persistent storage.

For more info, see u-boot/common/cmd_nvedit.c lines 14-24 and u-boot/README lines 3474-3881 (line numbers are for v2013.10).