Skip to contentSkip to conversation

Searching and Finding boot.scr and uboot.env on a Raspberry Pi 4

DISCUSSION Marc Haber 2 participants 5 messages Jun 9, 2025 – Jun 12, 2025

lore
Share & tools
lore

Marc Haber

author
#0 lore raw

Hi,

for flexibility reasons I want to use u-boot to boot Debian on a Raspberry Pi 4. I want to be able to fix a broken boot from remote over the serial console. I have therefore written a boot.scr script that looks for kernel and initrd on various partitions on various media in various directories, loads kernel, initrd and devicetree and boots.

I am using the uboot binary that comes with Debian's u-boot-rpi:arm64 2025.01-3 package. That one is configured like that:

CONFIG_ENV_IS_IN_FAT=y
CONFIG_ENV_FAT_INTERFACE="mmc"
CONFIG_ENV_FAT_DEVICE_AND_PART="0:1"
CONFIG_ENV_FAT_FILE="uboot.env"

This works as long as I am booting from an MMC card: The Raspi Firmware reads config.txt (from MMC), loads uboot.bin (from MMC), which in turn reads uboot.env and boot.scr (from MMC), the script runs and the system comes up. While this happens, u-boot talks to me on the serial console, presumably because uboot.env contains variables like baudrate=115200
stderr=serial,vidconsole
stdin=serial,usbkbd
stdout=serial,vidconsole

However, when I have everything on USB, things don't work as smoothly. The system does not come up and it doesn't talk to me on the serial console. What I guess is that the Raspi Firmware reads config.txt (from USB), loads uboot.bin (from USB) which then looks on the non-existent MMC device for uboot.env and boot.scr. Game Over.

Is it somehow possible to have u-boot look for uboot.env and/or boot.scr on the same device it was booted from? Can I compile in some kind of search order so that u-boot can find uboot.env and boot.scr on other devices than the first partition on mmc? Or do I really need to build my own u-boot.bin?

If there is another method to ask user questions about u-boot, please let me know.

Greetings
Marc

Signature · 4 lines
-----------------------------------------------------------------------------
Marc Haber         | "I don't trust Computers. They | Mailadresse im Header
Leimen, Germany    |  lose things."    Winona Ryder | Fon: *49 6224 1600402
Nordisch by Nature |  How to make an American Quilt | Fax: *49 6224 1600421
#1 lore raw

Hi Marc,

Quoted 34 lines · Marc Haber

[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

Hi,

for flexibility reasons I want to use u-boot to boot Debian on a Raspberry Pi 4. I want to be able to fix a broken boot from remote over the serial console. I have therefore written a boot.scr script that looks for kernel and initrd on various partitions on various media in various directories, loads kernel, initrd and devicetree and boots.

I am using the uboot binary that comes with Debian's u-boot-rpi:arm64 2025.01-3 package. That one is configured like that:

CONFIG_ENV_IS_IN_FAT=y
CONFIG_ENV_FAT_INTERFACE="mmc"
CONFIG_ENV_FAT_DEVICE_AND_PART="0:1"
CONFIG_ENV_FAT_FILE="uboot.env"

This works as long as I am booting from an MMC card: The Raspi Firmware reads config.txt (from MMC), loads uboot.bin (from MMC), which in turn reads uboot.env and boot.scr (from MMC), the script runs and the system comes up. While this happens, u-boot talks to me on the serial console, presumably because uboot.env contains variables like baudrate=115200
stderr=serial,vidconsole
stdin=serial,usbkbd
stdout=serial,vidconsole

However, when I have everything on USB, things don't work as smoothly. The system does not come up and it doesn't talk to me on the serial console. What I guess is that the Raspi Firmware reads config.txt (from USB), loads uboot.bin (from USB) which then looks on the non-existent MMC device for uboot.env and boot.scr. Game Over.

Quoted 6 lines

Is it somehow possible to have u-boot look for uboot.env and/or boot.scr on the same device it was booted from? Can I compile in some kind of search order so that u-boot can find uboot.env and boot.scr on other devices than the first partition on mmc? Or do I really need to build my own u-boot.bin?

On Theobroma boards (Rockchip-based), we force the environment to be coming from the same storage medium as U-Boot proper, c.f. board/theobroma-systems/common/common.c:arch_env_get_location()

This is the location of the U-Boot environment in raw, not in a filesystem, so this won't apply to your use-case.

But quickly looking at env/fat.c which makes use of the CONFIG_ENV_FAT* variables from above, it seems like you should be able to override env_fat_get_intf and env_fat_get_dev_part from a board file and implement what you want there, provided U-Boot (and/or the RPi firmware) provides you with the information from where it was booted.

If you don't want to change the U-Boot binary and somehow you can have a minimal environment to run a few commands, you could probably erase the whole environment and re-import it manually with env import?

Cheers,
Quentin

author
#2 lore raw

Hi Quentin,

thanks for answering. I really appreciate that.

Quoted 3 lines · Quentin Schulz

On Theobroma boards (Rockchip-based), we force the environment to be coming from the same storage medium as U-Boot proper, c.f. board/theobroma-systems/common/common.c:arch_env_get_location()

This is the location of the U-Boot environment in raw, not in a filesystem, so this won't apply to your use-case.

Sadly ;-)

Quoted 1 line

But quickly looking at env/fat.c which makes use of the CONFIG_ENV_FAT* variables from above, it seems like you should be able to override env_fat_get_intf and env_fat_get_dev_part from a board file and implement what you want there, provided U-Boot (and/or the RPi firmware) provides you with the information from where it was booted.

