Module 4 · Lesson 48 of 156

U-Boot SPL and building the bootloaders

Explain SPL's role and SRAM size limit, build TF-A and U-Boot with stm32mp15_trusted_defconfig, and flash an SD card that stops at the U-Boot prompt.

  • Beginner to advanced
  • 10 min read
  • 60 min lab
What you will need

Ubuntu 24.04 in a VM (WSL2 needs usbipd for SD card access); gcc-arm-none-eabi, device-tree-compiler, libssl-dev; an STM32MP157C-DK2 board, a microSD card and reader; serial console at 115200 baud

Your place in the module
By the end you can 4 objectives
  • Explain why SPL must fit in on-chip SRAM and what it does before DDR exists
  • Build U-Boot with stm32mp15_trusted_defconfig and TF-A with U-Boot as BL33
  • Identify the build artifacts tf-a-stm32mp157c-dk2.stm32 and fip.bin and say what each contains
  • Create the GPT with sgdisk, write both images into the fsbl1, fsbl2 and fip partitions with dd, and stop the board at the U-Boot prompt
Video coming soonU-Boot SPL and building the bootloaders
The video for this lesson is being recorded. Read the lesson below in the meantime.
Recall first from TF-A as the secure boot stage
  • TF-A BL2 is the FSBL: it runs from on-chip SYSRAM, initializes DDR, and loads BL32 and BL33 (U-Boot) from a FIP before handing control over
  • The secure monitor in BL32 (OP-TEE or SP_min on the STM32MP1; BL31 on AArch64 platforms) stays resident for the life of the system and answers PSCI calls such as CPU_ON and SYSTEM_RESET made with the SMC instruction
  • BL2 finds each image in the FIP by UUID in the table of contents, using its own board device tree that is compiled into the BL2 binary
01

Why U-Boot needs a small first stage

4 min read

After TF-A comes U-Boot, but a full U-Boot often cannot load itself from the tiny SRAM. This lesson answers SPL's role and size limit, how to build with stm32mp15_trusted_defconfig, and how to flash the result to an SD card.

At reset, the DDR controller on an SoC is unconfigured. The external RAM chips are physically present but unusable: the controller needs its timing parameters set, and DDR needs a calibration sequence before any read or write can be trusted. Until that happens, the only writable memory is the on the chip. On the STM32MP157 that is the 256 KB SYSRAM you met in the ROM lesson. Everything that runs before DDR init must fit its code, stack and data inside that small on-chip memory.

A full U-Boot does not fit that budget, and it does not try to. It carries a command shell, filesystem drivers, a network stack, an environment subsystem and dozens of commands, and it relocates itself to the top of DDR once RAM is available. The is the answer. It is built from the same U-Boot source tree, but Kconfig selects only what the early job needs: clock and DDR setup, one boot-medium driver (SD, eMMC, NAND or SPI flash), and a loader for the next image. No shell, no environment editing, no networking. Its size is not a style choice: CONFIG_SPL_MAX_SIZE and the linker script bound it to the SRAM region, and if the code grows past that region the build fails at link time rather than producing an image that would crash on the board.

Here is the STM32MP1 twist that connects this lesson to the previous one. In the trusted boot chain, TF-A BL2 already does the SPL job: it runs in SYSRAM, initializes DDR and loads the next stage. So U-Boot for this chain is built without SPL at all. The defconfig stm32mp15_trusted_defconfig sets CONFIG_TFABOOT=y, which tells U-Boot it is loaded by TF-A as BL33 and can assume DDR already works. U-Boot also ships stm32mp15_basic_defconfig, which takes the other route: no TF-A, and U-Boot's own SPL acts as the first stage, doing DDR init and loading U-Boot proper. Same chip, two chains, two defconfigs. This course uses the trusted chain, because it keeps the secure-world services from the TF-A lesson available to Linux later.

Going deeperSPL, TPL and full U-Boot: what each stage can afford

