Compiling Raspberry Pi firmware with Docker
Tags: docker, buildroot, compilation
Published: 2020-10-22 15:00:34

Firmware compilation is a very common process in the embedded systems. Under this context, there are some tools such as [Buildroot](https://buildroot.org/) and [Yocto](https://www.yoctoproject.org/) that can ease this task. However, there are situations that require some specific versions of some libraries and packages that can provoke some conflicts with the rest of applications in the development station. To avoid this, some virtualization mechanisms as [Docker](https://www.docker.com/) can be used to isolate the compilation process inside a single container. In this post, we explain a method to compile the [Raspberry Pi](https://www.raspberrypi.org/products/) firmware using Buildroot and Docker. The first step is to install Docker in the workstation. This can be slightly different depending on the operating system but for Debian-like distributions can be performed as follows:
<pre>
user@workstation $> apt-get install docker docker.io docker-compose
user@workstation $> groupadd docker
user@workstation $> usermod -aG docker user
</pre>

Once Docker has been installed, the next step is to create a specific container for the compilation process (together with a shared folder between the host and the container):
<pre>
user@workstation $> cd ~
user@workstation $> mkdir docker-shared
user@workstation $> docker run -it --name compilation_rpi -v ~/docker-shared:/mnt ubuntu:18.04
</pre>

When the previous commands finish, a new session inside the container should be initialized:
<pre>
root@9f9b62ea1496:/#
</pre>

Before compiling Buildroot, some libraries and packages must be installed inside the container:
<pre>
root@9f9b62ea1496:/# apt-get update
root@9f9b62ea1496:/# apt-get install -y git vim make gcc autoconf \
  texinfo file g++ unzip bison flex m4 build-essential gettext \  
  libncursesw5 libncursesw5-dev fakeroot wget rename cpio \
  python rsync bc device-tree-compiler u-boot-tools \
  libssl-dev libtool
</pre>

The next step consists on downloading Buildroot:
<pre>
root@9f9b62ea1496:/# cd /mnt
root@9f9b62ea1496:/# mkdir rpi-compilation
root@9f9b62ea1496:/# cd rpi-compilation
root@9f9b62ea1496:/# wget https://buildroot.org/downloads/buildroot-2020.02.7.tar.gz
root@9f9b62ea1496:/# tar xvf buildroot-2020.02.7.tar.gz 
</pre>

Finally, move to the Buildroot folder, configure it for the Raspberry and compile it using make command:
<pre>
root@9f9b62ea1496:/# cd /mnt/rpi-compilation/buildroot-2020.02.7
root@9f9b62ea1496:/# make raspberrypi3_64_defconfig
root@9f9b62ea1496:/# export FORCE_UNSAFE_CONFIGURE=1
root@9f9b62ea1496:/# make
</pre>

Thanks for reading!! =)

**Notes**
- The firmware images can be found in `/mnt/rpi-compilation/buildroot-2020.02.7/output/images` or in `~/docker-shared/rpi-compilation/buildroot-2020.02.7/output/images`
- FORCE_UNSAFE_CONFIGURE=1 is required to avoid errors trying to compile Buildroot as root
- To exit from docker container, just run exit command.
- To initialize the docker container again, run the following commands:
<pre>
user@workstation $> docker start compilation_rpi
user@workstation $> docker exec -it compilation_rpi bash
</pre>