The main purpose of the U-Boot boot loader is to locate the image for the operating system (usually Linux), load it into RAM and run it. To help accomplish this, U-Boot includes a scripting language that is embedded in the environment variables it reads at startup. The scripting language allows U-Boot to perform many other tasks besides loading the kernel. These tasks may include: deciding which memory device to load the kernel from, writing new images to memory devices for setup and error recovery, detecting when reset buttons are pressed and performing appropriate actions, setting up peripherals before the Linux kernel starts, determining appropriate command line options for booting the kernel, and many more.
After U-Boot is finished with its own startup and configuration, it begins running commands that are contained in environment variables. The first commands that U-Boot runs are in the ‘bootcmd’ variable, so this variable must exist or U-Boot can’t proceed. Different commands in an environment variable are separated by a semicolon. One environment variable can call another, similar to function calls in other scripting languages. U-Boot also supports keywords such as ‘if’ and ‘for’ to support decision making and loops. All of this makes the U-Boot environment variable system a fairly complete scripting language. For a normal boot sequence, the script should end by calling a command that will begin running the Linux kernel. The following is a minimal example of a U-Boot script:
# Address in RAM where the Linux device tree will be loaded. fdtaddr=0x88000000
# Address in RAM where the Linux kernel image will be loaded. kerneladdr=0x82000000
# Command to reset the board (TODO). boardreset=echo “Resetting all images to factory defaults”
# Arguments for the Linux command line, this will be read by the bootz command. bootargs=console=ttyS0,115200n8 root=/dev/mmcblk0p1 rootfstype=ext4 rootwait
# Command to load the Linux device tree from MMC loadfdt=load mmc 0:1 ${fdtaddr} /boot/dev.dtb
# Command to load the Linux kernel image from MMC loadlinux=load mmc 0:1 ${kerneladdr} /boot/uimage
# Command to boot Linux. Load the required images into RAM, then begin # running the kernel. This is the last command that U-Boot will run. bootlinux=run loadlinux; run loadfdt; bootz ${kerneladdr} – ${fdtaddr}
# Read a GPIO to see if the reset button is pressed for 2 seconds. Return # true if it is. The ‘exit’ command stops the current command, execution will # go back to the caller. resetrequested=if gpio i 27; then sleep 2; if gpio i 27; then true; exit; fi; false
# Entry point, this is the first command U-Boot will run on boot. # Do a board reset if needed, then boot Linux. In this command, bootlinux will # always run. bootcmd=if run resetrequested; then run boardreset; fi; run bootlinux |