Some SoCs split the early work even further with a . Despite the name, TPL runs first: on chips such as the Rockchip RK3399 the boot ROM can only load a very small image, so TPL does the DDR init in the tiniest SRAM, then loads SPL, which loads U-Boot proper. The pattern is always the same: each stage lives within the memory the previous stage made available. TPL can afford a few kilobytes of code and almost no stack; SPL can afford tens of kilobytes, a small malloc pool and a handful of drivers; full U-Boot, running in DDR, can afford hundreds of kilobytes, large buffers, a full driver model and the interactive shell. When you read a U-Boot port, checking which of these stages exist tells you immediately how constrained the chip's early boot memory is.

TPL, SPL and full U-Boot: memory budget and jobTPLSPLFull U-BootRuns intiny SRAMon-chip SRAMDDRSize budgeta few KBtens of KBhundreds ofKBTypical jobearliestinit, DDRDDR init,load nextload thekernelShellnonoyes
TPL, SPL and full U-Boot compared by memory budget and job
02

Building U-Boot and TF-A for the board

4 min read

In the real test the U-Boot build first stopped with error: command 'swig' failed: No such file or directory (and later Python and GnuTLS headers are needed too), which is why the list above is longer than just the compiler. Both projects are cross-built on your Ubuntu 24.04 host with the bare-metal Arm compiler, since the STM32MP157's Cortex-A7 cores run 32-bit Arm code here. Install the tools and fetch both source trees at known stable tags:

bash
sudo apt install git make gcc-arm-none-eabi device-tree-compiler libssl-dev swig python3-dev \
    python3-setuptools bison flex libgnutls28-dev uuid-dev
git clone --depth 1 --branch v2024.04 https://source.denx.de/u-boot/u-boot.git
git clone --depth 1 --branch v2.10 https://github.com/ARM-software/arm-trusted-firmware.git

Build U-Boot first. A selects the board family and the boot chain; the DEVICE_TREE variable selects the exact board's device tree inside that family:

bash
cd u-boot
export CROSS_COMPILE=arm-none-eabi-
make stm32mp15_trusted_defconfig
make DEVICE_TREE=stm32mp157c-dk2 all

The two outputs that matter are u-boot-nodtb.bin (the raw U-Boot binary without a device tree appended) and u-boot.dtb (the compiled board device tree). They are kept separate because TF-A wants to place them into its own container, which you build next. In the real run (U-Boot v2024.04, arm-none-eabi-gcc from Ubuntu 24.04) make DEVICE_TREE=stm32mp157c-dk2 all finished and left u-boot-nodtb.bin (1036544 bytes), u-boot.dtb (100224 bytes), u-boot.bin (1136768 bytes) and u-boot.stm32 (1137312 bytes). The .stm32 file is U-Boot with the STM32 image header; U-Boot's documentation says it is what the trusted defconfig produces for a TF-A boot without a FIP, while this lesson packs the nodtb binary and the dtb into a FIP. From the trusted-firmware-a directory:

bash
export CROSS_COMPILE=arm-none-eabi-
make PLAT=stm32mp1 ARCH=aarch32 ARM_ARCH_MAJOR=7 AARCH32_SP=sp_min \
  DTB_FILE_NAME=stm32mp157c-dk2.dtb \
  STM32MP_SDMMC=1 \
  BL33=../u-boot/u-boot-nodtb.bin \
  BL33_CFG=../u-boot/u-boot.dtb \
  all fip

This exact command finished with exit status 0 in the real test (TF-A v2.10). Note that TF-A's own documentation calls OP-TEE the recommended secure monitor and SP_min "not recommended" with "very limited support"; this lab uses sp_min only because it needs no extra project to build. Read the variables as a description of the chain from the last lesson: PLAT selects the STM32MP1 platform code, AARCH32_SP=sp_min selects the minimal secure payload that provides the secure-world services, and BL33 names the normal-world image TF-A will load after its own stages. BL33 is U-Boot. The fip target builds a . TF-A BL2 later reads this container from the boot medium, extracts U-Boot into DDR and jumps to it.

