U-Boot Environment

From www.chip-community.org
Jump to: navigation, search

HowTo

  1. sudo apt-get install u-boot-tools
  2. echo "/dev/mtdblock3 0 0x400000 0x4000" | sudo tee -a /etc/fw_env.config
  3. fw_printenv
  4. fw_setenv --help

Why?

The U-Boot environment configuration file /etc/fw_env.config has four required items: device, device offset, device size, and device page size.

Using dmesg to see the kernel logs, it can be seen that 5 MTD partitions have been passed in from U-Boot, along with the physical parameters of the flash device.

# dmesg  | grep "nand:"
[    1.140000] nand: device found, Manufacturer ID: 0xad, Chip ID: 0xde
[    1.145000] nand: Hynix H27QCG8T2E5R90BCF 64G 3.3V 8-bit
[    1.155000] nand: 8192 MiB, MLC, erase size: 4096 KiB, page size: 16384, OOB size: 1664
# dmesg | grep -A6 "MTD device"
[    1.700000] 5 ofpart partitions found on MTD device 1c03000.nand
[    1.710000] Creating 5 MTD partitions on "1c03000.nand":
[    1.715000] 0x000000000000-0x000000400000 : "SPL"
[    1.725000] 0x000000400000-0x000000800000 : "SPL.backup"
[    1.735000] 0x000000800000-0x000000c00000 : "U-Boot"
[    1.740000] 0x000000c00000-0x000001000000 : "env"
[    1.750000] 0x000001000000-0x000200000000 : "rootfs"

Not a very pretty way to get this information. Instead, mtdinfo can be used:

# mtdinfo -a
Count of MTD devices:           5
Present MTD devices:            mtd0, mtd1, mtd2, mtd3, mtd4
Sysfs interface supported:      yes

mtd0
Name:                           SPL
Type:                           mlc-nand
Eraseblock size:                4194304 bytes, 4.0 MiB
Amount of eraseblocks:          1 (4194304 bytes, 4.0 MiB)
Minimum input/output unit size: 16384 bytes
Sub-page size:                  16384 bytes
OOB size:                       1664 bytes
Character device major/minor:   90:0
Bad blocks are allowed:         true
Device is writable:             true

mtd1
Name:                           SPL.backup
Type:                           mlc-nand
Eraseblock size:                4194304 bytes, 4.0 MiB
Amount of eraseblocks:          1 (4194304 bytes, 4.0 MiB)
Minimum input/output unit size: 16384 bytes
Sub-page size:                  16384 bytes
OOB size:                       1664 bytes
Character device major/minor:   90:2
Bad blocks are allowed:         true
Device is writable:             true

mtd2
Name:                           U-Boot
Type:                           mlc-nand
Eraseblock size:                4194304 bytes, 4.0 MiB
Amount of eraseblocks:          1 (4194304 bytes, 4.0 MiB)
Minimum input/output unit size: 16384 bytes
Sub-page size:                  16384 bytes
OOB size:                       1664 bytes
Character device major/minor:   90:4
Bad blocks are allowed:         true
Device is writable:             true

mtd3
Name:                           env
Type:                           mlc-nand
Eraseblock size:                4194304 bytes, 4.0 MiB
Amount of eraseblocks:          1 (4194304 bytes, 4.0 MiB)
Minimum input/output unit size: 16384 bytes
Sub-page size:                  16384 bytes
OOB size:                       1664 bytes
Character device major/minor:   90:6
Bad blocks are allowed:         true
Device is writable:             true

mtd4
Name:                           rootfs
Type:                           mlc-nand
Eraseblock size:                4194304 bytes, 4.0 MiB
Amount of eraseblocks:          2044 (8573157376 bytes, 8.0 GiB)
Minimum input/output unit size: 16384 bytes
Sub-page size:                  16384 bytes
OOB size:                       1664 bytes
Character device major/minor:   90:8
Bad blocks are allowed:         true
Device is writable:             true

Thus, /dev/mtd3 is the partition to use for the U-Boot environment. Since /dev/mtd3 is dedicated to environment storage, the device offset is 0.

The environment tools require that the partition length be specified in hex while the page size can be either hex or decimal. Using a couple of shell one-liners the appropriate values can be quickly calculated.

# printf "0x%08x\n" $(( $((0x000000c00000-0x000001000000)) * -1 ))
0x00400000
# printf "0x%x\n" 16384
0x4000

This process is similar for platforms which store their environments on other media, though the device and offset will vary.