Accessing U-Boot environment from userspace - Beaglebone Black

Hello,

I’m trying to enable accessing U-Boot environment from userspace, but I am facing a problem setting up the U-Boot environment on SD card. I’m using currently a beagelebone black, since my future design will be based on it. To generate my OS image, I am using Buildroot 2022.08 with U-Boot 2021.04 as a bootloader and Linux, 5.10.30-ti-r3 as a kernel (standard buildroot configurations for beaglebone_defconfig). To set up the environment I have decided to do it on an actual MBR/GPT partition, so I can avoid accidentally overwriting relevant data of other partitions. I have based all my work on the RAUC Documentation, Release 1.7.119-3da9c (6.7. Interfacing with the Bootloader)

  • To partitionate my SD card I have modified genimgae.cfg like that :

image

  • The partitions table goten from fdsik -l when I have booted first time the board before configuring anything:

image

  • To calculate the offsets for the environments I used offset=hex(n sector * 512 bytes/sector)

  • Based on the previous screenshots, this is the uboot configuration to setup the environment on the SD card

image

  • This is the /etc/fw_env.config file I set up and added to my root file system.

image

I have added the libubootenv package in buildroot to ensure using fw_setenv / fw_printenv commands, once everything is set and I have booted the board this is the output of the fw_printenv / fw_setenv commands:

image

I have been struggling a long time without any solution could you please help me with this problem

Thank you in advance

Have you got a serial console ?

Does u_boot give any warning/errors when booting?
Can you edit/save environment variables from u_boot?

It has been some time since I last built u_boot, but at that time there were quite a few options for u_boot with regard to if and where to save environment variables.
I suggest you check the u_boot build config to see what is enabled.

Thank you so much for your response.

First of all, I have corrected the fw_env.config file like this since I have forgotten to comment the # MTD device name section and I have verifed enviroment offsets in the # Block device example.

Block device example

/dev/mmcblk0 0x41000200 0x20000
/dev/mmcblk0 0x41020200 0x20000

Even though, during booting the board! U-boot shows “Loading Environment from MMC… *** Warning - bad CRC, using default environment”

I can each time edit/save environment variables from u_boot with savenenv/setenv/printenv

I have checked the u-boot build configuration but it seem everything okay, could you please mention any specific config so I can verify it !

I would be concerned that on every boot you are getting a CRC warning.

If you create an environment variable, save it and reboot, is it still there when you reboot ?

Have you tried the u-boot mailing list ?

I apologize for my delayed response .

I haven’t tried the u-boot mailing list yet! But I have an update to this issue!

I have configured the /etc/fw_env.config like this:

Block device example

/dev/mmcblk0p4 0x00000 0x20000
/dev/mmcblk0p4 0x20000 0x20000

As well as u boot environment configuration through the menuconfig:

Capture d’écran de 2022-09-15 15-11-32

I have partitioned my SD card like through genimage.cfg like this :

image boot.vfat {
vfat {
files = {
“MLO”,
“u-boot.img”,
“zImage”,
“uEnv.txt”,
“am335x-evm.dtb”,
“am335x-evmsk.dtb”,
“am335x-bone.dtb”,
“am335x-boneblack.dtb”,
“am335x-bonegreen.dtb”,
“am335x-boneblack-wireless.dtb”,
}
}

size = 16M

}

image sdcard.img {
hdimage {
}

partition u-boot {
	partition-type = 0xC
	bootable = "true"
	image = "boot.vfat"
}

partition rootfs_A {
	partition-type = 0x83
	image = "rootfs.ext4"
	size = 512M
}

partition rootfs_B {
	partition-type = 0x83
	size = 512M
}

partition env_envredun {
	partition-type = 0x83
	size = 60M
}

}

The boot log shows that till now uboot uses the default environment which I don’t know where exactly is stored.

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

I run the saveenv command twice, I click then boot and now I am able to use the MMC Environment stored in partition 4 and I am able now modifying the u boot environment from user space (fw_setenv/fw_printenv)!I got this:

=> saveenv
Saving Environment to MMC… Writing to redundant MMC(0)… OK
=> saveenv
Saving Environment to MMC… Writing to MMC(0)… OK
=> boot

What I have understood is that we have to specify the partition where we will save the uboot environment + redundant environment in the /etc/fw_env. config, as well as writing Device offset and Env. size starting from the beginning of that partition (mmcblk0p4) (0x00000) in /etc/fw_env. config. In the U-boot environment configuration menu, we should add the Device offset and Env. size starting from the beginning of that partition on the device (mmcblk0), and the most important thing is to save first the env and redundant env from uboot and then reboot!

Should I stick with this understanding or I have to verify more? Is there any way to automate the process so I become directly able to access uboot env from user space?

It is good that you now have it working. I didn’t know about /etc/fw_env. config.

The last time I installed the fw_setenv/fw_printenv commands they just worked, but we are talking several years ago. Maybe I did have to configure it, but just forgot.

If you are writing SD cards, then once you have a default environment set, assuming you are using Linux, you can use dd to copy the environment from the partition of your working Image and then dd this to the environment partition when you make a new image. You should be able to hook into a post build script in UBoot to do this.

The only thing I would be cautious of is the SD card erase block size. This will vary depending on SD cards. It can be anything from 128K and up. In your case, if the SD card uses 256K erase block sizes, then writing to the backup environment, might also erase/write the main environment (depending on alignment). If you get a power failure/glitch during this you might corrupt both copies if you are unlucky.

As the partition you are using is 60M, I would push the backup environment up to 0x400000 (4M) just to be safe. Googling it appears 4M block sizes can be quite common.

To check,you can do

cat /sys/block/'sdcard'/device/preferred_erase_size

It might vary between manufacturers even if the sd-cards are the same size.

Thank you so much for all those clarifications ! I will check the cat /sys/block/‘sdcard’/device/preferred_erase_size command ! Thank you again for your time