1RPi4 custom image
2#################
3
4This chapter offers a detailed guide for creating a custom image for the
5Raspberry Pi 4 (RPi4). Key Buildroot components and concepts will be
6introduced. The process outlined here can be adapted for use with
7other boards. For advanced features not covered in this guide, please consult
8the `Buildroot manual <https://buildroot.org/downloads/manual/manual.html>`_
9
10This guide was done on ``Ubuntu 22.04``
11
12Prepare the workspace
13*********************
14
15The workspace is structured to utilize Buildroot without any modifications.
16Execute the following commands:
17
18.. code-block::
19
20    mkdir rpi4_buildroot_lvgl && cd rpi4_buildroot_lvgl
21    mkdir resources && cd resources
22    mkdir board
23    mkdir configs
24    touch Config.in && touch external.desc && touch external.mk
25    echo "name: rpi4_lvgl" >> external.desc
26    echo "desc: rpi4 lvgl custom build" >> external.desc
27    cd ..
28
29Each folder utility will be explained throughout the guide.
30
31Get Buildroot
32*************
33
34First, according to the `Builroot Manual <https://buildroot.org/downloads/
35manual/manual.html>`_, Buildroot requires certain packages to be installed
36before starting the build. Lets install them using Ubuntu's package manager.
37
38.. code-block:: bash
39
40    sudo apt install sed make binutils gcc g++ bash patch gzip bzip2 perl tar \
41            cpio python3 unzip rsync wget libncurses-dev
42
43Now that we have satisfied all the prerequisites lets download a stable release
44of Buildroot.
45
46.. code-block:: bash
47
48    git clone --branch 2024.08 --depth 1 https://github.com/buildroot/buildroot.git
49
50Create RPi4 image
51*****************
52
53Let's see if there is already a configuration for RPi4 in Buildroot:
54
55.. code-block:: bash
56
57    cd buildroot
58    make list-defconfigs | grep rasp
59
60There is a build available for RPi4 62 bits: ``raspberrypi4_64_defconfig``.
61
62You can also find all the configurations in the Buildroot repository
63``buildroot > configs``
64
65Start with this configuration as a base, but this configuration needs to be
66changed to meet lvgl requirements. So let's copy this base and keep it out of
67Buildroot.
68
69.. code-block:: bash
70
71    # move to the project root
72    cp buildroot/configs/raspberrypi4_64_defconfig resources/configs/raspberrypi4_lvgl_defconfig
73
74Create a new file ``build.sh`` at the root of the project with this content:
75
76.. code-block:: bash
77
78    #!/bin/bash
79    SCRIPTPATH="$( cd "$(dirname "$0")" >/dev/null 2>&1 ; pwd -P )"
80
81    cd $SCRIPTPATH
82    mkdir -p output
83    cd buildroot
84    make defconfig O=../output BR2_DEFCONFIG=../resources/configs/raspberrypi4_lvgl_defconfig
85
86Make this file executable.
87
88.. code-block:: bash
89
90    chmod +x build.sh
91    ./build.sh
92
93Running this script creates an output folder that contains everything the
94process will generate:
95
96    - Fetched resources
97    - Toolchain
98    - Images
99
100This helps maintain a clean Buildroot directory. When the script is executed,
101an (almost) empty folder named ``output`` is created, as the build process has
102not yet begun.
103
104Let's modify the configuration with ``menuconfig``.
105
106.. code-block:: bash
107
108    cd output
109    make menuconfig
110
111Change these configurations:
112
113    - System host name to ``lvgl-buildroot`` under ``system_configuration > System hostname``.
114    - Root password to ``lvgl-buildroot``  under ``system_configuration > Root password``.
115    - Enable SSH in ``target-packages > Networking applications > openssh``
116
117.. _build-environment:
118
119Build the image
120
121.. code-block:: bash
122
123    make
124
125What happened?
126**************
127
128Let's explore the contents of the output folder and what it contains.
129
130build
131=====
132
133This folder contains the intermediate files and build artifacts for the various
134packages that are being compiled. Each package has its own subdirectory within
135the ``build`` folder, where the build process takes place. It includes files
136such as configuration files, source code, and object files that are generated
137as part of the build process.
138
139host
140====
141
142The ``host`` folder contains files and binaries that are built for the host
143system rather than the target system. This includes tools and utilities that
144are needed to build packages or to run the build system itself. It may contain
145compilers, build tools, and libraries that are required to support the build
146process for the target.
147
148.. _images:
149
150images
151======
152
153This directory holds the final output images generated for the target system,
154such as filesystem images, kernel images, or bootloader images. Depending on
155the configuration, you may find files like ``rootfs.tar``, ``zImage``,
156``uImage``, or others that are ready to be deployed onto the target hardware.
157
158target
159======
160
161The ``target`` folder contains the files that are specifically intended for the
162target system. This includes the root filesystem and any additional files that
163will be included in the target environment. The structure within this folder
164often mimics the directory structure of a standard Linux system, containing
165directories like ``bin``, ``lib``, ``etc``, ``usr``, and others, which hold the
166binaries, libraries, configuration files, and other necessary components for
167the target system to function properly.
168
169.. _flash_the_image:
170
171Flash the image
172***************
173
174Insert the SD card into the laptop and check its mount point. It is typically
175labeled as sda or sdb, but you can use the lsblk command to confirm this
176information.
177
178If it is mounted on /dev/sda, run the following command
179
180.. code-block:: bash
181
182    sudo dd if=images/sdcard.img of=/dev/sda
183
184As mentioned in :ref:`images`, the output image is in ``images`` and named
185``sdcard.img``.
186
187Connect an Ethernet cable to the RPi4 and ensure the laptop and the RPi4 are on
188the same network.
189
190You can use ``ifconfig`` or ``ip a`` to find your IP address. Then, use
191``nmap`` or any tool to scan the network and find the IP address of the
192RPi4.
193
194.. code-block:: bash
195
196    nmap -sn <YOUR_IP_ADDRESS>/24 | grep lvgl
197
198All of this can be done with the UART instead of SSH if you don't want to
199connect the RPi4 to the network.
200
201