14
  • 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

Thang Le
  • 1,419
  • 1
  • 17
  • 25

2 Answers2

14

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).

George Hilliard
  • 15,402
  • 9
  • 58
  • 96
Joe Kul
  • 2,454
  • 16
  • 16
  • 1
    @ Joe Kul, Thanks. If u-boot copies block of memory of environment variables to RAM. Then where we defined the address of this section in RAM ? I surprise that why u-boot doesn't read and import its env section directly from flash to the **hash table** but need copy to RAM then imports later ? – Thang Le Dec 12 '13 at 02:16
  • U-Boot copies pretty much everything from flash to RAM. That is referred to as "relocation". To get the address in RAM for anything that came from flash, you can add "Relocation Offset" (printed at the console) to the flash location that's in u-boot.map. Also see arch/arm/lib/board.c. But relocation is a separate topic. – Joe Kul Dec 12 '13 at 16:49
  • @ Joe Kul: I don't know that u-boot needs a "relocation". For my system, boostrap loads u-boot to RAM, so i think u-boot only needs to read environment variables blocks but no need to relocation. Actually, my u-boot is a binary image, so it cannot perform any relocation. Thanks – Thang Le Dec 13 '13 at 02:02
  • U-Boot will relocate itself, whether it needs it or not. – Joe Kul Dec 13 '13 at 18:00
  • @ Joe Kul:U-boot only relocates itself if it detected that it is running on ROM/Flash. Else, relocation will be skipped if u-boot is running in RAM. Main task of relocate is to **copy** u-boot image from Flash/ROM to RAM then starts executing program from RAM to getting faster execution speed. I'm not wrong, then relocation code is on function **board_init_f** (/arm/lib/board.c) which will call assembly handle **relocation_code** . So, I think that is different with " _how u-boot copies/processes env blocks in Flash to RAM_ ?" – Thang Le Dec 15 '13 at 08:55
6

The address and size of env variables block will be defined in the board headers file. See include/configs/am3517_evm.h for example:

#define CONFIG_SYS_ENV_SECT_SIZE        (128 << 10)     /* 128 KiB */
#define CONFIG_ENV_OFFSET               SMNAND_ENV_OFFSET
#define CONFIG_ENV_ADDR                 SMNAND_ENV_OFFSET

u-boot loads CONFIG_SYS_ENV_SECT_SIZE from SMNAND_ENV_OFFSET. You can change values and then save them via saveenv.

yegorich
  • 4,653
  • 3
  • 31
  • 37