2.3 U-BOOT Scripting
A script is composed of a sequence of simple variables and recursive variables. Each recursive variable embeds a group of U-BOOT commands separated by semicolons (;). When a script is executed via the run command, these commands are evaluated sequentially.
U-BOOT supports two distinct command interpreters. The simple command line interface provides rudimentary command interpretation, executing U-BOOT commands sequentially with individual lines separated by semicolons (;) or newline characters. This legacy simple parser does not offer programmatic, conditional runtime control flows.
Conversely, the HUSH interface provides a Bourne-shell-compatible command interpretation layer, offering structured test statements, conditional branches, and loop control blocks. The HUSH parser dynamically determines the execution path of U-BOOT commands based on conditional evaluation results. It supports conditional constructs such as if...then...else...fi alongside loop mechanisms including for...do...done, while...do...done, for...in...do...done, and until...do...done. Furthermore, it natively implements the short-circuit logical operators && and || common to the Linux command line. Because advanced script control flow relies heavily on these features, complex U-BOOT scripts must execute within a HUSH-enabled shell environment.
2.3.1 U-BOOT Variables
U-BOOT variables are broadly divided into local environment variables and global variables. Local environment variables are utilized to store volatile, temporary configurations and are never written to non-volatile storage. The syntax for declaring a local variable follows this format:
var=value
There must be no whitespace surrounding the assignment operator (=), and the value must strictly be a single unspaced token. If the assigned string contains whitespace characters, the entire payload must be enclosed within single or double quotes. Enclosing a string in single quotes suppresses variable expansion, whereas double quotes permit inline variable expansion. The following example instantiates a local variable named LocalVar with the string payload "I am a local variable":
LocalVar="I am a local variable"
Global variables are declared using the standard syntax format:
setenv varName varValue
The three components are separated by whitespace. When the value payload must explicitly contain special characters such as $ or ;, those characters must be escaped with a leading backslash (\). A global variable value can encompass an entire command string separated by semicolons (;); the initial token of each statement is interpreted as a U-BOOT command, followed by its whitespace-delimited arguments. If a global variable holds multiple commands concatenated with semicolons (;), the entire command sequence must be wrapped in single quotes.
Global variables defined via setenv can be invoked as executable macros using the run command. The following example establishes a global variable named infoDisp1:
=> setenv infoDisp1 echo
This is to show how to use global variable.
The macro variable infoDisp1 can now be executed by passing it to the run command:
=> run infoDisp1
This is to show how to use global variable.
The following example shows how to utilize setenv to construct a complex global variable containing multiple sequential commands:
=> setenv infoDisp2 'echo Hello world,; echo I am a robot.'
=> run infoDisp2
Hello world,
I am a robot.
2.3.2 Test Commands
Test commands evaluate expressions, strings, or integers to determine the truth value of their logical relationships. When combined with conditional branches or loops, they enable dynamic command combinations. The standard conditional expressions are evaluated as follows:
-
( expression ): True if the inner expression evaluates to true.
-
! expression: True if the expression evaluates to false (logical inversion).
-
-n string: True if the length of the string is non-zero.
-
-z string: True if the length of the string is zero.
-
string: True if the length of the string is non-zero.
-
string1 = string2: True if both strings are identical.
-
string1 != string2: True if the strings are not identical.
For integer evaluation, relational comparisons are expressed using the flags -eq (equal), -lt (less than), -gt (greater than), -ge (greater than or equal), -le (less than or equal), and -ne (not equal). For instance, integer1 -eq integer2 evaluates whether integer1 equals integer2, while integer1 -lt integer2 checks if integer1 is less than integer2.
The evaluation construct test -n string or its structural alias [ -n string ] returns a truth value of 1 if the string length is non-zero, and 0 otherwise. Similarly, [ var1 -le var2 ] returns 1 if the integer value of var1 is less than or equal to var2, and 0 otherwise.
2.3.3 Conditional Statements
The standard syntax configuration for conditional branches follows this structure:
if (condition);
then
command list
elif (condition);
then
command list
else
command list
fi
In the following script block, conditional execution logic interacts with the test framework to select different system boot vectors based on the value of the net_boot variable. If the net_boot variable is unassigned, the system defaults to booting from the SD card. If net_boot equals 1, the platform switches to a network-based TFTP boot pathway. The itest command is leveraged here to explicitly handle integer comparison operations.
if test -n $net_boot;
then
if itest $net_boot == 1;
then
echo !!!!!!!!!!!!!!!!!!!!!!!!!!;
echo ! Booting from TFTP !;
echo !!!!!!!!!!!!!!!!!!!!!!!!!!;
tftp $fpgadata soc_system.rbf;
else
echo !!!!!!!!!!!!!!!!!!!!!!!!!!;
echo ! Booting from SD !;
echo !!!!!!!!!!!!!!!!!!!!!!!!!!;
fatload mmc 0:1 $fpgadata soc_system.rbf;
fi
else
echo !!!!!!!!!!!!!!!!!!!!!!!!!!;
echo Booting from SD !;
echo !!!!!!!!!!!!!!!!!!!!!!!!!!;
fatload mmc 0:1 $fpgadata soc_system.rbf;
fi
2.3.4 Loop Statements
There are three types of loop control statements implemented within the HUSH interface: for...in...do...done, while...do...done, and until...do...done. Distinct from the standard C-language iterative for loop, the loop variable in a HUSH interpreter's for loop can only iterate over values explicitly supplied in a tokenized literal list. The usage models and syntactical arrangements for these three loop variants are defined below:
for var in a b c d;
do
echo $var;
done
setenv var 1
while test var -eq 0;
do
echo $var;
setenv var 1;
done
setenv var begin;
until test var != finished;
do
echo I am doing something meaningless!;
echo Just to show how to use until loop.
setenv var finished;
done
2.3.5 U-BOOT Script Practical Example
The following block shows a structured, minimal U-BOOT deployment script configuration. Lines prefixed with the # symbol represent code comments.
# 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;fi
# 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