Step by stepFrom source to flashable files
  1. U-Boot: stm32mp15_trusted_defconfig plus DEVICE_TREE=stm32mp157c-dk2 produces u-boot-nodtb.bin and u-boot.dtb.

  2. TF-A: the make line above compiles BL2 and the secure payload, and packs your U-Boot into the FIP.

  3. Collect: build/stm32mp1/release/tf-a-stm32mp157c-dk2.stm32 is the first-stage image (209261 bytes in the real run); build/stm32mp1/release/fip.bin is the container with U-Boot inside (1187441 bytes). make -C tools/fiptool builds the fiptool, and tools/fiptool/fiptool info build/stm32mp1/release/fip.bin printed: Secure Payload BL32 (Trusted OS): offset=0x100, size=0x8B00, Non-Trusted Firmware BL33: offset=0x8C00, size=0xFD100, FW_CONFIG: offset=0x105D00, size=0x226, HW_CONFIG: offset=0x105F26, size=0x18780 and TOS_FW_CONFIG: offset=0x11E6A6, size=0x37CB. There is no BL31 entry, as expected for this 32-bit SoC.

Why the .stm32 suffix matters

The STM32MP1 ROM does not accept a plain binary: it requires an STM32 header describing the image, and it verifies that header before loading. The TF-A build appends this header automatically, which is why you flash tf-a-stm32mp157c-dk2.stm32 and not the raw ELF or .bin file. If you ever write the wrong file to the card's first sectors, the ROM rejects it and falls back to its next boot source.

From U-Boot sources to the two flashable filesU-Bootdefconfigstm32mp15_trusted_defconfigBuildU-Bootu-boot-nodtb.bin,u-boot.dtbBuild TF-A+ FIPBL33 isU-BootWrite SDcarddd at rawoffsetsU-Bootdefconfigstm32mp15_trusted_defconfigBuildU-Bootu-boot-nodtb.bin,u-boot.dtbBuild TF-A+ FIPBL33 isU-BootWrite SDcarddd at rawoffsets
U-Boot's outputs feed the TF-A build, which produces the two flashable files
03

Flashing the SD card and stopping at the prompt

5 min read

Insert the microSD card into the reader and identify its device node with lsblk: check the size column and make sure you pick the card, not your system disk. In a VM, pass the USB reader through to the guest first; plain WSL2 cannot see USB storage without usbipd, so a VM is the simpler path for this lab.

sgdisk and dd destroy what they write

The commands below overwrite the partition table and raw sectors with no confirmation. Replace /dev/sdX with your card's actual node from lsblk, and double-check it: pointing them at your system disk destroys data. Unmount any auto-mounted partitions of the card first.

U-Boot's board documentation (doc/board/st/stm32mp1.rst, section "Prepare an SD card") gives the minimal layout: GPT partitioning, two partitions named fsbl1 and fsbl2 of at least 256 KiB, and one partition named fip, with a fourth root filesystem partition after them. The documented commands are:

bash
lsblk
sudo sgdisk -o /dev/sdX
sudo sgdisk --resize-table=128 -a 1 \
    -n 1:34:545    -c 1:fsbl1 \
    -n 2:546:1057  -c 2:fsbl2 \
    -n 3:1058:9249 -c 3:fip \
    -n 4:9250:     -c 4:rootfs -A 4:set:2 \
    -p /dev/sdX
sudo dd if=tf-a-stm32mp157c-dk2.stm32 of=/dev/sdX1
sudo dd if=tf-a-stm32mp157c-dk2.stm32 of=/dev/sdX2
sudo dd if=fip.bin of=/dev/sdX3
sync

