</>EmbeddedInterview

What happens inside U-Boot from reset until it boots the kernel?

MediumEmbedded Linux & Boot

Short answer: U-Boot starts in architecture assembly (start.S), sets up the CPU and a temporary stack, runs board_init_f() (the pre-relocation init list: clocks, serial, DRAM), relocates itself to the top of DRAM, then runs board_init_r() (post-relocation init: MMC, flash, environment, network, console) and finally main_loop(), which either waits for commands or runs bootcmd after the bootdelay countdown.

In more detail

  1. start.S / _start (e.g. arch/arm/cpu/armv8/start.S): exception vectors, disable interrupts, set the CPU mode/exception level, invalidate caches and TLBs, call low-level lowlevel_init, set up a stack in SRAM or cache.
  2. board_init_f() in common/board_f.c: runs the init_sequence_f[] list: timer, serial (so you see output early), console, CPU and board info, DRAM init (dram_init()), and calculates where to relocate. The global data structure gd lives in a reserved area because there is no .data/.bss yet.
  3. Relocation (relocate_code): U-Boot copies itself to the top of DRAM, fixes up addresses, clears BSS, and continues there. Now the full RAM is available.
  4. board_init_r() in common/board_r.c: runs init_sequence_r[]: malloc area, driver model (initr_dm), MMC/NAND/SPI flash, environment (env_relocate loads bootcmd, bootargs etc. from storage), stdio and console, network, interrupts, and finally calls run_main_loop().
  5. main_loop() in common/main.c: prints the "Hit any key to stop autoboot" countdown. If interrupted, it gives the command prompt (=>); otherwise it runs bootcmd.

A common mix-up: board_init_f() runs before board_init_r(). The _f part runs from flash/SRAM with very little memory (originally "flash"), and the _r part runs after relocation to RAM.

What bootcmd typically does

load mmc 0:1 ${kernel_addr_r} Image
load mmc 0:1 ${fdt_addr_r} board.dtb
setenv bootargs console=ttyS0,115200 root=/dev/mmcblk0p2 rootwait
booti ${kernel_addr_r} - ${fdt_addr_r}

Modern U-Boot often uses distro boot / bootstd instead: it scans boot devices for an extlinux.conf or boot.scr and follows it.

SPL is a cut-down build of U-Boot (same source, CONFIG_SPL) that fits in SRAM; it runs board_init_f() style init, brings up DRAM, and loads U-Boot proper (or the kernel directly in Falcon mode).