Back
Close

Running U-Boot & Linux Kernel in QEMU

Donotalo
9,610 views

Installing Pre-requisites

Following command can be executed to install all software necessary for this tutorial:

sudo apt install build-essential libncurses-dev rsync git ninja-build libglib2.0-dev libpixman-1-dev bison flex libssl-dev 

Prepare a working directory:

mkdir tech.io && cd tech.io

This will be considered as the root working directory for this tutorial.

Installing Toolchain

Buildroot is a software package that can generate necessary tools for cross compiling code base for embedded Linux. This tutorial will use version v2023.02.

# Download the file
wget https://buildroot.org/downloads/buildroot-2023.02.tar.gz

# Extract from compressed file
tar xf buildroot-2023.02.tar.gz

tar parameters:

  • x = Extract files
  • f = Use archive file
cd buildroot-2023.02

# To generate toolchain configuration file
make menuconfig

Select the following options:

  1. Target options
    1. Target Architecture > RISCV
    2. Target Architecture Size > 64-bit
  2. Toolchain
    1. C library > musl
    2. Kernel Headers > Linux 6.1.x kernel headers
    3. Binutils Version > binutils 2.39
    4. GCC compiler Version > gcc 12.x

Exit and save the configuration. Build the toolchain:

make sdk -j$(nproc)
  • -j$(nproc) = $(nproc) will expand to number of available processing units, -j flag will paralellize build utilizing the output indicated by $(nproc)

The output is the file output/images/riscv64-buildroot-linux-musl_sdk-buildroot.tar.gz. Extract it:

# Create a toolchain directory for toolchain extraction
cd ..
mkdir toolchain && cd toolchain

tar xf ../buildroot-2023.02/output/images/riscv64-buildroot-linux-musl_sdk-buildroot.tar.gz

All the important binaries are now in riscv64-buildroot-linux-musl_sdk-buildroot/bin directory. The RISC-V compier is riscv64-linux-gcc. Fix the hardcoded paths by running the script:

riscv64-buildroot-linux-musl_sdk-buildroot/relocate-sdk.sh

Place the binaries in system path:

# Go to the working directory
cd ..

# Create a script to update system path
gedit tech.io-env.sh &

Write the following in the tech.io-env.sh file:

export PATH=~/tech.io/toolchain/riscv64-buildroot-linux-musl_sdk-buildroot/bin:$PATH

Update environment variable by running the script using source & test:

source tech.io-env.sh
riscv64-linux-gcc --version

It presented the version as follows:

riscv64-linux-gcc.br_real (Buildroot 2023.02) 12.2.0
Copyright (C) 2022 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

This confirms that the necessary toolchain has been installed and added to the system path.

Create your playground on Tech.io
This playground was created on Tech.io, our hands-on, knowledge-sharing platform for developers.
Go to tech.io