U-Boot Basics: Environment Variables and Booting a Kernel
Learn how U-Boot stores environment variables, what bootcmd and bootargs do, and how to load a Linux kernel and device tree and boot it from the U-Boot prompt.
Contents
U-Boot is a bootloader that runs before Linux on many embedded boards. Its behavior is controlled by U-Boot environment variables, and booting a kernel comes down to loading files into RAM and running one boot command. This article shows how to inspect and change the environment and how to boot a kernel by hand.
All commands below are typed at the U-Boot prompt, which you reach over a serial console (see Serial Console on Embedded Linux). They need real U-Boot, so none of them are run by the site’s test harness.
How the environment works
U-Boot keeps the environment in an in-memory copy. Changes are lost on reset unless you write them to persistent storage, such as flash. If the area holding the saved environment is erased, U-Boot falls back to the default environment built into the binary.
The basic commands, with their older aliases in parentheses:
env print # (printenv) show variables
env print -a # show all variables
env set autoload n # (setenv) set a variable
env set autoload # with no value, the variable is deleted
env save # (saveenv) write to persistent storage
env default -a # reset the whole environment to its defaultsThe env command has more subcommands, including export and import to copy the environment to and from memory as text, and run to execute the commands stored in a variable.
U-Boot variables that matter for booting
bootcmd: the command U-Boot runs automatically if you do not interrupt the boot delay.bootargs: the arguments passed to the operating system, which for Linux is the kernel command line.bootdelay: the wait beforebootcmdruns.0boots immediately but a key press can still stop it,-1disables autoboot, and-2boots immediately without checking for a key press.loadaddrandbootfile: the defaults theloadcommand uses for the address and file name.ipaddrandserverip: network settings used by commands such as TFTP.
The default environment is defined at build time, preferably in a text .env file in the board directory, where each line has the form var=value. Older boards define it in C headers instead.
Scripts in variables
A variable can hold several commands separated by semicolons, and env run executes it. Longer values can use if/then/fi, and other variables are referenced as ${name}. This is how most boards chain their boot steps:
env set loadkernel 'load mmc 0:1 ${kernel_addr_r} Image'
env set bootcmd 'run loadkernel'
env run bootcmdThe variable names kernel_addr_r, fdt_addr_r and ramdisk_addr_r are used in the U-Boot documentation examples, but whether your board defines them depends on its configuration. Check with env print.
Load files into RAM
The generic load command reads a file from a filesystem:
load <interface> [<dev[:part]> [<addr> [<filename> [bytes [pos]]]]]The interface is a block device type such as mmc, sata, scsi or usb. The address, partition, size and offset arguments are hexadecimal. If you omit the address, U-Boot uses loadaddr, and if you omit the file name it uses bootfile. After a successful load, filesize holds the number of bytes read.
load mmc 0:1 ${kernel_addr_r} Image
load mmc 0:1 ${fdt_addr_r} board.dtbThe file names here are placeholders. Use the names that exist on your boot partition. The device tree is the compiled .dtb described in Device Tree Basics.
Boot the kernel
For a Linux kernel in the flat or compressed Image format, use booti:
booti [<addr> [<initrd>[:<size>]] [<fdt>]]Pass - instead of the initrd address to boot with a device tree but no RAM disk. With a RAM disk loaded right after the kernel and device tree, the call from the U-Boot docs looks like this:
booti ${kernel_addr_r} ${ramdisk_addr_r}:${filesize} ${fdt_addr_r}For a small root filesystem to load here, Build an Initramfs by Hand and Boot It in QEMU shows one way to make one.
Without a RAM disk, it becomes:
env set bootargs 'console=ttyS0,115200'
booti ${kernel_addr_r} - ${fdt_addr_r}The bootargs value above is only an illustration. The correct console device and any root= setting depend on your board and root filesystem. If the kernel is compressed, booti can decompress it when the matching compression option is built into U-Boot and kernel_comp_addr_r and kernel_comp_size are set.
booti normally does not return. On error it sets $? to 1.
Other boot commands
bootm is documented for FIT and legacy images. U-Boot recommends the Flat Image Tree (FIT) format over the legacy image format, which is limited and insecure. A FIT boot names the image and configuration, for example bootm ${loadaddr}#<conf>. With no arguments, bootm boots the image at the current image address.
Make the boot automatic
Once the manual sequence works, put it in bootcmd and save it:
env set bootcmd 'load mmc 0:1 ${kernel_addr_r} Image; load mmc 0:1 ${fdt_addr_r} board.dtb; booti ${kernel_addr_r} - ${fdt_addr_r}'
env saveOn the next reset, U-Boot waits for bootdelay and then runs bootcmd. If you want to keep a way back, run env print bootcmd first and note the original value. env default can also restore the defaults.
Once the board is running Linux, Reduce Embedded Linux Boot Time shows how to measure where the remaining time goes.
Sources
This article was drafted with AI assistance, fact-checked against the sources listed above and reviewed by a human editor before publication. Spotted an error? See our corrections policy.