From f34d8ae5ef93295e210a3131ed428ad18e2a822e Mon Sep 17 00:00:00 2001 From: Keisuke Tsukuda Date: Mon, 19 Oct 2026 01:40:58 +0900 Subject: [PATCH] rpi-u-boot-scr: load the kernel from the bootflow device When bootflow finds boot.scr on a USB drive other than device 0, the script still loads the kernel from BOOT_MEDIA 0:1 and can pick the wrong drive. Use devtype, devnum and distro_bootpart set by bootflow, falling back to BOOT_MEDIA 0:1 when they are not set, and update the BOOT_MEDIA documentation to match. Only create uboot.env when booting from mmc 0:1, where U-Boot keeps it, so a USB boot does not write to an inserted SD card. Delete the per-boot bootflow variables before saving. Tested on a Raspberry Pi 4: SD boot, USB boot, and kernel loading from the second of two USB drives. Signed-off-by: Keisuke Tsukuda --- docs/extra-build-config.md | 2 +- recipes-bsp/rpi-u-boot-scr/files/boot.cmd.in | 17 +++++++++++++++-- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/docs/extra-build-config.md b/docs/extra-build-config.md index 150d23643..9c206ca0d 100644 --- a/docs/extra-build-config.md +++ b/docs/extra-build-config.md @@ -82,7 +82,7 @@ By default SD card media is used as boot media. To switch the boot media from SD card to USB memory, the following variables are supported in local.conf: `CMDLINE_ROOT_PARTITION` and `BOOT_MEDIA`. The default value of `CMDLINE_ROOT_PARTITION` is "/dev/mmcblk0p2" to mount SD card. If you want to mount USB memory partition, set CMDLINE_ROOT_PARTITION to "/dev/sda2". -`BOOT_MEDIA` allows `mmc` and `usb`. The "mmc" is required to load an image from the SD card, following the u-boot specification. Similarly, if you want to load a boot image file from USB memory, set BOOT_MEDIA to "usb". +`BOOT_MEDIA` allows `mmc` and `usb`. When U-Boot bootflow runs the boot script, the kernel is loaded from the device and partition holding the script. `BOOT_MEDIA` (device 0, partition 1) is only used when `devtype`, `devnum` and `distro_bootpart` are not set, for example when sourcing the script manually. For example, if you want to use USB boot, please define the following parameters in your local.conf file. diff --git a/recipes-bsp/rpi-u-boot-scr/files/boot.cmd.in b/recipes-bsp/rpi-u-boot-scr/files/boot.cmd.in index 58fd86a76..8b157ab05 100644 --- a/recipes-bsp/rpi-u-boot-scr/files/boot.cmd.in +++ b/recipes-bsp/rpi-u-boot-scr/files/boot.cmd.in @@ -1,4 +1,17 @@ fdt addr ${fdt_addr} && fdt get value bootargs /chosen bootargs -fatload @@BOOT_MEDIA@@ 0:1 ${kernel_addr_r} @@KERNEL_IMAGETYPE@@ -if test ! -e @@BOOT_MEDIA@@ 0:1 uboot.env; then saveenv; fi; + +# Load the kernel from the bootflow device; fall back to BOOT_MEDIA 0:1. +if test -n "${devtype}" -a -n "${devnum}" -a -n "${distro_bootpart}"; then + fatload ${devtype} ${devnum}:${distro_bootpart} ${kernel_addr_r} @@KERNEL_IMAGETYPE@@ +else + fatload @@BOOT_MEDIA@@ 0:1 ${kernel_addr_r} @@KERNEL_IMAGETYPE@@ +fi + +# uboot.env lives on mmc 0:1. Only create it when booted from there (not +# on another image's SD card) and without the per-boot bootflow variables. +if test "${devtype}" = mmc -a "${devnum}" = 0 -a "${distro_bootpart}" = 1 -a ! -e mmc 0:1 uboot.env; then + env delete devtype devnum distro_bootpart prefix + saveenv +fi + @@KERNEL_BOOTCMD@@ ${kernel_addr_r} - ${fdt_addr}