Problem
In bootloader the environment variables are accessible. The values could be accessed and modified perfectly from bootloader cli (hush shell).
When booting to the linux kernel and accessing from bash shell (userspace) through fw_printenv
the environment variables are not accessible. Getting following logs:
Warning: Bad CRC, using default environment
Setup:
- Using
MMC
storage - The
/etc/fw_env.config
file has following entry and the offset and size are same from uboot’s.config
file
/dev/mmcblk0p2 0x3f8000 0x8000
Uboot Environment in RK3588
Uboot Config
config ENV_OFFSET
hex "Environment offset"
depends on !ENV_IS_IN_UBI
depends on !ENV_IS_NOWHERE || ENVF
default 0x0 if ENVF
default 0x3f8000
help
Offset from the start of the device (or partition)
config ENV_SIZE
hex "Environment size"
default 0x8000
help
Size of the environment storage area
Parameters
CONFIG_ENV_SIZE=0x8000 --> 32K
CONFIG_ENV_OFFSET=0x3f8000 --> 4096-32=4064 4096K=4M
u-boot/tools/env/fw_env.c
Either crc0_ok = (crc0 == *environment.crc);
is invalid
I finally tracked the problem down. Basically it was a misunderstanding about the precise meaning of fields in the “fw_env.config” file. Specifically the ’environment size’ field. This should be the same value as the “CONFIG_ENV_SIZE” define in the U-Boot configuration header file. link
Debugging uboot and fw_printenv tool
- Print crc while ‘saveenv’ in uboot and fw_printenv in user space. And compare both.
- See how the partition
mmcblk0p2
is divided - The actual partition where the env is written is diffrent then the mounted one.
Block Device Logs
root@HOLIS-NVR:~# cat /proc/partitions
major minor #blocks name
1 0 4096 ram0
179 0 3702784 mmcblk0
179 1 1024 mmcblk0p1
179 2 4096 mmcblk0p2 # uboot
179 3 46080 mmcblk0p3 # kernel
179 4 3644384 mmcblk0p4 # fs
root@HOLIS-NVR:~# fdisk -l /dev/mmcblk0
Found valid GPT with protective MBR; using GPT
Disk /dev/mmcblk0: 7405568 sectors, 3616M
Logical sector size: 512
Disk identifier (GUID): 73987b6b-4974-4c94-a3e8-58ab2eb7a946
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 7405534
Number Start (sector) End (sector) Size Name
1 8192 10239 1024K security
2 16384 24575 4096K uboot
3 24576 116735 45.0M boot
4 116736 7405504 3558M rootfs
Kernel Bootup Logs
[ 12.805093] mmc0: SDHCI controller on fe2e0000.mmc [fe2e0000.mmc] using ADMA
[ 12.939494] mmc0: Host Software Queue enabled
[ 12.939504] mmc0: new HS400 MMC card at address 0001
[ 12.939740] mmcblk0: mmc0:0001 MK2704 3.53 GiB
[ 12.939829] mmcblk0boot0: mmc0:0001 MK2704 partition 1 2.00 MiB
[ 12.939915] mmcblk0boot1: mmc0:0001 MK2704 partition 2 2.00 MiB
[ 12.939977] mmcblk0rpmb: mmc0:0001 MK2704 partition 3 512 KiB, chardev (238:0)
[ 12.942662] mmcblk0: p1 p2 p3 p4
[ 20.556301] EXT4-fs (mmcblk0p4): recovery complete
[ 20.556675] EXT4-fs (mmcblk0p4): mounted filesystem with ordered data mode. Opts: (null)
[ 20.670871] EXT4-fs (mmcblk0p4): re-mounted. Opts: (null)
Partition Table
0x00000800@0x00002000(security)
0x00002000@0x00004000(uboot)
0x00008000@0x00001FC0(uboot-env)
0x00016800@0x00006000(boot)
0x007307DF@0x0001c800(rootfs:grow)
Comparison with RK3568
Block Device Logs
[root@Matrix-NVR ]# cat /proc/partitions
major minor #blocks name
1 0 4096 ram0
179 0 3702784 mmcblk0
179 1 4096 mmcblk0p1 # uboot
179 2 1024 mmcblk0p2 # env
179 3 6144 mmcblk0p3
179 4 32768 mmcblk0p4 # kernel
179 5 3650528 mmcblk0p5 # fs
[root@Matrix-NVR ]# fdisk -l /dev/mmcblk0
Found valid GPT with protective MBR; using GPT
Disk /dev/mmcblk0: 7405568 sectors, 3616M
Logical sector size: 512
Disk identifier (GUID): 73987b6b-4974-4c94-a3e8-58ab2eb7a946
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 7405534
Number Start (sector) End (sector) Size Name
1 16384 24575 4096K uboot
2 24576 26623 1024K uboot-env
3 26624 38911 6144K resource
4 38912 104447 32.0M boot
5 104448 7405504 3564M rootfs
Partition Table
0x00002000@0x00004000(uboot)
0x00000800@0x00006000(uboot-env)
0x00003000@0x00006800(resource)
0x00010000@0x00009800(boot)
0x00200000@0x00019800(rootfs:grow)
Parameters
CONFIG_ENV_SIZE=0x2000
CONFIG_ENV_OFFSET=0xC00000
Debug
# Read in u-boot
mmc list
mmc read 0x800000 <offset> <size> # offset = 0xC00000 / 512
md.b 0x800000 <size>
mmc read 0x800000 0x6000 0x2000
md.b 0x800000 0x2000
# User space
dd if=/dev/mmcblk0 of=env.bin bs=512 skip=24576 count=8192
hexdump -C env.bin -n 500
Solution
The partition was not created of uboot-env
with proper offset and size. And the /etc/fw_env.config
was pointing to /dev/mmcblk0p2
which is uboot
partition.
By changing partition to main block device (mmcblk0
) and passing correct offset and size in fw_env.config
file, the fw_printenv
will read from correct location.
/dev/mmcblk0 0x3f8000 0x8000