Can I get u-boot to somehow log where it is looking for files? That way I wouldn't have to guess where to put a board file.

Quoted 1 line

If you don't want to change the U-Boot binary and somehow you can have a minimal environment to run a few commands, you could probably erase the whole environment and re-import it manually with env import?

As far as I have understood, if u-boot finds neither its environment file or a boot.scr (that one comes from the environment as well, right?) it just sits there doing nothing. It's that first step that I don't understand.

If a change can be made while keeping backwards compatibility, I figure I can talk to the u-boot maintainer of Debian, or rebuild the u-boot-raspi package myself as a last resort. I just don't have a clue (yet) what and where to touch. I am still on the lower end of my u-boot learning curve.

Greetings
Marc

Signature · 4 lines
-----------------------------------------------------------------------------
Marc Haber         | "I don't trust Computers. They | Mailadresse im Header
Leimen, Germany    |  lose things."    Winona Ryder | Fon: *49 6224 1600402
Nordisch by Nature |  How to make an American Quilt | Fax: *49 6224 1600421
#3 lore raw

Hi Marc,

Quoted 7 lines · Marc Haber

[You don't often get email from [email protected]. Learn why this is important at https://aka.ms/LearnAboutSenderIdentification ]

Hi Quentin,

thanks for answering. I really appreciate that.

On Tue, Jun 10, 2025 at 12:36:23PM +0200, Quentin Schulz wrote:

Quoted 9 lines
Quoted 6 lines

On Theobroma boards (Rockchip-based), we force the environment to be coming from the same storage medium as U-Boot proper, c.f. board/theobroma-systems/common/common.c:arch_env_get_location()

This is the location of the U-Boot environment in raw, not in a filesystem, so this won't apply to your use-case.

Sadly ;-)

But the logic to detect which boot medium was used is there, hence why I mentioned it :)

Quoted 10 lines
Quoted 6 lines

But quickly looking at env/fat.c which makes use of the CONFIG_ENV_FAT* variables from above, it seems like you should be able to override env_fat_get_intf and env_fat_get_dev_part from a board file and implement what you want there, provided U-Boot (and/or the RPi firmware) provides you with the information from where it was booted.

Can I get u-boot to somehow log where it is looking for files? That way I wouldn't have to guess where to put a board file.

By "board file" i meant a C file in board/ (for RPi, typically board/raspberrypi/rpi/ I woudl assume).

Quoted 9 lines
Quoted 3 lines

If you don't want to change the U-Boot binary and somehow you can have a minimal environment to run a few commands, you could probably erase the whole environment and re-import it manually with env import?

As far as I have understood, if u-boot finds neither its environment file or a boot.scr (that one comes from the environment as well, right?) it just sits there doing nothing. It's that first step that I don't understand.

Which first step are you talking about?

Cheers,
Quentin

author
#4 lore raw

Hi Quentin, hi List,

tl;dr my issue is solved.

Quoted 6 lines · Quentin Schulz
Quoted 5 lines · Marc Haber
Quoted 4 lines · Quentin Schulz

On Theobroma boards (Rockchip-based), we force the environment to be coming from the same storage medium as U-Boot proper, c.f. board/theobroma-systems/common/common.c:arch_env_get_location()

Quoted 7 lines
Quoted 5 lines
Quoted 2 lines

This is the location of the U-Boot environment in raw, not in a filesystem, so this won't apply to your use-case.

Sadly ;-)

But the logic to detect which boot medium was used is there, hence why I mentioned it :)

I looks like Debian's u-boot for Raspberry Pi actually finds boot.scr laying at the side of u-boot.bin, and the initial environment the script gets contains $devtype, $devnum, and $distro_bootpart which can be used to continue finding things. $disto_bootpart is a bit confusingly named, but since it's the only variable that always seems to contain the correct number according to the tests I did, this is about enough.

I had to explicitly code out reading of uboot.env though (that for some reason doesn't happen automatically), and while at it I added some code to also read uboot.env.asc which easier to generate and to edit.

I apologize for not finding this myself any earlier, I was led into an incorrect assumption about u-boot since the Raspberry Pi 4 is VERY picky regarding choice of SD cards with the Firmware from April 2024 that comes with current Debian. The Firmware that comes with current Raspberry Pi OS is from March 2025 and is actually much less picky regarding SD cards. I sorted out three SD cards and was about to give up on u-boot when I discovered the firmware issue in the debug output of the second stage bootloader (which needs to be explicitly enabled by uart_2ndstage=1 in config.txt).

Writing "kernel=uboot.bin" in my config.txt while the file actually was named u-boot.bin cost me an additional couple of hours. I then went to bed and spotted the missing hyphen first thing in the morning. Recommendation: Letting things rest for a while helps a lot!

Quoted 7 lines
Quoted 5 lines

As far as I have understood, if u-boot finds neither its environment file or a boot.scr (that one comes from the environment as well, right?) it just sits there doing nothing. It's that first step that I don't understand.

Which first step are you talking about?

I was taling about finding the boot.scr script which now magically seems to work. I do not even need an SD card any more, everything can happen from the USB SSD. I reached my goal, can do everything with what's in Debian and thank you for your help!

Thank you very much, again, for providing u-boot, and for listening and giving answers that were able to point me in the right direction!

Cheers
Marc

Signature · 4 lines
-----------------------------------------------------------------------------
Marc Haber         | "I don't trust Computers. They | Mailadresse im Header
Leimen, Germany    |  lose things."    Winona Ryder | Fon: *49 6224 1600402
Nordisch by Nature |  How to make an American Quilt | Fax: *49 6224 1600421