Das U-Boot

What is a boot loader

Boot loader significance in embedded systems is prominent. Embedded devices are based on the SOC( System on Chip) approach. SOC-based processors have a limited amount of inbuilt RAM and ROM due to the very high cost. This limited RAM and ROM can only contain limited instructions to boot and has limited functionality to support. For advanced microprocessors this becomes a limitation. To overcome this issue, processors externally connect to the external DRAM controller. A small footprint bootloader handles initial boot calls and a chain of boot loaders get called to run a fully functional operating system.

SOC manufacturers on the other hand did not know the DRAM controllers will be used with processors in the final design. Actually, there will be thousands of DRAM controllers available in the market. So to make things simple, SOC manufacturers take advantage of 1st stage small footprint boot loaders. Example MLO in BeagleBone (TI AM335X based). MLO is designed to have an extremely small footprint to adjust within SOC RAM. Furthermore, MLO talks to the DRAM controller and passes controls to a more powerful boot loader eg u-boot. 

So in simple terms, we can say, the boot loader is a bridge between the final OS and SOC. Without a boot loader it will be almost impossible for SOC to load OS directly. 

However, nowadays more expensive SOC’s are available in the market. Those SOCs also has inbuilt RAM with more capacity and can boot directly without the need of external RAM.

Minimal responsibilities of boot loader in embedded systems:

  1. Initializing hardware. Prominently memory controllers
  2. Providing boot parameters to pass on to OS
  3. Launching OS

MInimum features provided by embedded systems boot loader:

  1. Reading and writing memory locations
  2. Uploading new binary images to borders RAM
  3. Copying binary images from RAM into flash

What is U-Boot?

U-Boot is an open source, cross-platform boot loader that provides out-of-box support for pre developed embedded boards and CPUs, including PowerPC, ARM, XScale, MIPS, Coldfire, NIOS, Microblaze, and x86

You should visit https://www.denx.de/wiki/U-Boot to get more information regarding u-Boot. 

Features of U-Boot

Customizable footprint

The U-Boot is highly customizable to provide a rich feature set and a small binary footprint.

Monitor

The U-Boot “monitor” command helps you to customize the boot process. 

Variables

The U-Boot provides environment variables. The U-Boot also provides the functionality to read, write or save those variables in the flash memory. These variables play an extremely important role in dynamically changing and sending valuable information to the bootloader and kernel without the need to recompile the bootloader and kernel source. The U-Boot can read environment variables from file or directly from memory too. This functionality also opens doors for writing scripts. The U-Boot allows scripts to have scripts tags within environment files. This opens a whole dimension of boot time scripting dynamically without the need of recompiling U-Boot. 

Ethernet and USB

The U-Boot provides supports for Ethernet and USB support at native level. This means you can mount USB devices and Ethernet devices and communicate with them at bootloader time. Most important use of this is to download kernel images from USB or ethernet locations. This simplifies programmer’s work as they compile and test 100’s of images while developing. 

Numbers

The U-Boot understands numbers in hexadecimal. For example, U-Boot

understands the number 30100000 as 0x30100000.

The boot process

We can divide the boot process in two major parts just to make it easier to understand. First step happens within SOC. SOC manufacturers mostly provide different peripheral support for boot purposes. You can refer to the SOC data sheet or technical reference manual for details under section boot registers. These boot registers are external registers(Hardware lines/GPIO) which will define the boot sequence. 

AM335X Technical Reference Module (Page 5035)

BeagleBone Black Schematics (Page 6)

In the second step SOC will look for bootloader images available according to the boot sequence specified. In case of SOC with limited memory a SPL(Secondary Program Loader) can be used to load U-Boot. The U-Boot can generate SPL automatically if specified in compilation variables. 

Once U-Boot finishes its booting, it tries to load the kernel image and DTB as specified in the environment file. 

U-Boot commands

Built-in commands

You should use “help” or “?” command to see all available commands in the U-Boot version you use.

Environment variables

Define simple variables

# setenv myPasscode 123456

# printenv myPasscode

myPasscode=123456

Use simple variable at other locations

# setenv myPasscode 123456

# setenv var This is my passcode: ${myPasscode}

# printenv var

var=This is my passcode: 123456

Use 2 variables in one statement

# setenv dumpaddr md.b \${addr} \${bytes}

# printenv dumpaddr

dumpaddr=md.b ${addr} ${bytes}

# setenv addr 2C000

# setenv bytes 5

# run dumpaddr

0002c000: 00 00 00 00 00 …..

Scripts

You can set 3 different variable using setenv and run them into sequence using run command. 

# setenv cmd1 setenv var val

# setenv cmd2 printenv var

# setenv cmd3 saveenv

# run cmd1 cmd2 cmd3

Or you can use “;” to write it in a single command.

# setenv cmd1 setenv var val\;printenv var\;saveenv

# run cmd1

Bootscript

The bootscript works in the following way:

U-Boot checks the loadbootsc variable. If set to “no”, it continues normal execution.

If the variable loadbootsc is set to “yes” then U-Boot tries to download the bootscript file with the filename stored in bootscript variable from the TFTP server IP address defined at variable serverip.

If the bootscript file is successfully downloaded, it is executed.

If any of the commands in the bootscript fails, the rest of the script is canceled.

When the bootscript has been fully executed (or canceled) U-Boot continues normal execution.

Creating a bootscript

For example, create a file called myscript.txt with the following contents:

setenv company hirun

setenv bootdelay 1

printenv company

setenv loadbootsc no

saveenv

Execute the mkimage tool (provided with U-Boot) with the file above as input file. Syntax is:

mkimage -T script -n “Bootscript” -C none -d <input_file> <output_file>