Embedded Linux Slides
Embedded Linux Slides
Corrections, suggestions, contributions and translations are welcome! embedded Linux and kernel engineering
Send them to [email protected]
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 1/531
Embedded Linux system development training
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 2/531
About Bootlin
About Bootlin
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 3/531
Bootlin introduction
▶ Engineering company
• In business since 2004
• Before 2018: Free Electrons
▶ Team based in France and Italy
▶ Serving customers worldwide
▶ Highly focused and recognized expertise
• Embedded Linux
• Linux kernel
• Embedded Linux build systems
▶ Strong open-source contributor
▶ Activities
• Engineering services
• Training courses
▶ https://bootlin.com
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 4/531
Bootlin engineering services
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 5/531
Bootlin training courses
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 6/531
Bootlin, an open-source contributor
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 7/531
Bootlin on-line resources
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 8/531
Generic course information
Generic course
information
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 9/531
Supported hardware
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 10/531
Shopping list: hardware for this course
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 11/531
Labs proposed on another platform
After this course, you can also run all labs on the
Beaglebone Black or Beaglebone Black Wireless ARM board.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 12/531
Labs proposed on another platform
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 13/531
Training quiz and certificate
▶ You have been given a quiz to test your knowledge on the topics covered by the
course. That’s not too late to take it if you haven’t done it yet!
▶ At the end of the course, we will submit this quiz to you again. That time, you
will see the correct answers.
▶ It allows Bootlin to assess your progress thanks to the course. That’s also a kind
of challenge, to look for clues throughout the lectures and labs / demos, as all the
answers are in the course!
▶ Another reason is that we only give training certificates to people who achieve at
least a 50% score in the final quiz and who attended all the sessions.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 14/531
Participate!
During the lectures...
▶ Don’t hesitate to ask questions. Other people in the audience may have similar
questions too.
▶ Don’t hesitate to share your experience too, for example to compare Linux with
other operating systems you know.
▶ Your point of view is most valuable, because it can be similar to your colleagues’
and different from the trainer’s.
▶ In on-line sessions
• Please always keep your camera on!
• Also make sure your name is properly filled.
• You can also use the ”Raise your hand” button when you wish to ask a question but
don’t want to interrupt.
▶ All this helps the trainer to engage with participants, see when something needs
clarifying and make the session more interactive, enjoyable and useful for everyone.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 15/531
Collaborate!
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 16/531
Introduction to Embedded Linux
Introduction to
Embedded Linux
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 17/531
Birth of Free Software
▶ 1983, Richard Stallman, GNU project and the free
software concept. Beginning of the development of gcc,
gdb, glibc and other important tools
▶ 1991, Linus Torvalds, Linux kernel project, a UNIX-like
operating system kernel. Together with GNU software and
many other open-source components: a completely free
operating system, GNU/Linux
▶ 1995, Linux is more and more popular on server systems
▶ 2000, Linux is more and more popular on embedded
systems
▶ 2008, Linux is more and more popular on mobile devices Richard Stallman in 2019
https://commons.wikimedia.org/
and phones wiki/File:Richard_Stallman_at_
LibrePlanet_2019.jpg
▶ 2012, Linux is available on cheap, extensible hardware:
Raspberry Pi, BeagleBone Black
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 18/531
Free software?
▶ A program is considered free when its license offers to all its users the following
four freedoms
• Freedom to run the software for any purpose
• Freedom to study the software and to change it
• Freedom to redistribute copies
• Freedom to distribute copies of modified versions
▶ These freedoms are granted for both commercial and non-commercial use
▶ They imply the availability of source code, software can be modified and
distributed to customers
▶ Good match for embedded systems!
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 19/531
What is embedded Linux?
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 20/531
Advantages of Linux and Open-Source in embedded systems
▶ Ability to reuse components
Many features, protocols and hardware are
supported. Allows to focus on the added ▶ Quality
value of your product. Your system is built on high-quality
foundations (kernel, compiler, C-library,
▶ Low cost
base utilities...). Many Open-Source
No per-unit royalties. Development tools applications have good quality too.
free too. But of course deploying Linux
costs time and effort. ▶ Community support
Can get very good support from the
▶ Full control
community if you approach it with a
You decide when to update components constructive attitude.
in your system. No vendor lock-in. This
secures your investment. ▶ Participation in community work
Possibility to collaborate with peers and
▶ Easy testing of new features
get opportunities beyond corporate
No need to negotiate with third-party barriers.
vendors. Just explore new solutions
released by the community.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 21/531
Introduction to Embedded Linux
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 22/531
Wireless routers
Product from BLOKS (http://bloks.de). Permission to use this picture only in this document, in updates and in translations.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 25/531
Robots
See the Linux on Mars: How the Perseverance Rover and Ingenuity
Image credits: Wikipedia Helicopter Leveraged Linux to Accomplish their Mission presentation from
Tim Canham (JPL, NASA): https://youtu.be/0_GfMcBmbCg?t=111
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 27/531
Introduction to Embedded Linux
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 28/531
Processor and architecture (1)
The Linux kernel and most other architecture-dependent components support a wide
range of 32 and 64 bit architectures
▶ x86 and x86-64, as found on PC platforms, but also embedded systems
(multimedia, industrial)
▶ ARM, with hundreds of different System on Chips
(SoC: CPU + on-chip devices, for all sorts of products)
▶ RISC-V, the rising architecture with a free instruction set
(from high-end cloud computing to the smallest embedded systems)
▶ PowerPC (mainly real-time, industrial applications)
▶ MIPS (mainly networking applications)
▶ Microblaze (Xilinx), Nios II (Altera): soft cores on FPGAs
▶ Others: ARC, m68k, Xtensa, SuperH...
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 29/531
Processor and architecture (2)
▶ Both MMU and no-MMU architectures are supported, even though no-MMU
architectures have a few limitations.
▶ Linux does not support small microcontrollers (8 or 16 bit)
▶ Besides the toolchain, the bootloader and the kernel, all other components are
generally architecture-independent
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 30/531
RAM and storage
▶ RAM: a very basic Linux system can work within 8 MB of RAM, but a more
realistic system will usually require at least 32 MB of RAM. Depends on the type
and size of applications.
▶ Storage: a very basic Linux system can work within 4 MB of storage, but usually
more is needed.
• Block storage: SD/MMC/eMMC, USB mass storage, SATA, etc,
• Raw flash storage is supported too, both NAND and NOR flash, with specific
filesystems
▶ Not necessarily interesting to be too restrictive on the amount of RAM/storage:
having flexibility at this level allows to increase performance and re-use as many
existing components as possible.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 31/531
Communication
▶ The Linux kernel has support for many common communication buses
• I2C
• SPI
• 1-wire
• SDIO
• PCI
• USB
• CAN (mainly used in automotive)
▶ And also extensive networking support
• Ethernet, Wifi, Bluetooth, CAN, etc.
• IPv4, IPv6, TCP, UDP, SCTP, DCCP, etc.
• Firewalling, advanced routing, multicast
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 32/531
Types of hardware platforms (1)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 33/531
Types of hardware platforms (2)
▶ Most SoCs are delivered with support for the Linux kernel and for an open-source
bootloader.
▶ Having support in the official versions of the projects (kernel, bootloader) is a lot
better: quality is better, new versions are available, and Long Term Support
releases are available.
▶ Some SoC vendors and/or board vendors do not contribute their changes back to
the mainline Linux kernel. Ask them to do so, or use another product if you can.
A good measurement is to see the delta between their kernel and the official one.
▶ Between properly supported hardware in the official Linux kernel and
poorly-supported hardware, there will be huge differences in development
time and cost.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 35/531
Introduction to Embedded Linux
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 36/531
Host and target
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 37/531
Software components
▶ Cross-compilation toolchain
• Compiler that runs on the development machine, but generates code for the target
▶ Bootloader
• Started by the hardware, responsible for basic initialization, loading and executing
the kernel
▶ Linux Kernel
• Contains the process and memory management, network stack, device drivers and
provides services to user space applications
▶ C library
• Of course, a library of C functions
• Also the interface between the kernel and the user space applications
▶ Libraries and applications
• Third-party or in-house
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 38/531
Embedded Linux work
Several distinct tasks are needed when deploying embedded Linux in a product:
▶ Board Support Package development
• A BSP contains a bootloader and kernel with the suitable device drivers for the
targeted hardware
• Purpose of our Kernel Development course
▶ System integration
• Integrate all the components, bootloader, kernel, third-party libraries and
applications and in-house applications into a working system
• Purpose of this course
▶ Development of applications
• Normal Linux applications, but using specifically chosen libraries
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 39/531
Embedded Linux development environment
Embedded Linux
development
environment
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 40/531
Embedded Linux solutions
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 41/531
OS for Linux development
We strongly recommend to use GNU/Linux as the desktop operating system to
embedded Linux developers, for multiple reasons.
▶ All community tools are developed and designed to run on Linux. Trying to use
them on other operating systems (Windows, macOS) will lead to trouble.
▶ As Linux also runs on the embedded device, all the knowledge gained from using
Linux on the desktop will apply similarly to the embedded device.
▶ If you are stuck with a Windows desktop, at least you should use GNU/Linux in a
virtual machine (such as VirtualBox which is open source), though there could be
a small performance penalty. With Windows 10/11, you can also run your favorite
native Linux distro through Windows Subsystem for Linux (WSL2)
:-)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 42/531
Desktop Linux distribution
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 43/531
Host vs. target
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 44/531
Serial line communication program
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 45/531
Practical lab - Training Setup
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 46/531
Cross-compiling toolchains
Cross-compiling
toolchains
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 47/531
Cross-compiling toolchains
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 48/531
Toolchain definition (1)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 49/531
Toolchain definition (2)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 50/531
Architecture tuple and toolchain prefix
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 51/531
Components of gcc toolchains
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 52/531
Binutils
▶ Binutils is a set of tools to generate and manipulate binaries (usually with the
ELF format) for a given CPU architecture
• as, the assembler, that generates binary code from assembler source code
• ld, the linker
• ar, ranlib, to generate .a archives (static libraries)
• objdump, readelf, size, nm, strings, to inspect binaries. Very useful analysis tools!
• objcopy, to modify binaries
• strip, to strip parts of binaries that are just needed for debugging (reducing their
size).
▶ GNU Binutils: https://www.gnu.org/software/binutils/, GPL license
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 53/531
C/C++ compiler
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 54/531
Kernel headers (1)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 55/531
Kernel headers (2)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 56/531
Kernel headers (3)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 57/531
C standard library
Application
• Interface between the applications and the POSIX-compatible
n
tio
nc lls
kernel fu
ca
BusyBox
m calls
syste
Linux-specific
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 58/531
glibc
▶ License: LGPL
▶ C standard library from the GNU project
▶ Designed for performance, standards compliance and
portability
▶ Found on all GNU / Linux host systems
▶ Of course, actively maintained
▶ By default, quite big for small embedded systems. On
armv7hf, version 2.31: libc: 1.5 MB, libm: 432 KB,
source: https://toolchains.bootlin.com
▶ https://www.gnu.org/software/libc/
Image source
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 59/531
uClibc-ng
▶ https://uclibc-ng.org/
▶ A continuation of the old uClibc project, license: LGPL
▶ Lightweight C standard library for small embedded systems
• High configurability: many features can be enabled or disabled through a
menuconfig interface.
• Supports most embedded architectures, including MMU-less ones (ARM Cortex-M,
Blackfin, etc.). The only standard library supporting ARM noMMU.
• No guaranteed binary compatibility. May need to recompile applications when the
library configuration changes.
• Some features may be implemented later than on glibc (real-time, floating-point
operations...)
• Focus on size (RAM and storage) rather than performance
• Size on armv7hf, version 1.0.34: libc: 712 KB, source:
https://toolchains.bootlin.com
▶ Actively supported, but Yocto Project stopped supporting it.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 60/531
musl C standard library
https://www.musl-libc.org/
▶ A lightweight, fast and simple standard library for embedded systems
▶ Created while uClibc’s development was stalled
▶ In particular, great at making small static executables, which can run
anywhere, even on a system built from another C standard library.
▶ More permissive license (MIT), making it easier to release static
executables. We will talk about the requirements of the LGPL license
(glibc, uClibc) later.
▶ Supported by build systems such as Buildroot and Yocto Project.
▶ Used by the Alpine Linux lightweight distribution
(https://www.alpinelinux.org/)
▶ Size on armv7hf, version 1.2.0: libc: 748 KB, source:
https://toolchains.bootlin.com
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 61/531
Other smaller C libraries
▶ Several other smaller C libraries exist, but they do not implement the full POSIX
interface required by most Linux applications
▶ They can run only relatively simple programs, typically to make very small static
executables and run in very small root filesystems.
▶ Therefore not commonly used in most embedded Linux systems
▶ Choices:
• Newlib, https://sourceware.org/newlib/, maintained by Red Hat, used mostly in
Cygwin, in bare metal and in small POSIX RTOS.
• Klibc, https://en.wikipedia.org/wiki/Klibc, from the kernel community,
designed to implement small executables for use in an initramfs at boot time.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 62/531
Advice for choosing the C standard library
▶ Advice to start developing and debugging your applications with glibc, which is
the most standard solution
▶ If you have size constraints, try to compile your app and then the entire filesystem
with uClibc or musl
• The size advantage of uClibc or musl, which used to be a significant argument, is
less relevant with today’s storage capacities.
• Smaller binaries and filesystems remain useful when optimizing boot time, though,
typically booting on a filesystem loaded in RAM, and to reduce the size of container
and virtual machine images (one of the use cases of Alpine Linux).
▶ If you run into trouble, it could be because of missing features in the C standard
library.
▶ In case you wish to make static executables, musl will be an easier choice in terms
of licensing constraints.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 63/531
Linux vs. bare-metal toolchain
▶ A Linux toolchain
• is a toolchain that includes a Linux-ready C standard library, which uses the Linux
system calls to implement system services
• can be used to build Linux user-space applications, but also bare-metal code
(firmware, bootloader, Linux kernel)
• is identified by the linux OS identifier in the toolchain tuple: arm-linux,
arm-none-linux-gnueabihf
▶ A bare metal toolchain
• is a toolchain that does not include a C standard library, or a very minimal one that
isn’t tied to a particular operating system
• can be used to build only bare-metal code (firmware, bootloader, Linux kernel)
• is identified by the none OS identifier in the toolchain tuple: arm-none-eabi,
arm-none-none-eabi (vendor is none, OS is none)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 64/531
An alternate compiler suite: LLVM
▶ Most Embedded Linux projects use toolchains based on GNU project: GCC
compiler, binutils, GDB debugger
▶ The LLVM project has been developing an alternative compiler suite:
• Clang, C/C++ compiler, https://clang.llvm.org/
• LLDB, debugger, https://lldb.llvm.org/
• LLD, linker, https://lld.llvm.org/
• and more, see https://llvm.org/
▶ While they are used by several high-profile projects, they are not yet in widespread
use in most Embedded Linux projects.
▶ Initially had better code optimization and diagnostics than GCC, but thanks to
having competition, GCC has improved significantly in this area.
▶ Available under MIT/BSD licenses
▶ https://en.wikipedia.org/wiki/LLVM
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 65/531
Cross-compiling toolchains
Toolchain Options
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 66/531
ABI
▶ When building a toolchain, the ABI used to generate binaries needs to be defined
▶ ABI, for Application Binary Interface, defines the calling conventions (how
function arguments are passed, how the return value is passed, how system calls
are made) and the organization of structures (alignment, etc.)
▶ All binaries in a system are typically compiled with the same ABI, and the kernel
must understand this ABI.
▶ On ARM 32-bit, two main ABIs: EABI and EABIhf
• EABIhf passes floating-point arguments in floating-point registers → needs an ARM
processor with a FPU
▶ On RISC-V, several ABIs: ilp32, ilp32f, ilp32d, lp64, lp64f, and lp64d
▶ https://en.wikipedia.org/wiki/Application_Binary_Interface
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 67/531
Floating point support
▶ All ARMv7-A (32-bit) and ARMv8-A (64-bit) processors have a floating point unit
▶ RISC-V cores with the F extension have a floating point unit
▶ Some older ARM cores (ARMv4/ARMv5) or some RISC-V cores may not have a
floating point unit
▶ For processors without a floating point unit, two solutions for floating point
computation:
• Generate hard float code and rely on the kernel to emulate the floating point
instructions. This is very slow.
• Generate soft float code, so that instead of generating floating point instructions,
calls to a user space library are generated
▶ Decision taken at toolchain configuration time
▶ For processors with a floating point unit, sometimes different FPU are possible.
For example on ARM: VFPv3, VFPv3-D16, VFPv4, VFPv4-D16, etc.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 68/531
CPU optimization flags
▶ GNU tools (gcc, binutils) can only be compiled for a specific target architecture at
a time (ARM, x86, RISC-V...)
▶ gcc offers further options:
• -march allows to select a specific target instruction set
• -mtune allows to optimize code for a specific CPU
• For example: -march=armv7 -mtune=cortex-a8
• -mcpu=cortex-a8 can be used instead to allow gcc to infer the target instruction set
(-march=armv7) and cpu optimizations (-mtune=cortex-a8)
• https://gcc.gnu.org/onlinedocs/gcc/ARM-Options.html
▶ At the GNU toolchain compilation time, values can be chosen. They are used:
• As the default values for the cross-compiling tools, when no other -march, -mtune,
-mcpu options are passed
• To compile the C library
▶ Note: LLVM (Clang, LLD...) utilities support multiple target architectures at once.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 69/531
Cross-compiling toolchains
Obtaining a Toolchain
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 70/531
Building a toolchain manually
▶ Building a cross-compiling toolchain manually is a fairly difficult process
▶ Lots of details to learn: many components to build with complicated configuration
▶ Typical process is:
• Build dependencies of binutils/gcc (GMP, MPFR, ISL, etc.)
• Build binutils
• Build a baremetal, first stage GCC
• Extract kernel headers from the Linux source code
• Build the C library using the first stage GCC
• Build the second stage and final GCC supporting the Linux OS and the C library.
▶ Many decisions to make about the components: C library, gcc and binutils
versions, ABI, floating point mechanisms, etc. Not trivial to find correct
combinations of these possibilities
▶ See the Crosstool-NG documentation for details on how toolchains are built.
▶ Talk: Anatomy of Cross-Compilation Toolchains, by Thomas Petazzoni, ELCE
2017, video and slides
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 71/531
Get a pre-compiled toolchain
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 72/531
Example of toolchains from ARM: downloading
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 73/531
Example of toolchains from ARM: using
$ wget https://developer.arm.com/-/media/Files/downloads/gnu-a/10.3-2021.07/binrel/[...]
[...]gcc-arm-10.3-2021.07-x86_64-arm-none-linux-gnueabihf.tar.xz
$ tar xf gcc-arm-10.3-2021.07-x86_64-arm-none-linux-gnueabihf.tar.xz
$ cd gcc-arm-10.3-2021.07-x86_64-arm-none-linux-gnueabihf/
$ file test
test: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, [...]
for GNU/Linux 3.2.0, with debug_info, not stripped
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 74/531
Toolchain building utilities
Another solution is to use utilities that automate the process of building the
toolchain
▶ Same advantage as the pre-compiled toolchains: you don’t need to mess up with
all the details of the build process
▶ But also offers more flexibility in terms of toolchain configuration, component
version selection, etc.
▶ Allows to rebuild the toolchain if needed to fix a bug or security issue.
▶ They also usually contain several patches that fix known issues with the different
components on some architectures
▶ Multiple tools with identical principle: shell scripts or Makefile that automatically
fetch, extract, configure, compile and install the different components
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 75/531
Toolchain building utilities (2)
Crosstool-ng
▶ Rewrite of the older Crosstool, with a
menuconfig-like configuration system
▶ Feature-full: supports uClibc, glibc and musl,
hard and soft float, many architectures
▶ Actively maintained
▶ https://crosstool-ng.github.io/
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 76/531
Toolchain building utilities (3)
Many root filesystem build systems also allow the construction of a cross-compiling
toolchain
▶ Buildroot
• Makefile-based. Can build glibc, uClibc and musl based toolchains, for a wide range
of architectures. Use make sdk to only generate a toolchain.
• https://buildroot.org
▶ PTXdist
• Makefile-based, maintained mainly by Pengutronix, supporting only glibc and uClibc
(version 2023.01 status)
• https://www.ptxdist.org/
▶ OpenEmbedded / Yocto Project
• A featureful, but more complicated build system, supporting only glibc and musl.
• https://www.openembedded.org/
• https://www.yoctoproject.org/
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 77/531
Crosstool-NG: download
▶ Getting Crosstool-NG
$ git clone https://github.com/crosstool-ng/crosstool-ng.git
▶ Using a well-known stable version
$ cd crosstool-ng
$ git checkout crosstool-ng-1.25.0
▶ As we’re fetching from Git, the configure script needs to be generated:
$ ./bootstrap
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 78/531
Crosstool-NG: installation
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 79/531
Crosstool-NG: toolchain configuration
▶ Once installed, the ct-ng tool allows to configure and build an arbitrary number
of toolchains
▶ Its configuration system is based on kconfig, like the Linux kernel configuration
system
▶ Configuration of the toolchain to build stored in a .config file
▶ Example configurations provided with Crosstool-NG
• List: ./ct-ng list-samples
• Load an example: ./ct-ng <sample-name>, replaces .config
• For example ./ct-ng aarch64-unknown-linux-gnu
• No sample loaded → default Crosstool-NG configuration is a bare-metal toolchain
for the Alpha CPU architecture!
▶ The configuration can then be refined using either:
• ./ct-ng menuconfig
• ./ct-ng nconfig
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 80/531
Crosstool-NG: toolchain configuration
./ct-ng menuconfig
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 81/531
Crosstool-NG: toolchain building
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 82/531
Important toolchain contents
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 83/531
Practical lab - Using Crosstool-NG
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 84/531
Bootloaders and firmware
Bootloaders and
firmware
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 85/531
Bootloaders and firmware
Introduction
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 86/531
Bootloader role
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 87/531
Bootloaders and firmware
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 88/531
Legacy BIOS booting (1)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 89/531
Legacy BIOS booting (2)
▶ Due to the limitation in size of the bootloader, bootloaders are split into two
stages
• Stage 1, which fits within the 446 bytes constraint
• Stage 2, which is loaded by stage 1, and can therefore be bigger
▶ Stage 2 is typically stored outside of any filesystem, at a fixed offset → simpler to
load by stage 1
▶ Stage 2 generally has filesystem support, so it can load the kernel image from a
filesystem
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 90/531
Legacy BIOS booting: sequence and storage
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 91/531
UEFI booting
▶ Starting from 2005-2006, UEFI is the new firmware interface on x86 platforms
• Unified Extensible Firmware Interface
• Describes the interface between the operating system and the firmware
• Firmware in charge of booting
• Firmware also provides runtime services to the operating system
• Stored in some flash memory, outside of regular user-accessible storage devices
▶ Loads EFI binaries from the EFI System Partition
• Generally a bootloader
• Can also be directly the Linux kernel, with an EFI Boot Stub
▶ Special partition, formatted with the FAT filesystem
• MBR: identified by type 0xEF
• GPT: identified with a specific globally unique identifier
▶ File /efi/boot/bootx32.efi, /efi/boot/bootx64.efi
▶ https://en.wikipedia.org/wiki/UEFI
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 92/531
UEFI booting: sequence and storage
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 93/531
ACPI
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 94/531
UEFI and ACPI on ARM
▶ Historically UEFI and ACPI are technologies coming from the Intel/x86 world
▶ ARM is also pushing for the adoption of UEFI and ACPI as part of its ARM
System Ready certification
• Mainly for servers/workstations SoCs
• Does not impact embedded SoCs
• Currently not common in embedded Linux projects on ARM
• https://www.arm.com/architecture/system-architectures/systemready-
certification-program
▶ Also some on-going effort to use UEFI on RISC-V, but not the de-facto standard
▶ When an embedded platform uses UEFI → its booting process is similar to an x86
platform
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 95/531
Bootloaders and firmware
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 96/531
Booting on embedded platforms: ROM code
▶ Most embedded processors include a ROM code that implements the initial step
of the boot process
▶ The ROM code is written by the processor vendor and directly built into the
processor
• Cannot be changed or updated
• Its behavior is described in the processor datasheet
▶ Responsible for finding a suitable bootloader, loading it and running it
• From NAND/NOR flash, from USB, from SD card, from eMMC, etc.
• Well defined location/format
▶ Generally runs with the external RAM not initialized, so it can only load the
bootloader into an internal SRAM
• Limited size of the bootloader, due to the size of the SRAM
• Forces the boot process to be split in two steps: first stage bootloader (small, runs
from SRAM, initializes external DRAM), second stage bootloader (larger, runs from
external DRAM)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 97/531
Booting on STM32MP1: datasheet
Source: https://www.st.com/resource/en/
application_note/dm00389996-getting-
started-with-stm32mp151-stm32mp153-and-
stm32mp157-line-hardware-development-
stmicroelectronics.pdf
Useful details:
https://wiki.st.com/stm32mpu/wiki/
STM32_MPU_ROM_code_overview
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 98/531
Booting on AM335x (32 bit BeagleBone): datasheet
Source:
https://www.mouser.com/pdfdocs/spruh73h.pdf,
chapter 26
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 99/531
Two stage booting sequence
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 100/531
ROM code recovery mechanism
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 101/531
Bootloaders and firmware
Bootloaders
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 102/531
GRUB
▶ Grand Unified Bootloader, from the GNU
project
▶ De-facto standard in most Linux distributions
for x86 platforms
▶ Supports x86 legacy and UEFI systems
▶ Can read many filesystem formats to load the
kernel image, modules and configuration
▶ Provides a menu and powerful shell with
various commands
▶ Can load kernel images over the network
▶ Also supports ARM, ARM64, RISC-V,
PowerPC, but less popular than other
bootloaders on those platforms
▶ https://www.gnu.org/software/grub/
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 103/531
Syslinux
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 104/531
systemd-boot
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 105/531
shim
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 106/531
U-Boot
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 107/531
Barebox
▶ Another bootloader for most embedded CPU
architectures: ARM/ARM64, MIPS, PowerPC, RISC-V,
x86, etc.
▶ Initially developed as an alternative to U-Boot to
address some U-Boot shortcomings
• kconfig for the configuration like the Linux kernel
• well-defined device model internally
• More Linux-style shell interface
• Cleaner code base
▶ Actively maintained and developed, but
• Less widely used than U-Boot
• Less platform support than in U-Boot
▶ https://www.barebox.org/
▶ Talk barebox Bells and Whistles, by Ahmad Fatoum,
ELCE 2020, video and slides
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 108/531
Bootloaders and firmware
Trusted firmware
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 109/531
Concept
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 110/531
ARM
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 111/531
ARM exception levels and worlds
▶ Standardized by ARM
▶ Services
• implemented by the secure firmware
• called by the operating system
▶ Prevents the operating system running in normal world from
directly accessing critical hardware resources
▶ PSCI, Power State Coordination Interface
• Power management related: turn CPUs on/off, CPU idle state,
platform shutdown/reset
▶ SCMI, System Control and Management Interface
• Power domain, clocks, sensor, performance
▶ Secure firmware implementing these interfaces is
• Mandatory to run Linux on ARMv8
• Mandatory to run Linux on some ARMv7 platforms, but not all
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 113/531
TF-A
▶ Trusted Firmware-A (TF-A) provides a reference implementation of secure world
software for Armv7-A and Armv8-A, including a Secure Monitor executing at
Exception Level 3 (EL3)
▶ Formerly known as ATF, for ARM Trusted Firmware
▶ Implements the various standard interfaces that operating systems need from the
secure firmware
▶ Has drivers for the hardware blocks that are not accessed directly by Linux
▶ Needs to be ported for each SoC
▶ Depending on the platform, may also need to be ported per board: DDR
initialization
▶ Used on the vast majority of ARMv8 platforms, and on a few recent ARMv7
platforms
▶ https://www.trustedfirmware.org/projects/tf-a/
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 114/531
Trusted OS, OP-TEE
▶ A trusted operating system can run in the secure world, also called Trusted
Execution Environment or TEE
▶ Hardware partitioning between secure world and normal world
• Some hardware resources only available in the secure world, by the trusted OS
▶ Allows to run trusted applications/services
• isolated from Linux
• can provide services to Linux applications
▶ Most common open-source implementation: OP-TEE
• Supported by most silicon vendors
• https://www.op-tee.org/
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 115/531
ARM: summary
Largely inspired from Ahmad Fatoum presentation From Reset Vector to Kernel, slides, video
See also details about the ARM terms: BL1, BL2...
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 116/531
RISC-V
▶ Linux-class RISC-V processors have several privilege
levels
• M-mode: machine mode
• S-mode: level at which the Linux kernel runs
• U-mode: level at which Linux user-space applications
run
▶ Some specific HW resources are not accessible in
S-mode
▶ A more priviledged firmware runs in M-mode
▶ RISC-V has defined SBI, Supervisor Binary Interface
• Standardized interface between the OS and the firmware
• https://github.com/riscv-non-isa/riscv-sbi-doc
▶ OpenSBI is a reference, open-source implementation of
SBI
• https://github.com/riscv-software-src/opensbi
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 117/531
Bootloaders and firmware
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 118/531
TI AM335x (32 bit BeagleBone): ARMv7
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 119/531
NXP i.MX6: ARMv7
Note: this diagram shows one possible boot flow on NXP i.MX6, but it is also possible
to use the U-Boot SPL → U-Boot boot flow on i.MX6.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 120/531
STM32MP1: ARMv7
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 122/531
TI AM62x (BeaglePlay): ARMv7 and ARMv8 cores
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 124/531
U-Boot
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 125/531
Where to get U-Boot from?
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 126/531
U-Boot configuration
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 127/531
U-Boot configuration file: stm32mp15_trusted_defconfig
CONFIG_ARM=y
CONFIG_ARCH_STM32MP=y
CONFIG_TFABOOT=y
CONFIG_SYS_MALLOC_F_LEN=0x3000
CONFIG_ENV_OFFSET=0x280000
CONFIG_ENV_SECT_SIZE=0x40000
CONFIG_DEFAULT_DEVICE_TREE="stm32mp157c-ev1"
[..]
CONFIG_CMD_ADTIMG=y
CONFIG_CMD_ERASEENV=y
CONFIG_CMD_NVEDIT_EFI=y
CONFIG_CMD_MEMINFO=y
CONFIG_CMD_MEMTEST=y
CONFIG_CMD_UNZIP=y
CONFIG_CMD_ADC=y
CONFIG_CMD_CLK=y
CONFIG_CMD_DFU=y
CONFIG_CMD_FUSE=y
CONFIG_CMD_GPIO=y
[...]
CONFIG_SPI=y
CONFIG_DM_SPI=y
CONFIG_STM32_QSPI=y
CONFIG_STM32_SPI=y
[...]
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 129/531
U-Boot compilation
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 130/531
Concept of U-Boot SPL
▶ To meet the need of a two-stage boot process, U-Boot has the concept of U-Boot
SPL
▶ SPL = Secondary Program Loader
▶ The SPL is a stripped-down version of U-Boot, made small enough to meet the
size constraints of a first stage bootloader
▶ Configured through menuconfig, one can define the subset of drivers to include
▶ No U-Boot shell/commands: the behavior is hardcoded in C code
▶ For some platforms: TPL, Tertiary Program Loader, an even more minimal first
stage bootloader to do TPL → SPL → main U-Boot.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 131/531
Device Tree in U-Boot
▶ The Device Tree is a data structure that describes the topology of the hardware
▶ Allows software to know which hardware peripherals are available and how they
are connected to the system
▶ Initially mainly used by Linux, now also used by U-Boot, Barebox, TF-A, etc.
▶ Used by U-Boot on most platforms.
▶ Device Tree files located in arch/ARCH/dts
▶ One .dts for each board: need to create one if you build a custom board
▶ U-Boot defconfigs usually specify a default Device Tree, but it can be overridden
using the DEVICE_TREE variable
▶ More details on the Device Tree later in this course.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 132/531
U-Boot build example: TI AM335x BeagleBoneBlack wireless
▶ One defconfig file suitable for all AM335x platforms:
configs/am335x_evm_defconfig
• Yes its name looks like it supports only the EVM (EValuation Module) board
• Contains CONFIG_DEFAULT_DEVICE_TREE="am335x-evm" → uses
arch/arm/dts/am335x-evm.dts by default
▶ One Device Tree file describing the BeagleBoneBlack Wireless:
arch/arm/dts/am335x-boneblack-wireless.dts
▶ Configure and build U-Boot
$ export PATH=/path/to/toolchain/bin:$PATH
$ make am335x_evm_defconfig
$ make DEVICE_TREE=am335x-boneblack-wireless CROSS_COMPILE=arm-linux-
▶ Produces:
• MLO, the SPL, first-stage bootloader. Called MLO (Mmc LOad) as required on TI
platforms.
• u-boot.img, full U-Boot, second-stage bootloader
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 133/531
Installing U-Boot
1. If U-Boot is loaded from external storage, just update the binaries on such storage.
2. If U-Boot is loaded from internal storage (eMMC or NAND), you can update it
using Snagboot (https://github.com/bootlin/snagboot) if it supports your
SoC, or with the custom solution from the SoC vendor.
3. An alternative is to reflash internal storage with JTAG (if available), but that’s
more complicated and requires a JTAG probe.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 134/531
U-boot shell prompt
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 135/531
U-Boot help command
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 136/531
U-Boot information commands
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 137/531
Concept of U-Boot environment
▶ A significant part of the U-Boot configuration happens at compile time:
menuconfig
▶ U-Boot also has runtime configuration, through the concept of environment
variables
▶ Environment variables are key/value pairs
• Some specific environment variables impact the behavior of different U-Boot
commands
• Additional custom environment variables can be added, and used in scripts
▶ U-Boot environment variables are loaded and modified in RAM
▶ U-Boot has a default environment built into its binary
• used when no other environment is found
• defined in the configuration
• the default environment is sometimes quite complex in some existing configurations
▶ The environment can be persistently stored in non-volatile storage
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 138/531
U-Boot environment persistent storage
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 139/531
U-Boot environment commands
▶ printenv
Shows all variables
▶ printenv <variable-name>
Shows the value of a variable
▶ setenv <variable-name> <variable-value>
Changes the value of a variable or defines a new one, only in RAM
▶ editenv <variable-name>
Interactively edits the value of a variable, only in RAM
▶ After an editenv or setenv, changes in the environment are lost if they are not
saved persistently
▶ saveenv
Saves the current state of the environment to storage for persistence.
▶ env command, with many sub-commands: env default, env info, env erase,
env set, env save, etc.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 140/531
U-Boot environment commands example
=> printenv
baudrate=19200
ethaddr=00:40:95:36:35:33
netmask=255.255.255.0
ipaddr=10.0.0.11
serverip=10.0.0.1
stdin=serial
stdout=serial
stderr=serial
=> setenv serverip 10.0.0.100
=> printenv serverip
serverip=10.0.0.100
=> saveenv
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 141/531
U-Boot memory allocation
▶ Many commands in U-Boot loading data into memory, or using data from
memory, expect a RAM address as argument
▶ No built-in memory allocation mechanism → up to the user to know usable
memory areas to load/use data
▶ Use the output of bdinfo to know the start address and size of RAM
▶ Avoid the end of the RAM, which is used by the U-Boot code and dynamic
memory allocations
▶ Not the best part of the U-Boot design, sadly
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 142/531
U-Boot memory manipulation commands
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 143/531
U-Boot raw storage commands
U-Boot can manipulate raw storage devices:
▶ NAND flash
• nand info
• nand read <addr> <off|partition> ▶ USB storage
<size> • usb info
• nand erase [<off> [<size>]] • usb read <addr> <blk#>
• nand write <addr> <off|partition> <cnt>
<size> • usb write <addr> <blk#>
• More: help nand <cnt>
▶ MMC • usb part
• • usb dev
mmc info
• • More: help usb
mmc read <addr> <blk#> <cnt>
• mmc write <addr> <blk#> <cnt> Note: <addr> are addresses in
• mmc part to show partition table RAM where data is stored
• mmc dev to show/set current MMC device
• More: help mmc
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 144/531
U-Boot commands example
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 145/531
U-Boot filesystem storage commands
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 146/531
U-Boot filesystem command example
List files
STM32MP> ls mmc 0:4
<DIR> 1024 . Load file
<DIR> 1024 ..
STM32MP> load mmc 0:4 c0000000 /etc/fstab
<DIR> 12288 lost+found
334 bytes read in 143 ms (2 KiB/s)
<DIR> 2048 bin
<DIR> 1024 boot
<DIR> 1024 dev
<DIR> 1024 etc Show file contents
[...]
STM32MP> md c0000000
c0000000: 663c2023 20656c69 74737973 093e6d65 # <file system>.
STM32MP> ls mmc 0:4 /etc
c0000010: 756f6d3c 7020746e 3c093e74 65707974 <mount pt>.<type
<DIR> 1024 .
c0000020: 6f3c093e 6f697470 093e736e 6d75643c >.<options>.<dum
<DIR> 1024 ..
c0000030: 3c093e70 73736170 642f0a3e 722f7665 p>.<pass>./dev/r
209 asound.conf
c0000040: 09746f6f 6509092f 09327478 6e2c7772 oot./..ext2.rw,n
<DIR> 1024 fonts
[...]
334 fstab
347 group
[...]
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 147/531
U-Boot networking
▶ Environment variables
• ethaddr: MAC address
• ipaddr: IP address of the board
• serverip: IP address of the server for network related commands
▶ Important commands
• ping: ping a destination machine. Note: U-Boot is not an operating system with
multitasking/interrupts, so ping from another machine to U-Boot cannot work.
• tftp: load a file using the TFTP protocol
• dhcp: get an IP address using DHCP
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 148/531
TFTP
▶ Network transfer from the development workstation to U-Boot on the target takes
place through TFTP
• Trivial File Transfer Protocol
• Somewhat similar to FTP, but without authentication and over UDP
▶ A TFTP server is needed on the development workstation
• sudo apt install tftpd-hpa
• All files in /srv/tftp are then visible through TFTP
• A TFTP client is available in the tftp-hpa package, for testing
▶ A TFTP client is integrated into U-Boot
• Configure the ipaddr, serverip, and ethaddr environment variables
• Use tftp <address> <filename> to load file contents to the specified RAM
address
• Example: tftp 0x21000000 zImage
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 149/531
Scripts in environment variables
▶ Environment variables can contain small scripts, to execute several commands and
test the results of commands.
• Useful to automate booting or upgrade processes
• Several commands can be chained using the ; operator
• Tests can be done using if command ; then ... ; else ... ; fi
• Scripts are executed using run <variable-name>
• You can reference other variables using ${variable-name}
▶ Examples
• setenv bootcmd 'tftp 0x21000000 zImage; tftp 0x22000000 dtb; bootz
0x21000000 - 0x22000000'
• setenv mmc-boot 'if fatload mmc 0 80000000 boot.ini; then source; else
if fatload mmc 0 80000000 zImage; then run mmc-do-boot; fi; fi'
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 150/531
U-Boot booting commands
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 151/531
U-Boot booting example
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 152/531
FIT image
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 153/531
Generic Distro boot (1)
▶ Each board/platform used to have its own U-Boot environment, with custom
variables/commands
▶ Wish to standardize the behavior of bootloaders, including U-Boot
▶ Generic Distro boot concept
▶ If enabled, at boot time, U-Boot:
• Can be asked to locate a bootable partition (part list command), as defined by
the bootable flag of the partition table
• With the sysboot command, will look for a /extlinux/extlinux.conf or
/boot/extlinux/extlinux.conf file describing how to boot, and will offer a
prompt in the console to choose between available configurations.
• Once a configuration is selected, will load and boot the corresponding kernel, device
tree and initramfs images.
• Example bootcmd:
part list mmc 0 -bootable bootpart; sysboot mmc 0:${bootpart} any
▶ https://u-boot.readthedocs.io/en/latest/develop/distro.html
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 154/531
Generic Distro boot (2)
Example /boot/extlinux/extlinux.conf
Several environment variables need to be set: label stm32mp157c-dk2-buildroot
kernel /boot/zImage
▶ kernel_addr_r: address in RAM to load devicetree /boot/stm32mp157c-dk2.dtb
append root=/dev/mmcblk0p4 rootwait
the kernel image
▶ ramdisk_addr_r: address in RAM to load U-Boot boot log
the initramfs image (if any) Hit any key to stop autoboot: 0
Boot over mmc0!
switch to partitions #0, OK
▶ fdt_addr_r: address in RAM to load the mmc0 is current device
Scanning mmc 0:4...
DTB (Flattened Device Tree) Found /boot/extlinux/extlinux.conf
Retrieving file: /boot/extlinux/extlinux.conf
▶ pxefile_addr_r: address in RAM to load 131 bytes read in 143 ms (0 Bytes/s)
1: stm32mp157c-dk2-buildroot
the configuration file (usually Retrieving file: /boot/zImage
7538376 bytes read in 462 ms (15.6 MiB/s)
extlinux.conf) append: root=/dev/mmcblk0p4 rootwait
Retrieving file: /boot/stm32mp157c-dk2.dtb
▶ bootfile: the path to the configuration 117969 bytes read in 148 ms (778.3 KiB/s)
Kernel image @ 0xc2000000 [ 0x000000 - 0x7306c8 ]
file, for example ## Flattened Device Tree blob at c4000000
Booting using the fdt blob at 0xc4000000
/boot/extlinux/extlinux.conf Loading Device Tree to cffe0000, end cffffcd0 ... OK
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 156/531
Concept of FIP
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 157/531
Configuring TF-A
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 158/531
Configure TF-A: important variables
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 159/531
Building TF-A for STM32MP1
$ make CROSS_COMPILE=arm-linux- \
ARM_ARCH_MAJOR=7 \
ARCH=aarch32 \
PLAT=stm32mp1 \
AARCH32_SP=sp_min \
DTB_FILE_NAME=stm32mp157a-dk1.dtb \
BL33=/path/to/u-boot/u-boot-nodtb.bin \
BL33_CFG=/path/to/u-boot/u-boot.dtb \
STM32MP_SDMMC=1 \
fip all
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 160/531
FIP image contents
fiptool info
$ ./tools/fiptool/fiptool info build/stm32mp1/release/fip.bin
Secure Payload BL32 (Trusted OS): offset=0x100, size=0x8AEC, cmdline="--tos-fw"
Non-Trusted Firmware BL33: offset=0x8BEC, size=0xECE6C, cmdline="--nt-fw"
FW_CONFIG: offset=0xF5A58, size=0x226, cmdline="--fw-config"
HW_CONFIG: offset=0xF5C7E, size=0x16A98, cmdline="--hw-config"
TOS_FW_CONFIG: offset=0x10C716, size=0x3CF6, cmdline="--tos-fw-config"
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 161/531
STM32MP1 partition layout
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 162/531
AM62x (BeaglePlay) partition layout
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 163/531
Practical lab - U-Boot / TF-A
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 164/531
Linux kernel introduction
Linux kernel
introduction
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 165/531
Linux kernel in the system
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 166/531
Linux kernel main roles
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 167/531
System calls
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 168/531
Pseudo filesystems
▶ Linux makes system and kernel information available in user space through
pseudo filesystems, sometimes also called virtual filesystems
▶ Pseudo filesystems allow applications to see directories and files that do not exist
on any real storage: they are created and updated on the fly by the kernel
▶ The two most important pseudo filesystems are
• proc, usually mounted on /proc:
Operating system related information (processes, memory management
parameters...)
• sysfs, usually mounted on /sys:
Representation of the system as a tree of devices connected by buses. Information
gathered by the kernel frameworks managing these devices.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 169/531
Linux kernel introduction
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 170/531
Linux versioning scheme
▶ Until 2003, there was a new “stabilized” release branch of Linux every 2 or 3 years
(2.0, 2.2, 2.4). Development branches took 2-3 years to be merged (too slow!).
▶ Since 2003, there is a new official release of Linux about every 10 weeks:
• Versions 2.6 (Dec. 2003) to 2.6.39 (May 2011)
• Versions 3.0 (Jul. 2011) to 3.19 (Feb. 2015)
• Versions 4.0 (Apr. 2015) to 4.20 (Dec. 2018)
• Versions 5.0 (Mar. 2019) to 5.19 (July 2022)
• Version 6.0 was released in Oct. 2022.
▶ Features are added to the kernel in a progressive way. Since 2003, kernel
developers have managed to do so without having to introduce a massively
incompatible development branch.
▶ For each release, there are bugfix and security updates called stable releases:
6.0.1, 6.0.2, etc.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 171/531
Linux development model
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 172/531
Need for long term support (1)
▶ Issue: bug and security fixes only released for most recent kernel versions.
▶ Solution: the last release of each year is made an LTS (Long Term Support)
release, and is supposed to be supported (and receive bug and security fixes) for
at least 2 years.
▶ Example at Google: starting from Android O (2017), all new Android devices have
to run such an LTS kernel.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 173/531
Need for long term support (2)
▶ You could also get long term support from a commercial embedded Linux
provider.
• Wind River Linux can be supported for up to 15 years.
• Ubuntu Core can be supported for up to 10 years.
▶ ”If you are not using a supported distribution kernel, or a stable / longterm kernel,
you have an insecure kernel” - Greg KH, 2019
Some vulnerabilities are fixed in stable without ever getting a CVE.
▶ The Civil Infrastructure Platform project is an industry / Linux Foundation effort
to support much longer (at least 10 years) selected LTS versions (currently 4.4,
4.19, 5.10 and 6.1) on selected architectures. See
https://wiki.linuxfoundation.org/civilinfrastructureplatform/start.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 174/531
Linux kernel introduction
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 175/531
Location of official kernel sources
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 176/531
Location of non-official kernel sources
▶ Many chip vendors supply their own kernel sources
• Focusing on hardware support first
• Can have a very important delta with mainline Linux
• Sometimes they break support for other platforms/devices without caring
• Useful in early phases only when mainline hasn’t caught up yet (many vendors invest
in the mainline kernel at the same time)
• Suitable for PoC, not suitable for products on the long term as usually no updates
are provided to these kernels
• Getting stuck with a deprecated system with broken software that cannot be
updated has a real cost in the end
▶ Many kernel sub-communities maintain their own kernel, with usually newer but
fewer stable features, only for cutting-edge development
• Architecture communities (ARM, MIPS, PowerPC, etc)
• Device drivers communities (I2C, SPI, USB, PCI, network, etc)
• Other communities (real-time, etc)
• Not suitable to be used in products
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 177/531
Getting Linux sources
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 178/531
Going through Linux sources
Identifier
search
Source
browsing
All versions
available
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 179/531
Linux kernel size and structure
▶ Linux v5.18 sources: close to 80k files, 35M lines, 1.3GiB
▶ But a compressed Linux kernel just sizes a few megabytes.
▶ So, why are these sources so big?
Because they include numerous device drivers, network protocols, architectures,
filesystems... The core is pretty small!
▶ As of kernel version v5.18 (in percentage of total number of lines):
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 181/531
Linux kernel introduction
Kernel configuration
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 182/531
Kernel configuration
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 183/531
Kernel configuration and build system
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 184/531
Specifying the target architecture
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 185/531
Choosing a compiler
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 186/531
Specifying ARCH and CROSS_COMPILE
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 187/531
Initial configuration
Difficult to find which kernel configuration will work with your hardware and root
filesystem. Start with one that works!
▶ Desktop or server case:
• Advisable to start with the configuration of your running kernel:
cp /boot/config-`uname -r` .config
▶ Embedded platform case:
• Default configurations stored in-tree as minimal configuration files (only listing
settings that are different with the defaults) in arch/<arch>/configs/
• make help will list the available configurations for your platform
• To load a default configuration file, just run make foo_defconfig (will erase your
current .config!)
On ARM 32-bit, there is usually one default configuration per CPU family
On ARM 64-bit, there is only one big default configuration to customize
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 188/531
Create your own default configuration
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 189/531
Built-in or module?
▶ The kernel image is a single file, resulting from the linking of all object files that
correspond to features enabled in the configuration
• This is the file that gets loaded in memory by the bootloader
• All built-in features are therefore available as soon as the kernel starts, at a time
where no filesystem exists
▶ Some features (device drivers, filesystems, etc.) can however be compiled as
modules
• These are plugins that can be loaded/unloaded dynamically to add/remove features
to the kernel
• Each module is stored as a separate file in the filesystem, and therefore access
to a filesystem is mandatory to use modules
• This is not possible in the early boot procedure of the kernel, because no filesystem
is available
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 190/531
Kernel option types
There are different types of options, defined in Kconfig files:
▶ bool options, they are either
• true (to include the feature in the kernel) or
• false (to exclude the feature from the kernel)
▶ tristate options, they are either
• true (to include the feature in the kernel image) or
• module (to include the feature as a kernel module) or
• false (to exclude the feature)
▶ int options, to specify integer values
▶ hex options, to specify hexadecimal values
Example: CONFIG_PAGE_OFFSET=0xC0000000
▶ string options, to specify string values
Example: CONFIG_LOCALVERSION=-no-network
Useful to distinguish between two kernels built from different options
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 191/531
Kernel option dependencies
Enabling a network driver requires the network stack to be enabled, therefore
configuration symbols have two ways to express dependencies:
▶ select dependency:
▶ depends on dependency: config A
config B select B
depends on A • When A is enabled, B is enabled too (and
• B is not visible until A is cannot be disabled manually)
enabled • Should preferably not select symbols with
• Works well for dependency depends on dependencies
chains • Used to declare hardware features or select
libraries
config SPI_ATH79
tristate "Atheros AR71XX/AR724X/AR913X SPI controller driver"
depends on ATH79 || COMPILE_TEST
select SPI_BITBANG
help
This enables support for the SPI controller present on the
Atheros AR71XX/AR724X/AR913X SoCs.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 192/531
Kernel configuration details
#
# CD-ROM/DVD Filesystems
#
CONFIG_ISO9660_FS=m
▶ The configuration is stored in the .config file at CONFIG_JOLIET=y
the root of kernel sources CONFIG_ZISOFS=y
• Simple text file, CONFIG_PARAM=value CONFIG_UDF_FS=y
• Options are grouped by sections and are prefixed # end of CD-ROM/DVD Filesystems
with CONFIG_
• Included by the top-level kernel Makefile #
• Typically not edited by hand because of the # DOS/FAT/EXFAT/NT Filesystems
#
dependencies CONFIG_FAT_FS=y
CONFIG_MSDOS_FS=y
# CONFIG_VFAT_FS is not set
CONFIG_FAT_DEFAULT_CODEPAGE=437
# CONFIG_EXFAT_FS is not set
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 193/531
xconfig
make xconfig
▶ A graphical interface to configure the
kernel.
▶ File browser: easy to load
configuration files
▶ Search interface to look for
parameters ([Ctrl] + [f])
▶ Required Debian/Ubuntu packages:
qtbase5-dev on Ubuntu 22.04
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 194/531
menuconfig
make menuconfig
▶ Useful when no graphics are available.
Very efficient interface.
▶ Same interface found in other tools:
BusyBox, Buildroot...
▶ Convenient number shortcuts to jump
directly to search results.
▶ Required Debian/Ubuntu packages:
libncurses-dev
▶ Alternative: make nconfig
(now also has the number shortcuts)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 195/531
Kernel configuration options
You can switch from one tool to another, they all load/save the same .config file,
and show the same set of options
Compiled as a module:
CONFIG_ISO9660_FS=m
Statically built:
CONFIG_UDF_FS=y
Values in resulting .config file Parameter values as displayed by xconfig Parameter values as displayed by menuconfig
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 196/531
make oldconfig
make oldconfig
▶ Useful to upgrade a .config file from an earlier kernel release
▶ Asks for values for new parameters.
▶ ... unlike make menuconfig and make xconfig which silently set default values
for new parameters.
If you edit a .config file by hand, it’s useful to run make oldconfig afterwards, to set
values to new parameters that could have appeared because of dependency changes.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 197/531
Undoing configuration changes
A frequent problem:
▶ After changing several kernel configuration settings, your kernel no longer works.
▶ If you don’t remember all the changes you made, you can get back to your
previous configuration:
$ cp .config.old .config
▶ All the configuration tools keep this .config.old backup copy.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 198/531
Linux kernel introduction
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 199/531
Kernel compilation
Example: make -j 8
▶ To recompile faster (7x according to some benchmarks),
use the ccache compiler cache:
Command: make -j8
export CROSS_COMPILE="ccache arm-linux-" Total time: 67 s
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 200/531
Kernel compilation results
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 201/531
Kernel installation: native case
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 202/531
Kernel installation: embedded case
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 203/531
Module installation: native case
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 204/531
Module installation: embedded case
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 205/531
Kernel cleanup targets
Cleaning targets:
clean - Remove most generated files but keep the config and
enough build support to build external modules
mrproper - Remove all generated files + config + various backup files
distclean - mrproper + remove editor backup and patch files
▶ If you are in a git tree, remove all files not tracked (and ignored) by git:
git clean -fdx
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 206/531
Kernel building overview
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 207/531
Linux kernel introduction
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 208/531
Hardware description
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 209/531
Customize your board device tree!
▶ Kernel developers write Device Tree Sources (DTS), which become Device Tree
Blobs (DTB) once compiled.
▶ There is one different Device Tree for each board/platform supported by the
kernel, available in arch/<arch>/boot/dts/<vendor>/<board>.dtb
(arch/arm/boot/dts/<board>.dtb on ARM 32 before Linux 6.5).
▶ As a board user, you may have legitimate needs to customize your board device
tree:
• To describe external devices attached to non-discoverable busses and configure them.
• To configure pin muxing: choosing what SoC signals are made available on the
board external connectors. See http://linux.tanzilli.com/ for a web service
doing this interactively.
• To configure some system parameters: flash partitions, kernel command line (other
ways exist)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 210/531
Booting with U-Boot
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 211/531
Kernel command line
▶ In addition to the compile time configuration, the kernel behavior can be adjusted
with no recompilation using the kernel command line
▶ The kernel command line is a string that defines various arguments to the kernel
• It is very important for system configuration
• root= for the root filesystem (covered later)
• console= for the destination of kernel messages
• Example: console=ttyS0 root=/dev/mmcblk0p2 rootwait
• Many more exist. The most important ones are documented in
admin-guide/kernel-parameters in kernel documentation.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 212/531
Passing the kernel command line
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 213/531
Kernel log
▶ The kernel keeps its messages in a circular buffer in memory
• The size is configurable using CONFIG_LOG_BUF_SHIFT
▶ When a module is loaded, related information is available in the kernel log.
▶ Kernel log messages are available through the dmesg command (diagnostic
message)
▶ Kernel log messages are also displayed on the console pointed by the console=
kernel command line argument
• Console messages can be filtered by level using the loglevel parameter:
loglevel= allows to filter messages displayed on the console based on priority
ignore_loglevel (same as loglevel=8) will lead to all messages being printed
quiet (same as loglevel=0) prevents any message from being displayed on the
console
• Example: console=ttyS0 root=/dev/mmcblk0p2 loglevel=5
▶ It is possible to write to the kernel log from user space:
echo "<n>Debug info" > /dev/kmsg
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 214/531
Practical lab - Kernel cross-compiling
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 215/531
Linux Root Filesystem
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 216/531
Linux Root Filesystem
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 217/531
Filesystems
▶ Filesystems are used to organize data in directories and files on storage devices or
on the network. The directories and files are organized as a hierarchy
▶ In UNIX systems, applications and users see a single global hierarchy of files and
directories, which can be composed of several filesystems.
▶ Filesystems are mounted in a specific location in this hierarchy of directories
• When a filesystem is mounted in a directory (called mount point), the contents of
this directory reflect the contents of this filesystem.
• When the filesystem is unmounted, the mount point is empty again.
▶ This allows applications to access files and directories easily, regardless of their
exact storage location
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 218/531
Filesystems (2)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 219/531
mount / umount
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 220/531
Root filesystem
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 221/531
Location of the root filesystem
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 222/531
Mounting rootfs from storage devices
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 223/531
Mounting rootfs over the network (1)
Once networking works, your root filesystem could be a directory on your GNU/Linux
development host, exported by NFS (Network File System). This is very convenient for
system development:
▶ Makes it very easy to update files on the root filesystem, without rebooting.
▶ Can have a big root filesystem even if you don’t have support for internal or
external storage yet.
▶ The root filesystem can be huge. You can even build native compiler tools and
build all the tools you need on the target itself (better to cross-compile though).
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 224/531
Mounting rootfs over the network (2)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 225/531
Mounting rootfs over the network (3)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 226/531
Mounting rootfs over the network (4)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 227/531
Root filesystem in memory: initramfs
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 228/531
External initramfs
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 229/531
Built-in initramfs
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 230/531
Linux Root Filesystem
Contents
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 231/531
Root filesystem organization
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 232/531
Important directories (1)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 233/531
Important directories (2)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 234/531
Separation of programs and libraries
▶ Basic programs are installed in /bin and /sbin and basic libraries in /lib
▶ All other programs are installed in /usr/bin and /usr/sbin and all other libraries in
/usr/lib
▶ In the past, on UNIX systems, /usr was very often mounted over the network, through
NFS
▶ In order to allow the system to boot when the network was down, some binaries and
libraries are stored in /bin, /sbin and /lib
▶ /bin and /sbin contain programs like ls, ip, cp, bash, etc.
▶ /lib contains the C library and sometimes a few other basic libraries
▶ All other programs and libraries are in /usr
▶ Update: distributions are now making /bin link to /usr/bin, /lib to /usr/lib and
/sbin to /usr/sbin. Details on
https://www.freedesktop.org/wiki/Software/systemd/TheCaseForTheUsrMerge/.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 235/531
Linux Root Filesystem
Pseudo Filesystems
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 236/531
proc virtual filesystem
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 237/531
proc contents
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 238/531
sysfs filesystem
▶ It allows to represent in user space the vision that the kernel has of the buses,
devices and drivers in the system
▶ It is useful for various user space applications that need to list and query the
available hardware, for example udev or mdev (see later)
▶ All applications using sysfs expect it to be mounted in the /sys directory
▶ Command to mount /sys:
mount -t sysfs nodev /sys
▶ $ ls /sys/
block bus class dev devices firmware
fs kernel module power
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 239/531
Linux Root Filesystem
Minimal filesystem
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 240/531
Basic applications
▶ In order to work, a Linux system needs at least a few applications
▶ An init application, which is the first user space application started by the kernel after
mounting the root filesystem (see https://en.wikipedia.org/wiki/Init):
• The kernel tries to run the command specified by the init= command line
parameter if available.
• Otherwise, it tries to run /sbin/init, /etc/init, /bin/init and /bin/sh.
• In the case of an initramfs, it will only look for /init. Another path can be supplied
by the rdinit= kernel argument.
• If none of this works, the kernel panics and the boot process is stopped.
• The init application is responsible for starting all other user space applications and
services, and for acting as a universal parent for processes whose parent terminate
before they do.
▶ A shell, to implement scripts, automate tasks, and allow a user to interact with the system
▶ Basic UNIX executables, for use in system scripts or in interactive shells: mv, cp, mkdir,
cat, modprobe, mount, ip, etc.
▶ These basic components have to be integrated into the root filesystem to make it usable
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 241/531
Overall booting process
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 242/531
Overall booting process with initramfs
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 243/531
BusyBox
BusyBox
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 244/531
Why BusyBox?
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 245/531
General purpose toolbox: BusyBox
https://www.busybox.net/
▶ Rewrite of many useful UNIX command line utilities
• Created in 1995 to implement a rescue and installer system
for Debian, fitting in a single floppy disk (1.44 MB)
• Integrated into a single project, which makes it easy to work
with
• Great for embedded systems: highly configurable, no
unnecessary features
• Called the Swiss Army Knife of Embedded Linux
▶ License: GNU GPLv2
▶ Alternative: Toybox, BSD licensed
(https://en.wikipedia.org/wiki/Toybox)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 246/531
BusyBox in the root filesystem
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 247/531
BusyBox - Most commands in one binary
[, [[, acpid, add-shell, addgroup, adduser, adjtimex, arch, arp, arping, ash, awk, base64, basename, bc, beep, blkdiscard, blkid,
blockdev, bootchartd, brctl, bunzip2, bzcat, bzip2, cal, cat, chat, chattr, chgrp, chmod, chown, chpasswd, chpst, chroot, chrt,
chvt, cksum, clear, cmp, comm, conspy, cp, cpio, crond, crontab, cryptpw, cttyhack, cut, date, dc, dd, deallocvt, delgroup,
deluser, depmod, devmem, df, dhcprelay, diff, dirname, dmesg, dnsd, dnsdomainname, dos2unix, dpkg, dpkg-deb, du, dumpkmap,
dumpleases, echo, ed, egrep, eject, env, envdir, envuidgid, ether-wake, expand, expr, factor, fakeidentd, fallocate, false,
fatattr, fbset, fbsplash, fdflush, fdformat, fdisk, fgconsole, fgrep, find, findfs, flock, fold, free, freeramdisk, fsck,
fsck.minix, fsfreeze, fstrim, fsync, ftpd, ftpget, ftpput, fuser, getopt, getty, grep, groups, gunzip, gzip, halt, hd, hdparm,
head, hexdump, hexedit, hostid, hostname, httpd, hush, hwclock, i2cdetect, i2cdump, i2cget, i2cset, i2ctransfer, id, ifconfig,
ifdown, ifenslave, ifplugd, ifup, inetd, init, insmod, install, ionice, iostat, ip, ipaddr, ipcalc, ipcrm, ipcs, iplink, ipneigh,
iproute, iprule, iptunnel, kbd_mode, kill, killall, killall5, klogd, last, less, link, linux32, linux64, linuxrc, ln, loadfont,
loadkmap, logger, login, logname, logread, losetup, lpd, lpq, lpr, ls, lsattr, lsmod, lsof, lspci, lsscsi, lsusb, lzcat, lzma,
lzop, makedevs, makemime, man, md5sum, mdev, mesg, microcom, mim, mkdir, mkdosfs, mke2fs, mkfifo, mkfs.ext2, mkfs.minix, mkfs.vfat,
mknod, mkpasswd, mkswap, mktemp, modinfo, modprobe, more, mount, mountpoint, mpstat, mt, mv, nameif, nanddump, nandwrite,
nbd-client, nc, netstat, nice, nl, nmeter, nohup, nologin, nproc, nsenter, nslookup, ntpd, nuke, od, openvt, partprobe, passwd,
paste, patch, pgrep, pidof, ping, ping6, pipe_progress, pivot_root, pkill, pmap, popmaildir, poweroff, powertop, printenv, printf,
ps, pscan, pstree, pwd, pwdx, raidautorun, rdate, rdev, readahead, readlink, readprofile, realpath, reboot, reformime,
remove-shell, renice, reset, resize, resume, rev, rm, rmdir, rmmod, route, rpm, rpm2cpio, rtcwake, run-init, run-parts, runlevel,
runsv, runsvdir, rx, script, scriptreplay, sed, sendmail, seq, setarch, setconsole, setfattr, setfont, setkeycodes, setlogcons,
setpriv, setserial, setsid, setuidgid, sh, sha1sum, sha256sum, sha3sum, sha512sum, showkey, shred, shuf, slattach, sleep, smemcap,
softlimit, sort, split, ssl_client, start-stop-daemon, stat, strings, stty, su, sulogin, sum, sv, svc, svlogd, svok, swapoff,
swapon, switch_root, sync, sysctl, syslogd, tac, tail, tar, taskset, tc, tcpsvd, tee, telnet, telnetd, test, tftp, tftpd, time,
timeout, top, touch, tr, traceroute, traceroute6, true, truncate, ts, tty, ttysize, tunctl, ubiattach, ubidetach, ubimkvol,
ubirename, ubirmvol, ubirsvol, ubiupdatevol, udhcpc, udhcpc6, udhcpd, udpsvd, uevent, umount, uname, unexpand, uniq, unix2dos,
unlink, unlzma, unshare, unxz, unzip, uptime, users, usleep, uudecode, uuencode, vconfig, vi, vlock, volname, w, wall, watch,
watchdog, wc, wget, which, who, whoami, whois, xargs, xxd, xz, xzcat, yes, zcat, zcip
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 249/531
BusyBox make menuconfig
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 250/531
Compiling BusyBox
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 251/531
Applet highlight: BusyBox init
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 252/531
Applet highlight - BusyBox vi
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 253/531
Practical lab - Tiny root filesystem built from scratch with BusyBox
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 254/531
Accessing hardware devices
Accessing hardware
devices
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 255/531
Accessing hardware devices
Kernel drivers
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 256/531
Typical software stack for hardware access
From the bottom to the top:
▶ A bus controller driver in the kernel drives an
I2C, SPI, USB, PCI controller
▶ A bus subsystem provides an API for drivers to
access a particular type of bus: I2C, SPI, PCI,
USB, etc.
▶ A device driver in the kernel drives a particular
device connected to a given bus
▶ A driver subsystem exposes features of certain
class of devices, through a standard
kernel/user-space interface
▶ An application can access the device through
this standard kernel/user-space interface either
directly or through a library.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 257/531
Stack illustrated with a GPIO expander
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 258/531
Standardized user-space interface
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 259/531
Numerous kernel subsystems for device classes
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 260/531
Accessing devices directly from user-space
▶ Even though device drivers in the kernel are preferred, it is also possible to access
devices directly from user-space
▶ Especially useful for very specific devices that do not fit in any existing kernel
subsystems
▶ The kernel provides the following mechanisms, depending on the bus:
• I2C: i2c-dev
• SPI: spidev
• Memory-mapped: UIO
• USB: /dev/bus/usb, through libusb
• PCI: sysfs entries for PCI
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 261/531
Accessing devices directly from user-space: GPIO example
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 262/531
What can go wrong with a user-space driver?
▶ You write your GPIO driver in user-space: other kernel drivers cannot use GPIOs
from this GPIO controller
• Other devices that use GPIO signals from this controller for reset, interrupt, etc.
cannot control/configure those signals
▶ You write your touchscreen driver in user-space: the standard Linux graphics stack
components cannot use your touchscreen
▶ You write your network driver in user-space
• You can probably send/receive packets
• But you cannot leverage the Linux kernel networking stack for IP, TCP, UDP, etc.
• And none of the Linux networking applications can use your network device
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 263/531
Upstream drivers vs. out-of-tree drivers
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 264/531
Finding Linux kernel drivers
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 265/531
Finding Linux kernel drivers: an example
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 266/531
Accessing hardware devices
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 267/531
User-space interfaces for hardware devices
For a high-level perspective: three main interfaces to access hardware devices exposed
by the Linux kernel
▶ Device nodes in /dev
▶ Entries in the sysfs filesystem
▶ Network sockets and related APIs
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 268/531
Devices in /dev/
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 269/531
Block vs. character devices
▶ Block devices
• A device composed of fixed-sized blocks, that can be read and written to store data
• Used for hard disks, USB keys, SD cards, etc.
▶ Character devices
• Originally, an infinite stream of bytes, with no beginning, no end, no size. The pure
example: a serial port.
• Used for serial ports, terminals, but also sound cards, video acquisition devices,
frame buffers
• Most of the devices that are not block devices are represented as character devices
by the Linux kernel
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 270/531
Devices: everything is a file
▶ A very important UNIX design decision was to represent most system objects as
files
▶ It allows applications to manipulate all system objects with the normal file API
(open, read, write, close, etc.)
▶ So, devices had to be represented as files to the applications
▶ This is done through a special artifact called a device file
▶ It is a special type of file, that associates a file name visible to user space
applications to the triplet (type, major, minor) that the kernel understands
▶ All device files are by convention stored in the /dev directory
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 271/531
Device files examples
Example of device files in a Linux system
$ ls -l /dev/ttyS0 /dev/tty1 /dev/sda /dev/sda1 /dev/sda2 /dev/sdc1 /dev/zero
brw-rw---- 1 root disk 8, 0 2011-05-27 08:56 /dev/sda
brw-rw---- 1 root disk 8, 1 2011-05-27 08:56 /dev/sda1
brw-rw---- 1 root disk 8, 2 2011-05-27 08:56 /dev/sda2
brw-rw---- 1 root disk 8, 32 2011-05-27 08:56 /dev/sdc
crw------- 1 root root 4, 1 2011-05-27 08:57 /dev/tty1
crw-rw---- 1 root dialout 4, 64 2011-05-27 08:56 /dev/ttyS0
crw-rw-rw- 1 root root 1, 5 2011-05-27 08:56 /dev/zero
Example C code that uses the usual file API to write data to a serial port
int fd;
fd = open("/dev/ttyS0", O_RDWR);
write(fd, "Hello", 5);
close(fd);
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 272/531
Creating device files
▶ Before Linux 2.6.32, on basic Linux systems, the device files had to be created
manually using the mknod command
• mknod /dev/<device> [c|b] major minor
• Needs root privileges
• Coherency between device files and devices handled by the kernel was left to the
system developer
▶ The devtmpfs virtual filesystem can be mounted on /dev → the kernel
automatically creates/removes device files
• CONFIG_DEVTMPFS_MOUNT → asks the kernel to mount devtmpfs automatically at
boot time (except when booting on an initramfs).
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 273/531
Better handling of device files: udev and mdev
▶ devtmpfs is great, but its capabilities are limited, so complementary solutions exist
▶ udev
• daemon that receives events from the kernel about devices appearing/disappearing
• can create/remove device files (but that’s done by devtmpfs now), adjust
permission/ownership, load kernel modules automatically, create symbolic links to
devices
• according to rules files in /lib/udev/rules.d and /etc/udev/rules.d
• used in almost all desktop Linux distributions
• https://en.wikipedia.org/wiki/Udev
▶ mdev
• lightweight implementation of udev, part of Busybox
• https://wiki.gentoo.org/wiki/Mdev
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 274/531
Examples of user-space interfaces in /dev
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 275/531
sysfs filesystem
▶ block/, symlinks to all block devices, in /sys/devices
▶ bus/, one sub-folder by type of bus
▶ class/, one sub-folder per class (category of devices): input, leds, pwm, etc.
▶ dev/
• block/, one symlink per block device, named after major/minor
• char/, one symlink per character device, named after major/minor
▶ devices/, all devices in the system, organized in a slightly chaotic way, see this
article
▶ firmware/, representation of firmware data
• devicetree/, directory and file representation of Device Tree nodes and properties
▶ fs/, properties related to filesystem drivers
▶ kernel/, properties related to various kernel subsystems
▶ module/, properties about kernel modules
▶ power/, power-management related properties
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 276/531
sysfs filesystem example
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 277/531
sysfs filesystem example
/sys/bus/i2c/devices/0-002a/
lrwxrwxrwx driver -> ../../../../../../bus/i2c/drivers/edt_ft5x06
-rw-r--r-- gain
drwxr-xr-x input
-r--r--r-- modalias
-r--r--r-- name
lrwxrwxrwx of_node -> ../../../../../../firmware/devicetree/base/soc/i2c@40012000/touchscreen@2a
-rw-r--r-- offset
-rw-r--r-- offset_x
-rw-r--r-- offset_y
drwxr-xr-x power
-rw-r--r-- report_rate
lrwxrwxrwx subsystem -> ../../../../../../bus/i2c
-rw-r--r-- threshold
-rw-r--r-- uevent
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 278/531
Example of driver interfaces in sysfs
▶ All devices are visible in sysfs, whether they have an interface in /dev or not
• Usually /dev is to access the device
• /sys is more about properties of the devices
▶ However, some devices only have a sysfs interface
• LED: /sys/class/leds, see documentation
• PWM: /sys/class/pwm, see documentation
• IIO: /sys/class/iio, see documentation
• etc.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 279/531
Accessing GPIOs
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 280/531
Other virtual filesystems
▶ debugfs
• Conventionally mounted in /sys/kernel/debug
• Contains lots of debug information from the kernel, including device related
• /sys/kernel/debug/pinctrl for pin-mux debugging, /sys/kernel/debug/gpio for
GPIO debugging, /sys/kernel/debug/pwm for PWM debugging, etc.
• https://www.kernel.org/doc/html/latest/filesystems/debugfs.html
▶ configfs
• Conventionally mounted in /sys/kernel/config
• Allows to manage configuration of advanced kernel mechanisms
• Example: configuration of USB gadget functionalities
• Documentation/filesystems/configfs/configfs.txt
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 281/531
Accessing hardware devices
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 282/531
Why kernel modules?
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 283/531
Module installation and metadata
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 284/531
Module dependencies: modules.dep
▶ Some kernel modules can depend on other modules, based on the symbols
(functions and data structures) that they use.
▶ Example: the ubifs module depends on the ubi and mtd modules.
• mtd and ubi need to be loaded before ubifs
▶ These dependencies are described both in
/lib/modules/<kernel-version>/modules.dep and in
/lib/modules/<kernel-version>/modules.dep.bin
▶ Will be used by module loading tools.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 285/531
Module alias: modules.alias
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 286/531
Module utilities: modinfo
# modinfo usb_storage
filename: /lib/modules/5.18.13-200.fc36.x86_64/kernel/drivers/usb/storage/usb-storage.ko.xz
license: GPL
description: USB Mass Storage driver for Linux
author: Matthew Dharm <[email protected]>
alias: usb:v*p*d*dc*dsc*dp*ic08isc06ip50in*
alias: usb:v*p*d*dc*dsc*dp*ic08isc05ip50in*
alias: usb:v*p*d*dc*dsc*dp*ic08isc04ip50in*
[...]
intree: Y
name: usb_storage
[...]
parm: option_zero_cd:ZeroCD mode (1=Force Modem (default), 2=Allow CD-Rom (uint)
parm: swi_tru_install:TRU-Install mode (1=Full Logic (def), 2=Force CD-Rom, 3=Force Modem) (uint)
parm: delay_use:seconds to delay before using a new device (uint)
parm: quirks:supplemental list of device IDs and their quirks (string)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 287/531
Module utilities: lsmod
▶ Lists currently loaded kernel modules
▶ Includes
• The reference count: incremented when the module is used by another module or by
a user-space process, prevents from unloading modules that are in-use
• Dependant modules: modules that depend on us
▶ Information retrieved through /proc/modules
$ lsmod
Module Size Used by
tun 61440 2
tls 118784 0
rfcomm 90112 4
snd_seq_dummy 16384 0
snd_hrtimer 16384 1
wireguard 94208 0
curve25519_x86_64 36864 1 wireguard
libcurve25519_generic 49152 2 curve25519_x86_64,wireguard
ip6_udp_tunnel 16384 1 wireguard
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 288/531
Module utilities: insmod and rmmod
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 289/531
Module utilities: modprobe
▶ modprobe is the more advanced tool for loading/unloading modules
▶ Takes just a module name as argument: modprobe <module-name>
▶ Takes care of dependencies automatically, using the modules.dep file
▶ Supports removing modules using modprobe -r, including its no longer used
dependencies
# modinfo fat_test | grep depends
depends: kunit,fat
# lsmod | grep -E "^(kunit|fat|fat_test)"
fat 86016 1 vfat
# modprobe fat_test
# lsmod | grep -E "^(kunit|fat|fat_test)"
fat_test 24576 0
kunit 36864 1 fat_test
fat 86016 2 fat_test,vfat
# sudo modprobe -r fat_test
# lsmod | grep -E "^(kunit|fat|fat_test)"
fat 86016 1 vfat
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 290/531
Passing parameters to modules
▶ Some modules have parameters to adjust their behavior
▶ Mostly for debugging/tweaking, as parameters are global to the module, not per-device
managed by the module
▶ Through insmod or modprobe:
insmod ./usb-storage.ko delay_use=0
modprobe usb-storage delay_use=0
▶ modprobe supports configuration files: /etc/modprobe.conf or in any file in
/etc/modprobe.d/:
options usb-storage delay_use=0
▶ Through the kernel command line, when the module is built statically into the kernel:
usb-storage.delay_use=0
• usb-storage is the module name
• delay_use is the module parameter name. It specifies a delay before accessing a
USB storage device (useful for rotating devices).
• 0 is the module parameter value
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 291/531
Modules in sysfs
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 292/531
Accessing hardware devices
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 293/531
Describing non-discoverable hardware
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 294/531
Describing non-discoverable hardware
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 294/531
Describing non-discoverable hardware
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 294/531
Device Tree: from source to blob
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 295/531
dtc example
$ cat foo.dts
/dts-v1/;
/ {
welcome = <0xBADCAFE>;
bootlin {
webinar = "great";
demo = <1>, <2>, <3>;
};
};
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 296/531
dtc example
$ cat foo.dts
/dts-v1/;
/ {
welcome = <0xBADCAFE>;
bootlin {
webinar = "great";
demo = <1>, <2>, <3>;
};
};
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 296/531
dtc example
/ { / {
welcome = <0xBADCAFE>; welcome = <0xbadcafe>;
bootlin {
webinar = "great"; bootlin {
demo = <1>, <2>, <3>; webinar = "great";
}; demo = <0x01 0x02 0x03>;
}; };
};
$ dtc -I dts -O dtb -o foo.dtb foo.dts
$ ls -l foo.dt*
-rw-r--r-- 1 thomas thomas 169 ... foo.dtb
-rw-r--r-- 1 thomas thomas 102 ... foo.dts
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 296/531
Device Tree: using the blob
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 297/531
Where are Device Tree Sources located?
▶ Even though they are OS-agnostic, no central and OS-neutral place to host
Device Tree sources and share them between projects
• Often discussed, never done
▶ In practice, the Linux kernel sources can be considered as the canonical location
for Device Tree Source files
• arch/<ARCH>/boot/dts/<vendor>/
• arch/arm/boot/dts (on ARM 32 architecture before Linux 6.5)
• ≈ 4500 Device Tree Source files (.dts and .dtsi) in Linux as of 6.0.
▶ Duplicated/synced in various projects
• U-Boot, Barebox, TF-A
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 298/531
Device Tree base syntax
▶ Tree of nodes
▶ Nodes with properties
▶ Node ≈ a device or IP block
▶ Properties ≈ device characteristics
▶ Notion of cells in property values
▶ Notion of phandle to point to other
nodes
▶ dtc only does syntax checking, no
semantic validation
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 299/531
DT overall structure: simplified example
/ {
#address-cells = <1>;
#size-cells = <1>;
model = "STMicroelectronics STM32MP157C-DK2 Discovery Board";
compatible = "st,stm32mp157c-dk2", "st,stm32mp157";
cpus { ... };
memory@0 { ... };
chosen { ... };
intc: interrupt-controller@a0021000 { ... };
soc {
i2c1: i2c@40012000 { ... };
ethernet0: ethernet@5800a000 { ... };
};
};
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 300/531
DT overall structure: simplified example
/ {
cpus {
#address-cells = <1>;
#size-cells = <0>;
cpu0: cpu@0 {
compatible = "arm,cortex-a7";
clock-frequency = <650000000>;
device_type = "cpu";
reg = <0>;
};
cpu1: cpu@1 {
compatible = "arm,cortex-a7";
clock-frequency = <650000000>;
device_type = "cpu";
reg = <1>;
};
};
memory@0 { ... };
chosen { ... };
intc: interrupt-controller@a0021000 { ... };
soc {
i2c1: i2c@40012000 { ... };
ethernet0: ethernet@5800a000 { ... };
};
};
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 300/531
DT overall structure: simplified example
/ {
cpus { ... };
memory@0 {
device_type = "memory";
reg = <0x0 0x20000000>;
};
chosen {
bootargs = "";
stdout-path = "serial0:115200n8";
};
intc: interrupt-controller@a0021000 { ... };
soc {
i2c1: i2c@40012000 { ... };
ethernet0: ethernet@5800a000 { ... };
};
};
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 300/531
DT overall structure: simplified example
/ {
cpus { ... };
memory@0 { ... };
chosen { ... };
intc: interrupt-controller@a0021000 {
compatible = "arm,cortex-a7-gic";
#interrupt-cells = <3>;
interrupt-controller;
reg = <0xa0021000 0x1000>,
<0xa0022000 0x2000>;
};
soc {
compatible = "simple-bus";
#address-cells = <1>;
#size-cells = <1>;
interrupt-parent = <&intc>;
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 300/531
DT overall structure: simplified example
/ {
cpus { ... };
memory@0 { ... };
chosen { ... };
intc: interrupt-controller@a0021000 { ... };
soc {
i2c1: i2c@40012000 {
compatible = "st,stm32mp15-i2c";
reg = <0x40012000 0x400>;
interrupts = <GIC_SPI 31 IRQ_TYPE_LEVEL_HIGH>,
<GIC_SPI 32 IRQ_TYPE_LEVEL_HIGH>;
#address-cells = <1>;
#size-cells = <0>;
status = "okay";
cs42l51: cs42l51@4a {
compatible = "cirrus,cs42l51";
reg = <0x4a>;
reset-gpios = <&gpiog 9 GPIO_ACTIVE_LOW>;
status = "okay";
};
};
ethernet0: ethernet@5800a000 { ... };
};
};
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 300/531
DT overall structure: simplified example
/ {
cpus { ... };
memory@0 { ... };
chosen { ... };
intc: interrupt-controller@a0021000 { ... };
soc {
compatible = "simple-bus";
...
interrupt-parent = <&intc>;
i2c1: i2c@40012000 { ... };
ethernet0: ethernet@5800a000 {
compatible = "st,stm32mp1-dwmac", "snps,dwmac-4.20a";
reg = <0x5800a000 0x2000>;
interrupts-extended = <&intc GIC_SPI 61 IRQ_TYPE_LEVEL_HIGH>;
status = "okay";
mdio0 {
#address-cells = <1>;
#size-cells = <0>;
compatible = "snps,dwmac-mdio";
phy0: ethernet-phy@0 {
reg = <0>;
};
};
};
};
};
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 300/531
Device Tree inheritance
▶ Device Tree files are not monolithic, they can be split in several files, including
each other.
▶ .dtsi files are included files, while .dts files are final Device Trees
• Only .dts files are accepted as input to dtc
▶ Typically, .dtsi will contain
• definitions of SoC-level information
• definitions common to several boards
▶ The .dts file contains the board-level information
▶ The inclusion works by overlaying the tree of the including file over the tree of
the included file, according to the order of the #include directives.
▶ Allows an including file to override values specified by an included file
▶ Uses the C pre-processor #include directive
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 301/531
Device Tree inheritance example
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 302/531
Inheritance and labels
Doing:
soc.dtsi
/ {
soc {
usart1: serial@5c000000 {
compatible = "st,stm32h7-uart";
reg = <0x5c000000 0x400>;
status = "disabled";
};
};
};
board.dts
#include "soc.dtsi"
/ {
soc {
serial@5c000000 {
status = "okay";
};
};
};
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 303/531
Inheritance and labels
board.dts board.dts
#include "soc.dtsi" #include "soc.dtsi"
/ { &usart1 {
soc { status = "okay";
serial@5c000000 { };
status = "okay";
};
}; → this solution is now often preferred
};
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 303/531
DT inheritance in STM32MP1 support
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 304/531
Device Tree design principles
▶ Describe hardware (how the hardware is), not configuration (how I choose to
use the hardware)
▶ OS-agnostic
• For a given piece of HW, Device Tree should be the same for U-Boot, FreeBSD or
Linux
• There should be no need to change the Device Tree when updating the OS
▶ Describe integration of hardware components, not the internals of hardware
components
• The details of how a specific device/IP block is working is handled by code in device
drivers
• The Device Tree describes how the device/IP block is connected/integrated with the
rest of the system: IRQ lines, DMA channels, clocks, reset lines, etc.
▶ Like all beautiful design principles, these principles are sometimes violated.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 305/531
Device Tree specifications
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 306/531
Device Tree binding: old style
Documentation/devicetree/bindings/mtd/spear_smi.txt
This IP is not used on STM32MP1.
smi: flash@fc000000 {
Required properties: compatible = "st,spear600-smi";
- compatible : "st,spear600-smi" #address-cells = <1>;
- reg : Address range of the mtd chip #size-cells = <1>;
- #address-cells, #size-cells : Must be present if the device has sub-nodes reg = <0xfc000000 0x1000>;
representing partitions. interrupt-parent = <&vic1>;
interrupts = <12>;
- interrupts: Should contain the STMMAC interrupts clock-rate = <50000000>; /* 50MHz */
- clock-rate : Functional clock rate of SMI in Hz
flash@f8000000 {
Optional properties: st,smi-fast-mode;
- st,smi-fast-mode : Flash supports read in fast mode ...
};
};
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 307/531
Device Tree binding: YAML style
Documentation/devicetree/bindings/i2c/st,stm32-i2c.yaml
maintainers: ...
- Pierre-Yves MORDRET <[email protected]>
clock-frequency:
properties: description: Desired I2C bus clock frequency in Hz. If not specified,
compatible: the default 100 kHz frequency will be used.
enum: For STM32F7, STM32H7 and STM32MP1 SoCs, if timing
- st,stm32f4-i2c parameters match, the bus clock frequency can be from
- st,stm32f7-i2c 1Hz to 1MHz.
- st,stm32mp15-i2c default: 100000
minimum: 1
reg: maximum: 1000000
maxItems: 1
required:
interrupts: - compatible
items: - reg
- description: interrupt ID for I2C event - interrupts
- description: interrupt ID for I2C error - resets
- clocks
resets:
maxItems: 1
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 308/531
Device Tree binding: YAML style example
examples:
- |
//Example 3 (with st,stm32mp15-i2c compatible on stm32mp)
#include <dt-bindings/interrupt-controller/arm-gic.h>
#include <dt-bindings/clock/stm32mp1-clks.h>
#include <dt-bindings/reset/stm32mp1-resets.h>
i2c@40013000 {
compatible = "st,stm32mp15-i2c";
#address-cells = <1>;
#size-cells = <0>;
reg = <0x40013000 0x400>;
interrupts = <GIC_SPI 33 IRQ_TYPE_LEVEL_HIGH>,
<GIC_SPI 34 IRQ_TYPE_LEVEL_HIGH>;
clocks = <&rcc I2C2_K>;
resets = <&rcc I2C2_R>;
i2c-scl-rising-time-ns = <185>;
i2c-scl-falling-time-ns = <20>;
st,syscfg-fmp = <&syscfg 0x4 0x2>;
};
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 309/531
Validating Device Tree in Linux
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 310/531
The compatible property
▶ Is a list of strings
• From the most specific to the least specific
▶ Describes the specific binding to which the node complies.
▶ It uniquely identifies the programming model of the device.
▶ Practically speaking, it is used by the operating system to find the appropriate
driver for this device.
▶ When describing real hardware, the typical form is vendor,model
▶ Examples:
• compatible = "arm,armv7-timer";
• compatible = "st,stm32mp1-dwmac", "snps,dwmac-4.20a";
• compatible = "regulator-fixed";
• compatible = "gpio-keys";
▶ Special value: simple-bus → bus where all sub-nodes are memory-mapped
devices
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 311/531
compatible property and Linux kernel drivers
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 312/531
Matching with drivers in Linux: platform driver
drivers/tty/serial/stm32-usart.c
static const struct of_device_id stm32_match[] = {
{ .compatible = "st,stm32-uart", .data = &stm32f4_info},
{ .compatible = "st,stm32f7-uart", .data = &stm32f7_info},
{ .compatible = "st,stm32h7-uart", .data = &stm32h7_info},
{},
};
MODULE_DEVICE_TABLE(of, stm32_match);
...
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 313/531
Matching with drivers in Linux: I2C driver
sound/soc/codecs/cs42l51.c
const struct of_device_id cs42l51_of_match[] = {
{ .compatible = "cirrus,cs42l51", },
{ }
};
MODULE_DEVICE_TABLE(of, cs42l51_of_match);
sound/soc/codecs/cs42l51-i2c.c
static struct i2c_driver cs42l51_i2c_driver = {
.driver = {
.name = "cs42l51",
.of_match_table = cs42l51_of_match,
.pm = &cs42l51_pm_ops,
},
.probe = cs42l51_i2c_probe,
.remove = cs42l51_i2c_remove,
.id_table = cs42l51_i2c_id,
};
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 314/531
reg property
sai4: sai@50027000 {
reg = <0x50027000 0x4>, <0x500273f0 0x10>;
};
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 315/531
reg property
▶ Most important property after compatible
▶ Memory-mapped devices: base physical address and size of the memory-mapped
registers. Can have several entries for multiple register areas.
▶ I2C devices: address of the device on the I2C bus.
&i2c1 {
hdmi-transmitter@39 {
reg = <0x39>;
};
cs42l51: cs42l51@4a {
reg = <0x4a>;
};
};
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 315/531
reg property
▶ Most important property after compatible
▶ Memory-mapped devices: base physical address and size of the memory-mapped
registers. Can have several entries for multiple register areas.
▶ I2C devices: address of the device on the I2C bus.
▶ SPI devices: chip select number
&qspi {
flash0: mx66l51235l@0 {
reg = <0>;
};
flash1: mx66l51235l@1 {
reg = <1>;
};
};
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 315/531
reg property
sai4: sai@50027000 {
reg = <0x50027000 0x4>, <0x500273f0 0x10>;
};
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 315/531
Status property
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 316/531
Resources: interrupts, clocks, DMA, reset lines, ...
intc: interrupt-controller@a0021000 {
compatible = "arm,cortex-a7-gic";
#interrupt-cells = <3>;
▶ Common pattern for resources shared interrupt-controller;
reg = <0xa0021000 0x1000>, <0xa0022000 0x2000>;
by multiple hardware blocks };
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 317/531
Pin-muxing description
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 318/531
Pin-muxing controllers on STM32MP1
arch/arm/boot/dts/stm32mp151.dtsi
pinctrl: pin-controller@50002000 {
#address-cells = <1>;
#size-cells = <1>;
compatible = "st,stm32mp157-pinctrl";
...
gpioa: gpio@50002000 { ... };
gpiob: gpio@50003000 { ... };
gpioc: gpio@50004000 { ... };
gpiod: gpio@50005000 { ... };
gpioe: gpio@50006000 { ... };
gpiof: gpio@50007000 { ... };
...
};
pinctrl_z: pin-controller-z@54004000 {
#address-cells = <1>;
#size-cells = <1>;
compatible = "st,stm32mp157-z-pinctrl";
ranges = <0 0x54004000 0x400>;
...
gpioz: gpio@54004000 { .... };
...
};
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 319/531
Pin-muxing configuration
arch/arm/boot/dts/stm32mp15-pinctrl.dtsi
&pinctrl {
...
i2c1_pins_a: i2c1-0 {
pins {
pinmux = <STM32_PINMUX('D', 12, AF5)>, /* I2C1_SCL */
<STM32_PINMUX('F', 15, AF5)>; /* I2C1_SDA */
bias-disable;
drive-open-drain;
slew-rate = <0>;
};
};
...
m_can1_pins_a: m-can1-0 {
pins1 {
pinmux = <STM32_PINMUX('H', 13, AF9)>; /* CAN1_TX */
slew-rate = <1>;
drive-push-pull;
bias-disable;
};
pins2 {
pinmux = <STM32_PINMUX('I', 9, AF9)>; /* CAN1_RX */
bias-disable;
};
};
...
};
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 320/531
Pin-muxing configuration
Source: STM32MP157C datasheet. Note that I2C1_SDA is also available on pin PF15 (not shown here).
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 320/531
Pin-muxing consumer
&i2c1 {
pinctrl-names = "default", "sleep";
pinctrl-0 = <&i2c1_pins_a>;
pinctrl-1 = <&i2c1_sleep_pins_a>;
...
};
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 321/531
Example: LED and I2C device
▶ Let’s see how to describe an LED and an I2C device connected to the DK1
platform.
▶ Create arch/arm/boot/dts/stm32mp157a-dk1-custom.dts which includes
stm32mp157a-dk1.dts
#include "stm32mp157a-dk1.dts"
▶ make dtbs
DTC arch/arm/boot/dts/stm32mp157a-dk1-custom.dtb
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 322/531
Example: describe an LED
stm32mp157a-dk1-custom.dts
#include "stm32mp157a-dk1.dts"
/ {
leds {
compatible = "gpio-leds";
webinar {
label = "webinar";
gpios = <&gpioe 1 GPIO_ACTIVE_HIGH>;
};
};
};
shell
# echo 255 > /sys/class/leds/webinar/brightness
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 323/531
Example: connect I2C temperature, humidity and pressure sensor
stm32mp157a-dk1-custom.dts
&i2c5 {
status = "okay";
clock-frequency = <100000>;
pinctrl-names = "default", "sleep";
pinctrl-0 = <&i2c5_pins_a>;
pinctrl-1 = <&i2c5_pins_sleep_a>;
pressure@76 {
compatible = "bosch,bme280";
reg = <0x76>;
};
};
shell
# cat /sys/bus/iio/devices/iio\:device2/in_humidityrelative_input
49147
# cat /sys/bus/iio/devices/iio\:device2/in_pressure_input
101.567167968
# cat /sys/bus/iio/devices/iio\:device2/in_temp_input
24380
Details at https://bootlin.com/blog/building-a-linux-system-for-the-
stm32mp1-connecting-an-i2c-sensor/
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 324/531
Further details about the Device Tree
Check out our Device Tree 101 webinar, by Thomas Petazzoni (2021)
▶ Slides: https://bootlin.com/blog/device-tree-101-webinar-slides-and-videos/
▶ Video: https://youtu.be/a9CZ1Uk3OYQ
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 325/531
Accessing hardware devices
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 326/531
Discoverable hardware
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 327/531
Practical lab - Accessing hardware devices
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 328/531
Block filesystems
Block filesystems
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 329/531
Block filesystems
Block devices
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 330/531
Block vs. raw flash
▶ Storage devices are classified in two main types: block devices and raw flash
devices
• They are handled by different subsystems and different filesystems
▶ Block devices can be read and written to on a per-block basis, in random order,
without erasing.
• Hard disks, RAM disks
• USB keys, SSD, SD cards, eMMC: these are based on flash storage, but have an
integrated controller that emulates a block device, managing the flash in a
transparent way.
▶ Raw flash devices are driven by a controller on the SoC. They can be read, but
writing requires prior erasing, and often occurs on a larger size than the “block”
size.
• NOR flash, NAND flash
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 331/531
Block device list
▶ The list of all block devices available in the system can be found in
/proc/partitions
$ cat /proc/partitions
major minor #blocks name
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 332/531
Partitioning
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 333/531
Transfering data to a block device
▶ It is often necessary to transfer data to or from a block device in a raw way
• Especially to write a filesystem image to a block device
▶ This directly writes to the block device itself, bypassing any filesystem layer.
▶ The block devices in /dev/ allow such raw access
▶ dd (disk duplicate) is the tool of choice for such transfers:
• dd if=/dev/mmcblk0p1 of=testfile bs=1M count=16
Transfers 16 blocks of 1 MB from /dev/mmcblk0p1 to testfile
• dd if=testfile of=/dev/sda2 bs=1M seek=4
Transfers the complete contents of testfile to /dev/sda2, by blocks of 1 MB, but
starting at offset 4 MB in /dev/sda2
• Typical mistake: copying a file (which is not a filesystem image) to a filesystem
without mounting it first:
dd if=zImage of=/dev/sde1
Instead, you should use:
sudo mount /dev/sde1 /boot
cp zImage /boot/
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 334/531
Block filesystems
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 335/531
Ext2
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 336/531
Journaled filesystems
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 337/531
Filesystem recovery after crashes
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 338/531
Ext4
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 339/531
XFS
A Journaling filesystem
▶ Since 1994 (started by Silicon Graphics for the IRIX OS)
▶ Actively maintained and developed by Red Hat now
▶ Features: variable block size, direct I/O, online growth...
▶ Minimum partition size: 16MiB (9.7MiB of free space)
https://en.wikipedia.org/wiki/XFS
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 340/531
Btrfs
A copy-on-write filesystem
▶ Pronounced as ”better F S”, ”butter F S” or ”b-tree F S”, since 2009.
▶ A modern filesystem with many advanced features: volumes, snapshots,
transparent compression... Looks great for storage experts.
▶ Minimum partition size: 109MiB (only 32MiB of free space).
▶ However, big module size and long initialization time (bad for boot time)
https://en.wikipedia.org/wiki/Btrfs
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 341/531
F2FS — Flash-Friendly File System
A log-structured filesystem
▶ Since 2012 (started by Samsung, actively maintained)
▶ Designed from the start to take into account the characteristics of solid-state
based storage (eMMC, SD, SSD)
▶ In particular, trying to make most writes sequential (best on SSD)
▶ Support for transparent encryption and compression (LZO, LZ4, Zstd), possible
on a file by file (or file type) basis, through extended file attributes.
▶ Maximum partition size: 16TB, maximum file size: 3.94TB
▶ Minimum partition size: 52MiB (8MiB free space)
https://en.wikipedia.org/wiki/F2FS
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 342/531
SquashFS — A Read-Only and Compressed File System
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 343/531
EROFS — Enhanced Read-Only File System
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 344/531
Our advice for choosing the best filesystem
▶ Some filesystems will work better than others depending on how you use them.
▶ Fortunately, filesystems are easy to benchmark, being transparent to applications:
• Format your storage with each filesystem
• Copy your data to it
• Run your system on it and benchmark its performance.
• Keep the one working best in your case.
▶ If you haven’t done benchmarks yet, a good default choice is ext4 for read/write
partitions.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 345/531
Filesystem benchmarks
Boot Mount Read Seq. Write Seq. read Delete Space
time 1 time read write write
delete
ext4 very very fair good best very good good good
good good good
xfs bad average fair good very best average fair fair
good
btrfs worst good fair good good good fair worst good
f2fs fair average good good fair very very average worst
good good
See our presentation for more details and benchmarks (Linux 6.3, ARM32 BeagleBone Black):
https://bootlin.com/pub/conferences/2023/eoss/opdenacker-finding-best-block-filesystem/
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 346/531
Compatibility filesystems
Linux also supports several other filesystem formats, mainly to be interoperable with
other operating systems:
▶ vfat (CONFIG_VFAT_FS) for compatibility with the FAT filesystem used in the
Windows world and on numerous removable devices
• Also convenient to store bootloader binaries (FAT easy to understand for ROM code)
• This filesystem does not support features like permissions, ownership, symbolic links,
etc. Cannot be used for a Linux root filesystem.
• Linux now supports the exFAT filesystem too (CONFIG_EXFAT_FS).
▶ ntfs (CONFIG_NTFS_FS) for compatibility with Windows NTFS filesystem.
▶ hfs (CONFIG_HFS_FS) for compatibility with the MacOS HFS filesystem.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 347/531
tmpfs: filesystem in RAM
CONFIG_TMPFS
▶ Not a block filesystem of course!
▶ Perfect to store temporary data in RAM: system log files, connection data,
temporary files...
▶ More space-efficient than ramdisks: files are directly in the file cache, grows and
shrinks to accommodate stored files
▶ How to use: choose a name to distinguish the various tmpfs instances you have
(unlike in most other filesystems, each tmpfs instance is different). Examples:
mount -t tmpfs run /run
mount -t tmpfs shm /dev/shm
▶ See filesystems/tmpfs in kernel documentation.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 348/531
Block filesystems
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 349/531
Creating filesystems
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 350/531
Mounting filesystem images
▶ Once a filesystem image has been created, one can access and modify its contents
from the development workstation, using the loop mechanism:
▶ Example:
mkdir /mnt/test
mount -t ext4 -o loop rootfs.img /mnt/test
▶ In the /mnt/test directory, one can access and modify the contents of the
rootfs.img file.
▶ This is possible thanks to loop, which is a kernel driver that emulates a block
device with the contents of a file.
▶ Note: -o loop no longer necessary with recent versions of mount from GNU
Coreutils. Not true with BusyBox mount.
▶ Do not forget to run umount before using the filesystem image!
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 351/531
How to access partitions in a disk image
▶ You may have dumped a complete block device (with partitions) into a disk image.
▶ The losetup command allows to manually associate a loop device to a file, and
offers a --partscan option allowing to also create extra block device files for the
partitions inside the image:
$ ls -la /dev/loop2*
brw-rw---- 1 root disk 7, 2 Jan 14 10:50 /dev/loop2
brw-rw---- 1 root disk 259, 11 Jan 14 10:50 /dev/loop2p1
brw-rw---- 1 root disk 259, 12 Jan 14 10:50 /dev/loop2p2
▶ Each partition can then be accessed individually, for example:
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 353/531
Mixing read-only and read-write filesystems
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 354/531
Issues with flash-based block storage
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 355/531
Practical lab - Block filesystems
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 356/531
Flash storage and filesystems
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 357/531
Block devices vs raw flash devices: reminder
▶ Block devices:
• Allow for random data access using fixed size blocks
• Do not require special care when writing on the media
• Block size is relatively small (minimum 512 bytes, can be increased for performance
reasons)
• Considered as reliable (if the storage media is not, some hardware or software parts
are supposed to make it reliable)
▶ Raw flash devices:
• Flash chips directly driven by the flash controller on your SoC. You can control how
they are managed.
• Allow for random data access too, but require erasing before writing on the media.
• Read and write (for example 4 KiB) don’t use the same block size as erasing (for
example 128 KiB).
• Multiple flash technologies: NOR flash, NAND flash (Single Level Cell - SLC: 1 bit
per cell, MLC: multiple bits per cell).
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 358/531
NAND flash storage: constraints
▶ Reliability
• Reliability depends on flash technology (SLC, MLC)
• Require mechanisms to recover from bit flips: ECC
(Error Correcting Code), stored in the OOB
(Out-Of-Band area)
▶ Lifetime
• Relatively short lifetime: between 1,000,000 (SLC) and
1,000 (MLC) erase cycles per block
• Wear leveling required to erase blocks evenly
• Bad block detection/handling required too
▶ Widely used anyway in embedded systems for several reasons:
low cost, high capacity, good read and write performance.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 359/531
The MTD subsystem
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 360/531
MTD partitioning
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 361/531
MTD partitions on Linux
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 362/531
Commands to manage NAND devices
▶ From U-Boot
• help nand to see all nand subcommands
• nand info, nand read, nand write, nand erase...
▶ From Linux
• mtdchar driver: one /dev/mtdX and /dev/mtdXro character device per partition.
• Accessed through ioctl() operations to erase and flash the storage.
• Used by these utilities: flash_eraseall, nandwrite
Provided by the mtd-utils package, also available in BusyBox
• There are also host commands in mtd-utils: mkfs.jffs2, mkfs.ubifs, ubinize...
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 363/531
Flash wear leveling
▶ Wear leveling consists in distributing erases over the whole flash device to avoid
quickly reaching the maximum number of erase cycles on blocks that are written
really often
▶ Can be done in:
• the filesystem layer (JFFS2, YAFFS2, ...)
• an intermediate layer dedicated to wear leveling (UBI)
▶ The wear leveling implementation is what makes your flash lifetime good or not
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 364/531
Flash file-systems
▶ ’Standard’ file systems (ext2, ext4...) are meant to work on block devices
▶ Specific file systems have been developed to deal with flash constraints
▶ These file systems are relying on the MTD layer to access flash chips
▶ There are several legacy flash filesystems which might be useful for small
partitions: JFFS2, YAFFS2.
▶ Nowadays, UBI/UBIFS is the de facto standard for medium to large capacity
NANDs
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 365/531
UBI (1)
Standard file
UBI: Unsorted Block Images
API
▶ Design choices:
• Split the wear leveling and filesystem layers
• Add some flexibility UBIFS
• Focus on scalability, performance and reliability filesystem
▶ Drawback: introduces noticeable space overhead,
especially when used on small devices or partitions. UBI
JFFS2 still makes sense on small MTD partitions.
▶ Implements logical volumes on top of MTD devices (like
MTD
LVM for block devices)
driver
▶ Allows wear leveling to operate on the whole storage,
not only on individual partitions.
http://www.linux-mtd.infradead.org/doc/ubi.html
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 366/531
UBI (2)
When there is too much activity on an LEB, UBI can decide to move it to another
PEB with a lower erase count. Even read-only volumes participate to wear leveling!
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 367/531
UBI: good practice
▶ UBI distributes erases all over the flash device: the more space you assign to a
partition attached to the UBI layer the more efficient wear leveling will be.
▶ If you need partitioning, use UBI volumes, not MTD partitions.
▶ Some partitions will still have to be MTD partitions: e.g. the bootloaders.
▶ U-Boot now even supports storing its environment in a UBI volume!
▶ If you do need extra MTD partitions, try to group them at the beginning of the
flash device (often more reliable area).
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 368/531
UBI: bad and good practice
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 369/531
UBIFS
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 370/531
ubinize for UBI image creation
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 371/531
Linux: Block emulation layers
▶ Sometimes needed to use read-only block filesystems such as Squashfs and EROFS
▶ Linux provides two block emulation layers:
• mtdblock (CONFIG_MTD_BLOCK): block devices emulated on top of MTD devices.
Named /dev/mtdblockX, one for each partition.
Originally the mount command wanted a block device to mount JFFS2 and YAFFS2.
Don’t write to mtdblock devices: bad blocks are not handled!
• ubiblock (CONFIG_MTD_UBI_BLOCK): read-only block devices emulated on top of
UBI volumes
Used on static (read-only) volumes
Usually named /dev/ubiblockX_Y, where X is the UBI device id and Y is the UBI
volume id (example: /dev/ubiblock0_3)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 372/531
Cross-compiling user-space libraries and applications
Cross-compiling
user-space libraries and
applications
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 373/531
Integrating user-space libraries and applications
▶ One of the advantages of embedded Linux is the wide range of third-party libraries
and applications that one can leverage in its product
▶ There’s much more than U-Boot, Linux and Busybox that we can re-use from the
open-source world
▶ Networking, graphics, multimedia, crypto, language interpreters, and more.
▶ Each of those additional software components needs to be cross-compiled and
installed for our target
▶ Including all their dependencies
• Which can be quite complex as open-source encourages code re-use
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 374/531
Concept of build system
▶ Each open-source software project comes with its own set of scripts/files to
control its configuration/compilation: its build system
• Detect if system requirements/dependencies are met
• Compile all source files, to generate applications/libraries, as well as documentation
• Installs build products
▶ Most common build systems:
• Hand-written Makefiles
• Autotools: autoconf, automake, libtool
https://en.wikipedia.org/wiki/GNU_Autotools
• CMake
https://cmake.org/
• Meson
https://mesonbuild.com/
• Language specific build systems for Python, Perl, Go, Rust, NodeJS, etc.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 375/531
Target and staging spaces
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 376/531
Cross-compiling with hand-written Makefiles
▶ There is no general rule, as each project has a different set of Makefiles, that use
a different set of variables
▶ Though it is common to use make standard variables: CC (C compiler path), CXX
(C++ compiler path), LD (linker path), CFLAGS (C compiler flags), CXXFLAGS
(C++ compiler flags), LDFLAGS (linker flags)
▶ DESTDIR for installation destination, sometimes PREFIX for execution location
▶ Common sequence
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 377/531
Example: uftp native compilation
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 378/531
Example: uftp cross-compilation
First attempt
$ export PATH=/xtools/gcc-arm-10.3-2021.07-x86_64-arm-none-linux-gnueabihf/bin:$PATH
$ make CC=arm-none-linux-gnueabihf-gcc
[...]
encryption.h:87:10: fatal error: openssl/rsa.h: No such file or directory
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 379/531
Example: OpenSSL cross-compilation
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 380/531
Example: uftp with OpenSSL support
$ make CC=arm-none-linux-gnueabihf-gcc
encryption.h:87:10: fatal error: openssl/rsa.h:
No such file or directory $ make CC=arm-none-linux-gnueabihf-gcc \
[...] CFLAGS=-I/tmp/staging/usr/include \
LDFLAGS=-L/tmp/staging/usr/lib
[... builds OK! ...]
It cannot find the header, let’s add CFLAGS $ make DESTDIR=/tmp/target install
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 381/531
Autotools
▶ A family of tools, which associated together form a complete and extensible build
system
• autoconf is used to handle the configuration of the software package
• automake is used to generate the Makefiles needed to build the software package
• libtool is used to handle the generation of shared libraries in a system-independent
way
▶ Most of these tools are old and relatively complicated to use
▶ But they are used by a large number of software components, even though Meson
is gaining significant traction as a replacement today
▶ See also Bootlin Autotools training materials
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 382/531
automake / autoconf / autoheader
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 383/531
automake / autoconf
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 384/531
autotools usage: four steps
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 385/531
Example: can-utils native compilation
Download
$ git clone https://github.com/linux-can/can-utils.git Configuration
$ cd can-utils/ $ ./configure --prefix=/usr
$ git checkout v2021.08.0 $ ls -1 *makefile*
$ ls -1 configure* *makefile* GNUmakefile
configure.ac GNUmakefile.am
GNUmakefile.am GNUmakefile.in
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 386/531
Example: can-utils cross-compilation
$ export PATH=/xtools/gcc-arm-10.3-2021.07-x86_64-arm-none-linux-gnueabihf/bin:$PATH
$ ./configure --prefix=/usr --host=arm-none-linux-gnueabihf
$ make
$ make DESTDIR=/tmp/target install
$ file /tmp/target/usr/bin/candump
/tmp/target/usr/bin/candump: ELF 32-bit LSB executable, ARM
Note: This is a simple example, as can-utils does not have any dependency other than
the C library, and has a simple configure.ac file.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 387/531
CMake
https://en.wikipedia.org/wiki/CMake
▶ More modern build system, started in 1999, maintained by a company called
Kitware
▶ Used by Qt 6, KDE, and many projects which didn’t like autotools
▶ Perhaps losing traction these days in favor of Meson
▶ Needs cmake installed on your machine
▶ Based on:
• CMakeLists.txt files that describe what the dependencies are and what to build
and install
• cmake, a tool that processes CMakeLists.txt to generate either Makefiles (default)
or Ninja files (covered later)
▶ Typical sequence, when using the Makefile backend:
1. cmake .
2. make
3. make install
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 388/531
Example: cJSON native compilation
Installed files
$ tree /tmp/test
/tmp/test/
Download ��� usr
��� include
$ git clone https://github.com/DaveGamble/cJSON.git � ��� cjson
$ cd cJSON � ��� cJSON.h
$ git checkout v1.7.15 ��� lib64
��� cmake
� ��� cJSON
Configure, build, install � ��� cjson.cmake
� ��� cJSONConfig.cmake
$ cmake -DCMAKE_INSTALL_PREFIX=/usr . � ��� cJSONConfigVersion.cmake
$ make � ��� cjson-noconfig.cmake
$ make DESTDIR=/tmp/test install ��� libcjson.so -> libcjson.so.1
��� libcjson.so.1 -> libcjson.so.1.7.15
��� libcjson.so.1.7.15
��� pkgconfig
��� libcjson.pc
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 389/531
Example: cJSON cross-compilation
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 390/531
CMake toolchain file
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 391/531
Meson
https://en.wikipedia.org/wiki/Meson_(software)
▶ The most modern one, written in Python
▶ Gaining big traction in lots of major open-source projects
▶ Processes meson.build + meson_options.txt and generates Ninja files
▶ Ninja is an alternative to make, with much shorter build times
▶ Needs meson and ninja installed on your machine
▶ Meson requires an out-of-tree build: the build directory must be distinct from the
source directory
1. mkdir build
2. cd build
3. meson ..
4. ninja
5. ninja install
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 392/531
Example: ipcalc native compilation
Download
$ git clone https://gitlab.com/ipcalc/ipcalc.git
$ cd ipcalc
$ git checkout 1.0.1 Installed files
$ tree /tmp/test
/tmp/test/
Configuration, build, installation ��� usr
��� bin
$ mkdir build
��� ipcalc
$ cd build
$ meson --prefix /usr ..
$ ninja
$ DESTDIR=/tmp/test ninja install
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 393/531
Meson cross file
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 394/531
Example: ipcalc cross-compilation
$ cat cross-file.txt
[binaries]
c = 'arm-none-linux-gnueabihf-gcc'
strip = 'arm-none-linux-gnueabihf-strip'
[host_machine]
system = 'linux'
cpu_family = 'arm'
cpu = 'cortex-a9'
endian = 'little'
$ mkdir build-cross
$ cd build-cross
$ meson --cross-file ../cross-file.txt --prefix /usr ..
$ ninja
$ DESTDIR=/tmp/target ninja install
$ file /tmp/target/usr/bin/ipcalc
/tmp/target/usr/bin/ipcalc: ELF 32-bit LSB executable, ARM
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 395/531
Distinction between prefix and DESTDIR
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 396/531
pkg-config
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 397/531
pkg-config example for native compilation
$ pkg-config --list-all
openssl OpenSSL - Secure Sockets Layer and cryptography libraries and tools
zlib zlib - zlib compression library
blkid blkid - Block device id library
cairo-script cairo-script - script surface backend for cairo graphics library
cairo-pdf cairo-pdf - PDF surface backend for cairo graphics library
xcb-xinput XCB XInput - XCB XInput Extension (EXPERIMENTAL)
libcurl libcurl - Library to transfer files with ftp, http, etc.
[...]
$ pkg-config --cflags --libs openssl
-lssl -lcrypto
$ pkg-config --cflags --libs cairo-script
-I/usr/include/cairo -I/usr/include/libpng16 -I/usr/include/freetype2 -I/usr/include/harfbuzz
[...] -lcairo -lz
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 398/531
pkg-config example for cross-compilation
Use PKG_CONFIG_LIBDIR
$ export PKG_CONFIG_LIBDIR=/tmp/staging/usr/lib/pkgconfig
$ pkg-config --list-all
openssl OpenSSL - Secure Sockets Layer and cryptography libraries and tools
libssl OpenSSL-libssl - Secure Sockets Layer and cryptography libraries
libcrypto OpenSSL-libcrypto - OpenSSL cryptography library
$ pkg-config --cflags --libs openssl
-I/usr/include -L/usr/lib -lssl -lcrypto
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 399/531
Practical lab - Cross-compiling applications and libraries
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 400/531
Embedded system building tools
Embedded system
building tools
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 401/531
Approaches
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 402/531
Approaches pros and cons
Pros Cons
Building everything manually Full flexibility Dependency hell
Learning experience Need to understand a lot of details
Version compatibility
Lack of reproducibility
Binary distribution Easy to create and extend Hard to customize
Debian, Ubuntu, Fedora, etc. Extensive set of packages Hard to optimize (boot time, size)
Usually excellent security maintenance Hard to rebuild the full system from source
Large system
Uses native compilation (slow)
No well-defined mechanism to generate an
image
Lots of mandatory dependencies
Not available for all architectures
Build systems Nearly full flexibility Not as easy as a binary distribution
Buildroot, Yocto, PTXdist, OpenWrt, etc. Built from source: customization and op- Build time
timization are easy
Fully reproducible
Uses cross-compilation
Have embedded specific packages not nec-
essarily in desktop distros
Make more features optional
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 403/531
Embedded system building tools
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 404/531
Embedded Linux build system: principle
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 405/531
Build systems vs. Embedded Linux build systems
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 406/531
Buildroot: introduction
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 407/531
Buildroot: configuration and build
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 408/531
Buildroot: adding a new package
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 409/531
Buildroot: adding a new package, Config.in
package/jose/Config.in
config BR2_PACKAGE_JOSE
bool "jose"
depends on BR2_TOOLCHAIN_HAS_THREADS
select BR2_PACKAGE_ZLIB
select BR2_PACKAGE_JANSSON
select BR2_PACKAGE_OPENSSL
help
C-language implementation of Javascript Object Signing and
Encryption.
https://github.com/latchset/jose
package/Config.in
[...]
source "package/jose/Config.in"
[...]
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 410/531
Buildroot: adding new package, .mk file
package/jose/jose.mk
JOSE_VERSION = 11
JOSE_SOURCE = jose-$(JOSE_VERSION).tar.xz
JOSE_SITE = https://github.com/latchset/jose/releases/download/v$(JOSE_VERSION)
JOSE_LICENSE = Apache-2.0
JOSE_LICENSE_FILES = COPYING
JOSE_INSTALL_STAGING = YES
JOSE_DEPENDENCIES = host-pkgconf zlib jansson openssl
$(eval $(meson-package))
▶ The package directory and the prefix of all variables must be identical to the suffix
of the main configuration option BR2_PACKAGE_JOSE
▶ The meson-package infrastructure knows how to build Meson packages. Many
other infrastructures exist, for different build systems
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 411/531
Buildroot resources
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 412/531
Yocto Project / OpenEmbedded
▶ OpenEmbedded
• Started in 2003
• Goal is to build custom Linux distributions for
embedded devices
• Back then, no stable releases, limited/no
documentation, difficult to use for products
▶ Yocto Project
• Started in 2011
• By the Linux Foundation
• Goal is to industrialize OpenEmbedded
• Funds the development of OpenEmbedded,
makes regular stable releases, QA effort,
extensive documentation
• One Long Term Support release every 2 years,
supported for 4 years.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 413/531
Yocto Project overview
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 414/531
Yocto Project concepts
▶ Terminology
• Layer: Git repository containing a collection of recipes, machines, etc.
• Recipe: metadata that describes how to build a particular software component, the
contents of an image to generate
• Machine: a specific hardware platform
• bitbake: the orchestration tool that processes recipes to generate the final products
▶ Yocto/OpenEmbedded generate a distribution
• For each recipe, it produces one or several binary packages (deb, rpm, ipk)
• A selection of these binary packages are installed to create a root filesystem image
that can be flashed
• The other packages can be installed at runtime on the system using a package
management system: apt, dnf, opkg
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 415/531
Public layers (1/2)
▶ Core layers
• bitbake, not really a layer, but the core build orchestration tool
• openembedded-core, the very core recipes, to build the most common software
packages: Linux, BusyBox, toolchain, systemd, mesa3d, X.org, Wayland
bootloaders. Supports only QEMU machines.
• poky, a layer from the Yocto Project that combines openembedded-core, bitbake,
that defines the Poky distribution, a reference distribution. Supports a few more
machines. In practice not useful for real projects.
• meta-openembedded, community maintained additional recipes from the
OpenEmbedded project
▶ BSP layers, provided by HW vendors or the community, to support additional
hardware platforms: recipes for building custom Linux kernel, bootloaders, for
HW-related software components
• meta-intel, meta-arm, meta-ti, meta-xilinx, meta-freescale, meta-atmel,
meta-st-stm32mp, etc.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 416/531
Public layers (2/2)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 417/531
Combine layers
▶ For your project, you will typically combine a number of public layers
• At least the openembedded-core layer
• Possibly one or several BSP layers
• Possibly one or several additional software layers
▶ And you will create your own layer, containing recipes for:
• Machine definitions for your custom hardware platforms
• Image/distro definitions for your custom system(s)
• Recipes for your custom software
▶ A tool is often used to automate the retrieval of the necessary layers, at the right
version
• Google repo tool, the Yocto-specific Kas utility
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 418/531
Yocto quick start: STM32MP1 example
Note: we’re not using a tool such as repo or Kas here, we are fetching each layer
manually.
Enter the build environment
$ source openembedded-core/oe-init-build-env
This automatically enters a directory called build/, with a few files/directories already
prepared.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 419/531
Yocto quick start: STM32MP1 example
Build results
$ ls tmp/deploy/images/stm32mp1/
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 420/531
Yocto recipe example
openembedded-core/tree/meta/recipes-extended/libmnl/libmnl_1.0.5.bb
SUMMARY = "Minimalistic user-space Netlink utility library"
DESCRIPTION = "Minimalistic user-space library oriented to Netlink developers, providing \
functions for common tasks in parsing, validating, and constructing both the Netlink header and TLVs."
HOMEPAGE = "https://www.netfilter.org/projects/libmnl/index.html"
SECTION = "libs"
LICENSE = "LGPL-2.1-or-later"
LIC_FILES_CHKSUM = "file://COPYING;md5=4fbd65380cdd255951079008b364516c"
SRC_URI = "https://netfilter.org/projects/libmnl/files/libmnl-${PV}.tar.bz2"
SRC_URI[sha256sum] = "274b9b919ef3152bfb3da3a13c950dd60d6e2bcd54230ffeca298d03b40d0525"
BBCLASSEXTEND = "native"
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 421/531
Yocto resources
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 422/531
Buildroot vs. Yocto: a few key differences
▶ What it builds
• Yocto: builds a distribution, with binary packages and a package management
system
• Buildroot: builds a fixed functionality root filesystem, no binary packages
• Note: binary packages are not necessarily a good thing for embedded!
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 423/531
Buildroot vs. Yocto: a few key differences
▶ What it builds
▶ Configuration
• Yocto: flexible, powerful but complex configuration description
• Buildroot: very simple configuration system, but sometimes limited
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 423/531
Buildroot vs. Yocto: a few key differences
▶ What it builds
▶ Configuration
▶ Build strategy
• Yocto: complex and heavy logic, but with efficient caching of artifacts and “rebuild
only what’s needed” features
• Buildroot: simple but somewhat dumb logic, no caching of built artifacts, full
rebuilds needed for some config changes
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 423/531
Buildroot vs. Yocto: a few key differences
▶ What it builds
▶ Configuration
▶ Build strategy
▶ Ecosystem
• Yocto: (relatively) small common base in OpenEmbedded, lots of features
supported in third party layers → lots of things, but varying quality
• Buildroot: everything in one tree → perhaps less things, but more consistent quality
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 423/531
Buildroot vs. Yocto: a few key differences
▶ What it builds
▶ Configuration
▶ Build strategy
▶ Ecosystem
▶ Complexity/learning curve
• Yocto: admittedly steep learning curve, bitbake remains a magic black box for most
people
• Buildroot: much smoother and shorter learning curve, the tool is simple to
approach, and reasonably simple to understand
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 423/531
Buildroot vs. Yocto: a few key differences
▶ What it builds
▶ Configuration
▶ Build strategy
▶ Ecosystem
▶ Complexity/learning curve
▶ And also a matter of personal taste/preference, as often when choosing tools
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 423/531
OpenWrt
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 424/531
Embedded system building tools
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 425/531
Binary distributions
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 426/531
Binary distributions pitfalls
▶ Be careful when using a binary distribution on how you create your system image,
and how reproducible this process is
▶ We have seen projects use the following (bad) procedure:
• Install a binary distribution manually on their target hardware
• Install all necessary packages by hand
• Compile the final applications on the target
• Tweak configuration files directly on the target
• Then duplicate the resulting SD card for all other boards
▶ This process is really bad as:
• it is not reproducible
• it requires installing many more things on the target than needed (development
tools), increasing the footprint, the attack surface and the maintenance effort
▶ If you end up using a binary distribution in production, make sure you have an
automated and reproducible process to generate the complete image, ready to
flash on your target.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 427/531
Debian/Ubuntu image building tools
ELBE
▶ E.mbedded L.inux B.uild E.nvironment DebOS
▶ Implemented in Python ▶ Debian OS images builder
▶ Uses an XML file as input to describe the ▶ Implemented in Go
system to generate ▶ Uses a YAML file as input to
▶ Can use pre-built packages from describe the system to
Debian/Ubuntu repositories, but can also generate
cross-compile and install additional packages ▶ Creating Debian-Based
▶ https://elbe-rfs.org/ Embedded Systems in the
▶ Building Embedded Debian and Ubuntu Cloud Using Debos talk
Systems with ELBE talk ▶ https://github.com/go-
▶ ELBE: automated building of Ubuntu images debos/debos
for a Raspberry Pi 3B
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 428/531
Android
▶ The obviously highly popular mobile operating system
▶ Uses the Linux kernel
▶ Most of the user-space is completely different from a normal embedded Linux
system
• Most components rewritten by Google
• bionic C library
• Custom init system and device management
• Custom IPC mechanism, custom display stack, custom multimedia stack
• Custom build system
▶ Android pitfalls for industrial embedded systems
• Large footprint, and resource hungry
• Complexity and build time
• Maintenance issues: difficult to upgrade to newer releases due to increasing hardware
requirements
▶ Embedded Android Training course from Opersys, with freely available training
materials
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 429/531
Automotive Grade Linux, Tizen
▶ Industry groups collaborate around the creation of embedded Linux distributions
targeting specific markets
• These are regular embedded Linux systems, usually based on Yocto, with a selection
of relevant open-source software components
• Fund the development of missing features in existing components, or development of
new software components
▶ Automotive Grade Linux
• Linux Foundation project
• Collaborative open source project that is bringing together automakers, suppliers and
technology companies to accelerate the development and adoption of a fully open
software stack for the connected car
• https://www.automotivelinux.org/
▶ Tizen
• Linux Foundation project too
• Operating system targeting TVs, wearables, phones, in-vehicle infotainment, based
on HTML5 applications.
• https://www.tizen.org/
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 430/531
Practical lab - System build with Buildroot
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 431/531
Open source licenses and compliance
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 432/531
Open source licenses and compliance
Introduction
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 433/531
Free software vs. open-source
▶ Free software: term defined by the Free Software Foundation, grants 4 freedoms
• Freedom to use
• Freedom to study
• Freedom to copy
• Freedom to modify and distribute modified copies
• See https://www.gnu.org/philosophy/free-sw.html
▶ Open Source: term defined by the Open Source Initiative, with 10 criterias
• See https://www.opensource.org/docs/osd
▶ Free Software movement insists more on ethics, while Open Source insists more
on the technical advantages
▶ From a freedom standpoint, they are similar.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 434/531
Open source licenses
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 435/531
Non-Copyleft VS Copyleft licenses
Non-Copyleft Copyleft
(BSD, MIT, Apache, X11…) (GPL, LGPL, AGPL…)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 436/531
What is copyleft
▶ The concept of copyleft is to ask for reciprocity in the freedoms given to a user.
▶ You receive software under a copyleft license and redistribute it, modified or not
→ you must do so under the same license
• Same freedoms to the new users
• Incentive, but no obligation, to contribute back your changes instead of keeping
them secret
▶ Copyleft is not the opposite of copyright!
▶ Non-copyleft licenses have no such requirements: modified versions can be made
proprietary, but they still require attribution
▶ https://en.wikipedia.org/wiki/Copyleft
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 437/531
Open source licenses and compliance
Non-copyleft licenses
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 438/531
Most common non-copyleft licenses
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 439/531
Open source licenses and compliance
Copyleft licenses
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 440/531
GPL: GNU General Public License
▶ The flagship license of the GNU project
▶ Used by Linux, BusyBox, U-Boot, Barebox, GRUB, many projects from GNU
▶ Is a copyleft license
• Requires derivative works to be released under the same license
• Source code must be redistributed, including modifications
• If GPL code is integrated in your code, your code must now be GPL-licensed
• Only applies when redistribution takes place
▶ Also called strong copyleft license
• Programs linked with a library released under the GPL must also be released under
the GPL
• Does not prevent GPL programs and non-GPL programs from co-existing in the
same system or to communicate
▶ https://www.gnu.org/licenses/gpl-2.0.en.html
▶ https://www.gnu.org/licenses/gpl-3.0.en.html
▶ https://en.wikipedia.org/wiki/GNU_General_Public_License
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 441/531
LGPL: GNU Lesser General Public License
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 442/531
GPL/LGPL: redistribution
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 443/531
GPL/LGPL: version 2 vs version 3
▶ GPLv2/LGPLv2 published in 1991, widely used in the open-source world for major
projects
▶ GPLv3/LGPLv3 published in 2007, and adopted by some projects
▶ Main differences
• More legalese and definitions to clarify the license
• Explicit patent grant
• Grace period of 30 days to get back into compliance instead of immediate
termination
• Anti-Tivoization clause
▶ Anti-Tivoization
• Requirement that the user must be able to run the modified versions on the device
• Need to provide installation instructions
• Only required for User products, i.e. consumer devices
• On-going debate on how strong this requirement is, and how difficult it is to comply
with
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 444/531
GPL: v2, v3, v2 or later, v3 or later
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 445/531
Dual licensing
▶ Some companies use a dual licensing business model, mainly for software libraries
▶ Their software is offered under two licenses:
• A strong copyleft license, typically GPL, to encourage adoption of the software by
the open-source world, allow the development and distribution of GPL licensed
applications based on this library
• A commercial license, offered against a fee, which allows to develop and distribute
proprietary applications based on this library.
▶ Examples: Qt (only parts), MySQL, wolfSSL, Asterisk, etc.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 446/531
Is this free software?
▶ Most of the free software projects are covered by about 10 well-known licenses, so
it is fairly easy for the majority of projects to get a good understanding of the
license
▶ Check Free Software Foundation’s opinion
https://www.fsf.org/licensing/licenses/
▶ Check Open Source Initiative’s opinion
https://www.opensource.org/licenses
▶ Check the simplified license description on tl;drLegal
https://www.tldrlegal.com
▶ Otherwise, read the license text
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 447/531
Licensing: examples
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 448/531
Open source licenses and compliance
Best practices
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 449/531
Respect free software licenses
▶ Free Software is not public domain software, the distributors have obligations due
to the licenses
▶ Before using a free software component, make sure the license matches your
project constraints
▶ Make sure to keep your modifications and adaptations well-separated from the
original version.
▶ Make sure to keep a complete list of the free software packages you use, and the
version in use
▶ Buildroot and Yocto Project can generate this list for you!
• Buildroot: make legal-info
• Yocto: see the project documentation
▶ Conform to the license requirements before shipping the product to the customers.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 450/531
Keeping changes separate
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 451/531
Overview of major embedded Linux software stacks
Overview of major
embedded Linux
software stacks
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 452/531
D-Bus
▶ Message-oriented middleware mechanism that allows
communication between multiple processes running concurrently
on the same machine
▶ Relies on a daemon to pass messages between applications
▶ Mainly used by system daemons to offer services to client
applications
▶ Example: a network configuration daemon, running as root,
offers a D-Bus API that CLI and GUI clients can use to
configure networking
▶ Several busses
• One system bus, accessible by all users, for system services
• One session bus for each user logged in
▶ Object model: interfaces, objects, methods, signals
▶ https://www.freedesktop.org/wiki/Software/dbus/
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 453/531
systemd (1)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 454/531
systemd (2)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 455/531
systemd service unit file example
/usr/lib/systemd/system/sshd.service
[Unit]
Description=OpenSSH server daemon
Documentation=man:sshd(8) man:sshd_config(5)
After=network.target sshd-keygen.service
Wants=sshd-keygen.service
[Service]
EnvironmentFile=/etc/sysconfig/sshd
ExecStart=/usr/sbin/sshd -D $OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s
[Install]
WantedBy=multi-user.target
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 456/531
Example systemctl/journalctl commands
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 457/531
Linux graphics stack overview
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 458/531
Display controller support
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 459/531
GPU support: OpenGL acceleration
▶ Open-source
• A kernel driver in the DRM subsystem to send commands to the GPU and manage
memory
• mesa3d user-space library implementing the various OpenGL APIs, contains massive
GPU-specific logic
• More and more GPUs supported
• https://www.mesa3d.org/
▶ Proprietary
• Many embedded GPUs used to be supported only through proprietary blobs →
long-term maintenance issues
• A kernel driver provided out-of-tree by the vendor → they are not accepted upstream
if the user-space is closed source
• A (huge) closed-source user-space binary blob implementing the various OpenGL
APIs
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 460/531
Concept of display servers
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 461/531
X11 and X.org
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 462/531
Wayland
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 463/531
Wayland compositors
▶ Weston
• The reference compositor
• https://gitlab.freedesktop.org/wayland/weston
▶ Mutter, used by the GNOME desktop environment
https://gitlab.gnome.org/GNOME/mutter
▶ wlroots, a Wayland compositor library, used by
• Cage, a Wayland kiosk-style compositor
https://github.com/Hjdskes/cage
• swayWM, a tiling Wayland compositor
https://swaywm.org/
▶ And many more
https://wiki.archlinux.org/title/wayland#Compositors
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 464/531
Concept of graphics toolkits
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 467/531
Flutter
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 468/531
SDL
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 469/531
Other graphical toolkits
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 470/531
Further details on Linux graphics
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 471/531
Linux multimedia stack overview
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 472/531
Audio stack
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 473/531
Video stack
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 474/531
GStreamer
▶ Library for constructing graphs of media-handling components
▶ Allows to create pipelines to transform, convert, stream, display, capture
multimedia streams, both audio and video
▶ Composed of a vast amounts of plugins: video capture/display, audio
capture/playback, encoding/decoding, scaling, filtering, and more.
▶ https://gstreamer.freedesktop.org/
▶ An interesting alternative is ffmpeg
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 475/531
Linux networking stack
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 476/531
Web accessible UI
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 477/531
Web browsers: rendering engines
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 478/531
Web-based UIs
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 479/531
Programming languages
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 480/531
Practical lab - Integration of additional software stacks
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 481/531
Embedded Linux application development
Embedded Linux
application development
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 482/531
Contents
▶ Application development
• Developing applications on embedded Linux
• Building your applications
▶ Debugging and analysis tools
• Debuggers
• Remote debugging
• Tracing and profiling
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 483/531
Embedded Linux application development
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 484/531
Application development
▶ An embedded Linux system is just a normal Linux system, with usually a smaller
selection of components
▶ In terms of application development, developing on embedded Linux is exactly the
same as developing on a desktop Linux system
▶ All existing skills can be re-used, without any particular adaptation
▶ All existing libraries, either third-party or in-house, can be integrated into the
embedded Linux system
• Taking into account, of course, the limitation of the embedded systems in terms of
performance, storage and memory
▶ Application development could start on x86, even before the hardware is available.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 485/531
Leverage existing libraries and languages
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 486/531
Building your applications/libraries
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 487/531
Getting started with meson
Minimal meson.build
project('example', 'c')
executable('demo', 'main.c')
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 488/531
Options with meson
meson_options.txt
option('demo-debug', type : 'feature', value : 'disabled')
meson.build
project('tutorial', 'c')
demo_c_args = []
if get_option('demo-debug').enabled()
demo_c_args += '-DDEBUG'
endif
executable('demo', 'main.c', c_args: demo_c_args)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 489/531
Library dependencies with meson
meson.build
project('tutorial', 'c')
gtkdep = dependency('gtk+-3.0')
executable('demo', 'main.c', dependencies : gtkdep)
The dependency gtk+-3.0 is searched using pkg-config.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 490/531
Embedded Linux application development
Debugging
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 491/531
GDB: GNU Project Debugger
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 493/531
GDB crash course (2/3)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 494/531
GDB crash course (3/3)
▶ continue (c)
Continue the execution after a breakpoint
▶ next (n)
Continue to the next line, stepping over function calls
▶ step (s)
Continue to the next line, entering into subfunctions
▶ stepi (si)
Continue to the next instruction
▶ finish
Execute up to function return
▶ backtrace (bt)
Display the program stack
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 495/531
Embedded Linux application development
Remote debugging
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 496/531
Remote debugging
▶ In a non-embedded environment, debugging takes place using gdb or one of its
front-ends.
▶ gdb has direct access to the binary and libraries compiled with debugging symbols.
▶ However, in an embedded context, the target platform environment is often too
limited to allow direct debugging with gdb (2.4 MB on x86).
▶ Remote debugging is preferred
• ARCH-linux-gdb is used on the development workstation, offering all its features.
• gdbserver is used on the target system (only 400 KB on arm).
ARCH-linux-gdb
gdbserver
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 497/531
Remote debugging: architecture
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 498/531
Remote debugging: target setup
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 499/531
Remote debugging: host setup
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 500/531
Coredumps for post mortem analysis
▶ When an application crashes due to a segmentation fault and the application was
not under control of a debugger, we get no information about the crash
▶ Fortunately, Linux can generate a core file that contains the image of the
application memory at the moment of the crash in the ELF format. gdb can use
this core file to let us analyze the state of the crashed application
▶ On the target
• Use ulimit -c unlimited in the shell starting the application, to enable the
generation of a core file when a crash occurs
• The output name for the coredump file can be modified using
/proc/sys/kernel/core_pattern.
• See man core(5)
▶ On the host
• After the crash, transfer the core file from the target to the host, and run
ARCH-linux-gdb -c core-file application-binary
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 501/531
minicoredumper
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 502/531
Embedded Linux application development
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 503/531
strace
System call tracer - https://strace.io
▶ Available on all GNU/Linux systems
Can be built by your cross-compiling toolchain generator or by
your build system.
▶ Allows to see what any of your processes is doing: accessing files,
allocating memory... Often sufficient to find simple bugs.
▶ Usage:
strace <command> (starting a new process)
strace -f <command> (follow child processes too)
strace -p <pid> (tracing an existing process)
strace -c <command> (time statistics per system call)
strace -e <expr> <command> (use expression for advanced
filtering)
Image credits: https://strace.io/
See the strace manual for details.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 504/531
strace example output
> strace cat Makefile
[...]
fstat64(3, {st_mode=S_IFREG|0644, st_size=111585, ...}) = 0
mmap2(NULL, 111585, PROT_READ, MAP_PRIVATE, 3, 0) = 0xb7f69000
close(3) = 0
access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)
open("/lib/tls/i686/cmov/libc.so.6", O_RDONLY) = 3
read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0\320h\1\0004\0\0\0\344"..., 512) = 512
fstat64(3, {st_mode=S_IFREG|0755, st_size=1442180, ...}) = 0
mmap2(NULL, 1451632, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0xb7e06000
mprotect(0xb7f62000, 4096, PROT_NONE) = 0
mmap2(0xb7f66000, 9840, PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0xb7f66000
close(3) = 0
[...]
openat(AT_FDCWD, "Makefile", O_RDONLY) = 3
newfstatat(3, "", {st_mode=S_IFREG|0644, st_size=173, ...}, AT_EMPTY_PATH) = 0
fadvise64(3, 0, 0, POSIX_FADV_SEQUENTIAL) = 0
mmap(NULL, 139264, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0x7f7290d28000
read(3, "ifneq ($(KERNELRELEASE),)\nobj-m "..., 131072) = 173
write(1, "ifneq ($(KERNELRELEASE),)\nobj-m "..., 173ifneq ($(KERNELRELEASE),)
Hint: follow the open file descriptors returned by open(). This tells you what files are
handled by further system calls.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 505/531
strace -c example output
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 506/531
ltrace
A tool to trace shared library calls used by a program and all the signals it receives
▶ Very useful complement to strace, which shows only system calls.
▶ Of course, works even if you don’t have the sources
▶ Allows to filter library calls with regular expressions, or just by a list of function
names.
▶ With the -S option it shows system calls too!
▶ Also offers a summary with its -c option.
▶ Manual page: https://linux.die.net/man/1/ltrace
▶ Works better with glibc. ltrace used to be broken with uClibc (now fixed), and is
not supported with Musl (Buildroot 2022.11 status).
See https://en.wikipedia.org/wiki/Ltrace for details
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 507/531
ltrace example output
# ltrace ffmpeg -f video4linux2 -video_size 544x288 -input_format mjpeg -i /dev
/video0 -pix_fmt rgb565le -f fbdev /dev/fb0
__libc_start_main([ "ffmpeg", "-f", "video4linux2", "-video_size"... ] <unfinished ...>
setvbuf(0xb6a0ec80, nil, 2, 0) = 0
av_log_set_flags(1, 0, 1, 0) = 1
strchr("f", ':') = nil
strlen("f") = 1
strncmp("f", "L", 1) = 26
strncmp("f", "h", 1) = -2
strncmp("f", "?", 1) = 39
strncmp("f", "help", 1) = -2
strncmp("f", "-help", 1) = 57
strncmp("f", "version", 1) = -16
strncmp("f", "buildconf", 1) = 4
strncmp("f", "formats", 1) = 0
strlen("formats") = 7
strncmp("f", "muxers", 1) = -7
strncmp("f", "demuxers", 1) = 2
strncmp("f", "devices", 1) = 2
strncmp("f", "codecs", 1) = 3
...
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 508/531
ltrace summary
Example summary at the end of the ltrace output (-c option)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 509/531
ftrace
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 510/531
kernelshark
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 511/531
perf
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 512/531
perf examples
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 513/531
perf GUI: hotspot
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 514/531
gprof
▶ Application-level profiler
▶ Part of binutils
▶ Requires passing gcc -pg option at build/link time
▶ Run your program normally, it automatically generates a gmon.out file when
exiting
▶ Use the gprof tool on gmon.out to extract profiling data
▶ http://sourceware.org/binutils/docs/gprof/
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 515/531
gprof example
main
100.00%
(0.09%)
$ ./test-gprof 65.88% 34.03%
$ gprof test-gprof gmon.out 1× 1×
Flat profile:
func1 func2
65.88% 34.03%
Each sample counts as 0.01 seconds. (35.31%) (34.03%)
% cumulative self self total 1× 1×
time seconds seconds calls s/call s/call name
30.57%
35.31 7.46 7.46 1 7.46 13.92 func1 1×
34.03 14.65 7.19 1 7.19 7.19 func2
30.57 21.11 6.46 1 6.46 6.46 new_func1 new_func1
0.09 21.13 0.02 main 30.57%
(30.57%)
[...] 1×
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 516/531
Embedded Linux application development
Memory debugging
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 517/531
Valgrind
https://valgrind.org/
▶ instrumentation framework for building dynamic analysis tools
• detect many memory management and threading bugs
• profile programs
▶ Supported architectures: x86, x86-64, ARMv7, ARMv8, mips32,
s390, ppc32 and ppc64
▶ Very popular tool especially for debugging memory issues
▶ Runs your program on a synthetic CPU → significant
performance impact (100 x slower on SAMA5D3!), but very
detailed instrumentation
▶ Runs on the target. Easy to build with Yocto Project or
Buildroot.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 518/531
Valgrind tools
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 519/531
Valgrind examples
▶ Memcheck
$ valgrind --leak-check=yes <program>
==19182== Invalid write of size 4
==19182== at 0x804838F: f (example.c:6)
==19182== by 0x80483AB: main (example.c:11)
==19182== Address 0x1BA45050 is 0 bytes after a block of size 40 alloc'd
==19182== at 0x1B8FF5CD: malloc (vg_replace_malloc.c:130)
==19182== by 0x8048385: f (example.c:5)
==19182== by 0x80483AB: main (example.c:11)
▶ Callgrind
$ valgrind --tool=callgrind --dump-instr=yes --simulate-cache=yes --collect-jumps=yes <program>
$ ls callgrind.out.*
callgrind.out.1234
$ callgrind_annotate callgrind.out.1234
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 520/531
Kcachegrind - Visualizing Valgrind profiling data
https://github.com/KDE/kcachegrind
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 521/531
Debugging resources
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 522/531
Practical lab - Application development and debugging
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 523/531
Useful resources
Useful resources
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 524/531
Books
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 525/531
Web sites
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 526/531
International conferences (1)
▶ Embedded Linux Conference:
• https://embeddedlinuxconference.com/
• Organized by the Linux Foundation
• Once per year, alternating North America/Europe
• Very interesting kernel and user space topics for embedded
systems developers. Many kernel and embedded project
maintainers are present.
• Presentation slides and videos freely available on
https://elinux.org/ELC_Presentations
▶ Linux Plumbers
• https://linuxplumbersconf.org
• About the low-level plumbing of Linux: kernel, audio, power
management, device management, multimedia, etc.
• Not really a conventional conference with formal presentations,
but rather a place where contributors on each topic meet, share
their progress and make plans for work ahead.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 527/531
International conferences (2)
▶ FOSDEM: https://fosdem.org
• Brussels (Belgium), February
• Community-oriented conference, free, during the
week-end
• Many developer rooms, including on low-level,
embedded and hardware topics
▶ Embedded Recipes: https://embedded-recipes.org
• Paris (France), September
• 2-day conference about all embedded Linux topics
• Well attended by known contributors
• Very affordable conference, thanks to sponsors (like
Bootlin).
▶ Most conferences are now also accessible on-line, which
makes them much more affordable.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 528/531
Last slides
Last slides
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 529/531
Last slide
Thank you!
And may the Source be with you
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 530/531
Rights to copy
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 531/531
Extra slides
Extra slides
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 1/2
Linux connectivity stack
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 2/2