Booting Linux with U-BOOT

Reading Time: 3 minutes
This post is a small introduction on how to boot Linux using U-Boot. We’ll use QEMU virtual machine for ARM architecture. Thus the following are required to follow along with this post.

Software Required

  • QEMU with ARM 64 bit support.
  • Linux Kernel, a recent one will do or a one with Arm 64 bit (AARCH) support.
  • A Cross Compiler toolchain. This can be downloaded from Linaro or ARM
  • U-BOOT with Virtio support.
  • BusyBox

What we want to do?

The basic idea is to be able to create a system, much like we’ve one on which we’re developing, and put this system on a media which can be booted.

Since we use a Computer on a daily basis we know somethings that must be present in this new system we would like to build

  1. A way to save files, i.e a File System.
  2. A way to make sure devices we plugin in works. Thus a bunch of drivers.
  3. A way to upgrade this new system.
  4. A way to boot it automatically when it powers on.

These are some of the basic things we need to do in order to make this new system work, however depending what you would like to build it for, viz, a router or say a streaming device you might want to add or remove some of the features to this basic list.

How we want to do this?

We might want to go with some general purpose distribution viz OpenSuSE or Ubuntu however since we want to understand the fundamentals here we’ll just use BusyBox

The following steps can be done in order to achieve this sytem

  1. Compile our Boot Loader, i.e. U-BOOT. Make sure to compile it using the none instead of linux-eabi.
  2. Compile our Linux kernel and required drivers.
  3. Compile our Busybox.
  4. Package the above on a media.

Compiling U-BOOT

This one is pretty straight forward. Since we’ll be using Arm 64 bit virtual machine we’ll use that configuration. The following commands show how this can be done

#The following command needs to be run from
#u-boot directory

~/u-boot> ARCH=arm64 CROSS_COMPILE=aarch64-none-elf-gnu- \
make qemu_arm64_defconfig

~/u-boot> ARCH=arm64 CROSS_COMPILE=aarch64-none-elf-gnu- \
make -j16 

Once the above command is complete we’ll have a file named u-boot 

~/u-boot> file u-boot
u-boot: ELF 64-bit LSB pie executable, ARM aarch64, version 1 (SYSV), statically linked, with debug_info, not stripped 

Compiling Linux kernel

We’ll use the same tool chain used to compile U-Boot for compiling Linux kernel. Since we want to run this on QEMU virtual machine therefore we need to select that machine only as shown below

~/kernel_source/linux> ARCH=arm64 \
CROSS_COMPILE=aarch64-none-elf-gnu- make menuconfig 

We need to select ARM Software Emulation machine which can be done by selecting the following option

Platform Selection
QEMU Platform Selection

As shown in the above image we need to select ARMv8 software model. Note that the CPU supported is Cortex-A57 for this machine. 

Compiling BusyBox

Since BusyBox is an application program it needs supports from the kernel, and thus needs to be build from the none-linux toolchain instead of none-elf tool

busybox> ARCH=arm64 CROSS_COMPILE=aarch64 \
-none-linux-gnu- make menuconfig 

We’ve two choices here, to either statically link everything or dynamically link all applets (commands). For this tutorial we’ll go with the dynamically link option to show the extra steps which need to be done in order to run these programs. This can be done on the Settings  option of menuconfig.

BusyBox Settings
Dynamic Linking BusyBox

Once this Step is done then we’ve created everything we require to make this thing work. All that’s needed now is to package it. We’ll see that in the next post

Leave a Reply