(sgdisk comes from the gdisk package. For a card that Linux names /dev/mmcblk0, the partitions are /dev/mmcblk0p1, /dev/mmcblk0p2 and /dev/mmcblk0p3. Run the commands once the partitions appear; if the kernel still shows the old table, run sudo partprobe /dev/sdX first.) In a real run of the sgdisk part on a 64 MiB image file, the table printed fsbl1 (sectors 34 to 545, 256.0 KiB), fsbl2 (546 to 1057, 256.0 KiB), fip (1058 to 9249, 4.0 MiB) and rootfs (9250 to the end). The first partition starts at sector 34, byte offset 17408, because sectors 0 to 33 hold the protective MBR and the GPT; writing an image at byte 0 would overwrite them. The two fsbl partitions have the same content on purpose: the U-Boot documentation says they are there "to guarantee a fail-safe update of FSBL", and fsbl2 can be left out if you do not need that ROM feature. The tf-a file must be the .stm32 file, with its STM32 header, because the ROM checks that header. sync forces the writes out of the page cache onto the card before you pull it. The next lesson, "The flash layout and U-Boot basics", looks at the whole layout; for now you only need these partitions.

Put the card in the DK2, confirm the boot pins select the SD card as covered in the GPIO and boot pins lesson, connect the serial console at 115200 baud (the DK2's ST-LINK USB connector exposes it, typically as /dev/ttyACM0), and power on:

bash
picocom -b 115200 /dev/ttyACM0

You should see TF-A's NOTICE lines, then the U-Boot banner, then the autoboot countdown. The exact version string and build date will differ on your machine; the shape of the lines is what to look for:

text
NOTICE:  Model: STMicroelectronics STM32MP157C-DK2 Discovery Board
...
U-Boot 2024.04 (...)
Hit any key to stop autoboot:  0
STM32MP>

Press any key during the countdown and autoboot stops: you are at the STM32MP> prompt, with the full U-Boot running from the DDR that TF-A initialized. That is the lab's finish line: ROM validated your TF-A header, BL2 initialized DDR and unpacked your FIP, and BL33 (the U-Boot you built) is now waiting for commands. If nothing appears, work backwards through the chain: no NOTICE lines means the ROM never accepted the first stage (wrong file, a partition not named fsbl1 or fsbl2, wrong boot pins); NOTICE lines but no U-Boot banner means the FIP is missing or not in the partition named fip.

From ROM to the U-Boot promptROMTF-A BL2U-BootYouload FSBL intoSYSRAMload BL33 intoDDRautobootcountdownpress a keySTM32MP>prompt1. ROM → TF-A BL2load FSBL into SYSRAM2. TF-A BL2 → U-Bootload BL33 into DDR3. U-Boot → Youautoboot countdown4. You → U-Bootpress a key5. U-Boot → YouSTM32MP> prompt
From ROM to the STM32MP> prompt, and where your keypress lands

You can now build both bootloaders from source and get a board to the U-Boot prompt with images you understand end to end. The next lesson, "The flash layout and U-Boot basics", answers what else lives on that card beyond the three regions you just wrote, and what you can do at the prompt you just reached.

Key takeaways

  • SPL is a cut-down U-Boot that runs from on-chip SRAM, initializes DDR and loads the full U-Boot; its size is capped by the SRAM it runs in
  • On the STM32MP15 trusted chain TF-A BL2 does the SPL job, so stm32mp15_trusted_defconfig builds U-Boot proper only, as BL33
  • TF-A packs U-Boot (BL33) and its device tree into fip.bin; the ROM chain reads tf-a-*.stm32 first, then the FIP
  • The SD card needs a GPT with partitions named fsbl1, fsbl2 and fip: the ROM and TF-A find the images by name; the next lesson maps the full layout
Reflect
  1. Explain to a colleague, in two sentences: explain why SPL must fit in on-chip SRAM and what it does before DDR exists
  2. Which objective are you least sure about? Re-read that section, then do its exercise in the practice pack.
  3. What surprised you when you ran the commands yourself?
On a real board

Repeat the key commands on your Raspberry Pi or lab board over SSH or the serial console. Note anything that behaves differently from your PC, such as missing tools, a BusyBox shell or a read-only filesystem.

References and further reading

Find your next step

↑ ↓ to move · Enter to open · Esc to close