Embedded Linux Qemu Slides
Embedded Linux Qemu 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/520
Rights to copy
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 2/520
Hyperlinks in the document
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 3/520
Company at a glance
▶ Engineering company created in 2004, named ”Free Electrons” until Feb. 2018.
▶ Locations: Orange, Toulouse, Lyon (France)
▶ Serving customers all around the world
▶ Head count: 12 - Only Free Software enthusiasts!
▶ Focus: Embedded Linux, Linux kernel, build systems and low level Free and Open
Source Software for embedded and real-time systems.
▶ Bootlin is often in the top 20 companies contributing to the Linux kernel.
▶ Activities: development, training, consulting, technical support.
▶ Added value: get the best of the user and development community and the
resources it offers.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 4/520
Bootlin on-line resources
https://elixir.bootlin.com
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 5/520
Generic course information
Generic course
information
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 6/520
Labs on QEMU virtual hardware
EMU
▶ Can boot the Linux kernel from U-Boot
▶ Supports mainline Linux and U-Boot
▶ Networking possible with the emulated board
▶ Emulated SD card
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 7/520
Labs proposed on another platform
After this course, you can also run all labs on the
Microchip SAMA5D3 Xplained ARM board.
In addition, you will also have real-time and NAND
flash labs!
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 8/520
Labs proposed on another platform
After this course, you can also run most labs on the
STM32MP157A-DK1 Discovery board
(https://www.st.com/en/evaluation-
tools/stm32mp157a-dk1.html)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 9/520
Participate!
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 10/520
Practical lab guidelines
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 11/520
Advise: write down your commands!
During practical labs, write down all your commands in a text file.
▶ You can save a lot of time re-using commands Lab commands
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 12/520
Cooperate!
As in the Free Software and Open Source community, cooperation during practical labs
is valuable in this training session:
▶ If you complete your labs before other people, don’t hesitate to help other people
and investigate the issues they face. The faster we progress as a group, the more
time we have to explore extra topics.
▶ Explain what you understood to other participants when needed. It also helps to
consolidate your knowledge.
▶ Don’t hesitate to report potential bugs to your instructor.
▶ Don’t hesitate to look for solutions on the Internet as well.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 13/520
Command memento sheet
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 14/520
vi basic commands
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 15/520
Introduction to Embedded Linux
Introduction to
Embedded Linux
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 16/520
Birth of Free Software
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 17/520
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 18/520
What is embedded Linux?
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 19/520
Introduction to Embedded Linux
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 20/520
Re-using components
▶ The key advantage of Linux and open-source in embedded systems is the ability
to re-use components
▶ The open-source ecosystem already provides many components for standard
features, from hardware support to network protocols, going through multimedia,
graphic, cryptographic libraries, etc.
▶ As soon as a hardware device, or a protocol, or a feature is wide-spread enough,
high chance of having open-source components that support it.
▶ Allows to quickly design and develop complicated products, based on existing
components.
▶ No-one should re-develop yet another operating system kernel, TCP/IP stack,
USB stack or another graphical toolkit library.
▶ Allows to focus on the added value of your product.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 21/520
Low cost
▶ Free software can be duplicated on as many devices as you want, free of charge.
▶ If your embedded system uses only free software, you can reduce the cost of
software licenses to zero. Even the development tools are free, unless you choose
a commercial embedded Linux edition.
▶ Of course, using Linux is not free of cost. You still need substantial learning and
engineering efforts to achieve your goals.
▶ Allows to have a higher budget for the hardware or to increase the
company’s skills and knowledge
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 22/520
Full control
▶ With open-source, you have the source code for all components in your system
▶ Allows unlimited modifications, changes, tuning, debugging, optimization, for an
unlimited period of time
▶ Without lock-in or dependency from a third-party vendor
▶ To be true, non open-source components must be avoided when the system is
designed and developed
▶ Allows to have full control over the software part of your system and
secure your investment
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 23/520
Quality
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 24/520
Eases testing of new features
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 25/520
Community support
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 26/520
Taking part into the community
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 27/520
Introduction to Embedded Linux
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 28/520
Wireless routers
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 33/520
Introduction to Embedded Linux
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 34/520
Processor and architecture (1)
The Linux kernel and most other architecture-dependent components support a wide
range of 32 and 64 bits architectures
▶ x86 and x86-64, as found on PC platforms, but also embedded systems
(multimedia, industrial)
▶ ARM, with hundreds of different SoCs (all sorts of products)
▶ RiscV, 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)
▶ SuperH (mainly set top box and multimedia applications)
▶ c6x (TI DSP architecture)
▶ Microblaze (soft-core for Xilinx FPGA)
▶ Others: ARC, m68k, Xtensa...
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 35/520
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 36/520
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.
▶ Flash storage is supported, both NAND and NOR flash, with specific filesystems
▶ Block storage including SD/MMC cards and eMMC is supported
▶ Not necessarily interesting to be too restrictive on the amount of RAM/storage:
having flexibility at this level allows to re-use as many existing components as
possible.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 37/520
Communication
▶ The Linux kernel has support for many common communication buses
▶ I2C
▶ SPI
▶ CAN
▶ 1-wire
▶ SDIO
▶ USB
▶ 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 38/520
Types of hardware platforms (1)
PocketBeagle
Image credits (Beagleboard.org):
https://beagleboard.org/pocket
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 39/520
Types of hardware platforms (2)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 40/520
Criteria for choosing the hardware
▶ Make sure the hardware you plan to use is already supported by the Linux kernel,
and has an open-source bootloader, especially the SoC you’re targeting.
▶ Having support in the official versions of the projects (kernel, bootloader) is a lot
better: quality is better, and new versions 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 41/520
Introduction to Embedded Linux
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 42/520
Host and target
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 43/520
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
▶ 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 44/520
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 training
▶ System integration
▶ Integrate all the components, bootloader, kernel, third-party libraries and
applications and in-house applications into a working system
▶ Purpose of this training
▶ Development of applications
▶ Normal Linux applications, but using specifically chosen libraries
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 45/520
Embedded Linux development environment
Embedded Linux
development
environment
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 46/520
Embedded Linux solutions
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 47/520
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, Mac OS X) will lead to trouble, and
their usage on these systems is generally not supported by community developers.
▶ 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.
:-)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 48/520
Desktop Linux distribution
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 49/520
Linux root and non-root users
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 50/520
Software packages
▶ The distribution mechanism for software in GNU/Linux is different from the one
in Windows
▶ Linux distributions provides a central and coherent way of installing, updating and
removing applications and libraries: packages
▶ Packages contains the application or library files, and associated
meta-information, such as the version and the dependencies
▶ .deb on Debian and Ubuntu, .rpm on Red Hat, Fedora, openSUSE
▶ Packages are stored in repositories, usually on HTTP or FTP servers
▶ You should only use packages from official repositories for your distribution, unless
strictly required.
▶ Note: Snap and Flatpak offer new ways of packaging applications in a
self-contained way. See https://www.atechtown.com/flatpak-vs-snap/.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 51/520
Managing software packages (1)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 52/520
Managing software packages (2)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 54/520
Serial line communication program
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 55/520
Command line tips
▶ Using the command line is mandatory for many operations needed for embedded
Linux development
▶ It is a very powerful way of interacting with the system, with which you can save
a lot of time.
▶ Some useful tips
▶ You can use several tabs in the Gnome Terminal
▶ Remember that you can use relative paths (for example: ../../linux) in addition
to absolute paths (for example: /home/user)
▶ In a shell, hit [Control] [r], then a keyword, will search through the command
history. Hit [Control] [r] again to search backwards in the history
▶ You can copy/paste paths directly from the file manager to the terminal by
drag-and-drop.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 56/520
Practical lab - Training Setup
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 57/520
Cross-compiling toolchains
Cross-compiling
toolchains
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 58/520
Cross-compiling toolchains
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 59/520
Toolchain definition (1)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 60/520
Toolchain definition (2)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 61/520
Machines in build procedures
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 62/520
Different toolchain build procedures
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 63/520
Components
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 64/520
Binutils
▶ Binutils is a set of tools to generate and manipulate binaries 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, used for 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).
▶ https://www.gnu.org/software/binutils/
▶ GPL license
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 65/520
Kernel headers (1)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 66/520
Kernel headers (2)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 67/520
Kernel headers (3)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 68/520
C/C++ compiler
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 69/520
C library
Application
▶ Interface between the applications and the POSIX-compatible
n
tio
nc lls
kernel fu
ca
BusyBox
m calls
Virtual
IPC syste
file
fu
Linux-specific
C library.
Comparing libcs by feature: https://www.etalabs.net/compare_libcs.html
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 70/520
Cross-compiling toolchains
C Libraries
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 71/520
glibc
▶ License: LGPL
▶ C 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.23: libc: 1.5 MB, libm: 492 KB,
source: https://toolchains.bootlin.com
▶ But some features not needed in embedded systems can
be configured out (merged from the old eglibc project).
▶ https://www.gnu.org/software/libc/
Image: https://bit.ly/2EzHl6m
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 72/520
uClibc-ng
▶ https://uclibc-ng.org/
▶ A continuation of the old uClibc project, license: LGPL
▶ Lightweight C 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 library supporting ARM noMMU.
▶ No guaranteed binary compatibility. May need to recompile applications when the
library configuration changes.
▶ Some glibc features may not be implemented yet (real-time, floating-point
operations...)
▶ Focus on size rather than performance
▶ Size on armv7hf, version 1.0.24: libc: 652 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 73/520
musl C library
https://www.musl-libc.org/
▶ A lightweight, fast and simple library for embedded systems
▶ Created while uClibc’s development was stalled
▶ In particular, great at making small static executables
▶ 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 distribution
(https://www.alpinelinux.org/), fitting in about 130 MB of
storage.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 74/520
glibc vs uclibc-ng vs musl - small static executables
Let’s compile and strip a hello.c program statically and compare the size
▶ With gcc 6.3, armel, musl 1.1.16:
7300 bytes
▶ With gcc 6.3, armel, uclibc-ng 1.0.22 :
67204 bytes.
▶ With gcc 6.2, armel, glibc:
492792 bytes
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 75/520
glibc vs uclibc vs musl (static)
Let’s compile and strip BusyBox 1.26.2 statically and compare the size
▶ With gcc 6.3, armel, musl 1.1.16:
183348 bytes
▶ With gcc 6.3, armel, uclibc-ng 1.0.22 :
210620 bytes.
▶ With gcc 6.2, armel, glibc:
755088 bytes
Notes:
▶ BusyBox is automatically compiled with -Os and stripped.
▶ Compiling with shared libraries will mostly eliminate size differences
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 76/520
Other smaller C libraries
▶ Several other smaller C libraries have been developed, but none of them have the
goal of allowing the compilation of large existing applications
▶ They can run only relatively simple programs, typically to make very small static
executables and run in very small root filesystems.
▶ Choices:
▶ Newlib, https://sourceware.org/newlib/
▶ Klibc, https://kernel.org/pub/linux/libs/klibc/, designed for use in an
initramfs or initrd at boot time.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 77/520
Advise for choosing the C library
▶ Advice to start developing and debugging your applications with glibc, which is
the most standard solution.
▶ Then, when everything works, if you have size constraints, try to compile your app
and then the entire filesystem with uClibc or musl.
▶ If you run into trouble, it could be because of missing features in the C library.
▶ In case you wish to make static executables, musl will be an easier choice.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 78/520
Cross-compiling toolchains
Toolchain Options
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 79/520
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, two main ABIs: OABI and EABI
▶ Nowadays everybody uses EABI
▶ On MIPS, several ABIs: o32, o64, n32, n64
▶ https://en.wikipedia.org/wiki/Application_Binary_Interface
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 80/520
Floating point support
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 81/520
CPU optimization flags
Obtaining a Toolchain
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 83/520
Building a toolchain manually
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 84/520
Get a pre-compiled toolchain
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 85/520
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.
▶ 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 86/520
Toolchain building utilities (2)
Crosstool-ng
▶ Rewrite of the older Crosstool, with a
menuconfig-like configuration system
▶ Feature-full: supports uClibc, glibc, musl,
bionic (Android’s C library), 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 87/520
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.
▶ https://buildroot.org
▶ PTXdist
▶ Makefile-based, maintained mainly by Pengutronix. It only supports uClibc and glibc
(version 2020.05 status)
▶ https://www.ptxdist.org/
▶ OpenEmbedded / Yocto Project
▶ A featureful, but more complicated build system
▶ http://www.openembedded.org/
▶ https://www.yoctoproject.org/
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 88/520
Crosstool-NG: installation and usage
▶ Installation of Crosstool-NG can be done system-wide, or just locally in the source
directory. For local installation:
./configure --enable-local
make
make install
▶ Some sample configurations for various architectures are available in samples, they
can be listed using
./ct-ng list-samples
▶ To load a sample configuration
./ct-ng <sample-name>
▶ To adjust the configuration
./ct-ng menuconfig
▶ To build the toolchain
./ct-ng build
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 89/520
Toolchain contents
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 90/520
Practical lab - Using Crosstool-NG
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 91/520
Bootloaders
Bootloaders
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 92/520
Bootloaders
Boot Sequence
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 93/520
Bootloaders
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 94/520
Bootloaders on BIOS-based x86 (1)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 95/520
Bootloaders on x86 (2)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 96/520
Booting on embedded CPUs: case 1
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 97/520
Booting on embedded CPUs: case 2
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 98/520
Booting on Microchip ARM SAMA5D3
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 99/520
Booting on ARM TI OMAP2+ / AM33xx
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 100/520
Booting on Marvell SoCs
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 101/520
Generic bootloaders for embedded CPUs
▶ We will focus on the generic part, the main bootloader, offering the most
important features.
▶ There are several open-source generic bootloaders.
Here are the most popular ones:
▶ U-Boot, the universal bootloader by Denx
The most used on ARM, also used on PPC, MIPS, x86, m68k, RiscV, etc. The
de-facto standard nowadays. We will study it in detail.
https://www.denx.de/wiki/U-Boot
▶ Barebox, an architecture-neutral bootloader, written as a successor of U-Boot. It
doesn’t have as much hardware support as U-Boot yet. U-Boot has improved quite a
lot thanks to this competitor.
https://www.barebox.org
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 102/520
Bootloaders
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 103/520
U-Boot
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 104/520
U-Boot configuration
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 105/520
U-Boot configuration file
CHIP_defconfig
CONFIG_ARM=y
CONFIG_ARCH_SUNXI=y
CONFIG_MACH_SUN5I=y
CONFIG_DRAM_TIMINGS_DDR3_800E_1066G_1333J=y
# CONFIG_MMC is not set
CONFIG_USB0_VBUS_PIN="PB10"
CONFIG_VIDEO_COMPOSITE=y
CONFIG_DEFAULT_DEVICE_TREE="sun5i-r8-chip"
CONFIG_SPL=y
CONFIG_SYS_EXTRA_OPTIONS="CONS_INDEX=2"
# CONFIG_CMD_IMLS is not set
CONFIG_CMD_DFU=y
CONFIG_CMD_USB_MASS_STORAGE=y
CONFIG_AXP_ALDO3_VOLT=3300
CONFIG_AXP_ALDO4_VOLT=3300
CONFIG_USB_MUSB_GADGET=y
CONFIG_USB_GADGET=y
CONFIG_USB_GADGET_DOWNLOAD=y
CONFIG_G_DNL_MANUFACTURER="Allwinner Technology"
CONFIG_G_DNL_VENDOR_NUM=0x1f3a
CONFIG_G_DNL_PRODUCT_NUM=0x1010
CONFIG_USB_EHCI_HCD=y
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 106/520
Configuring and compiling U-Boot
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 107/520
Installing U-Boot
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 108/520
U-boot prompt
▶ Connect the target to the host through a serial console.
▶ Power-up the board. On the serial console, you will see something like:
U-Boot 2020.04 (May 26 2020 - 16:05:43 +0200)
CPU: SAMA5D36
Crystal frequency: 12 MHz
CPU clock : 528 MHz
Master clock : 132 MHz
DRAM: 256 MiB
NAND: 256 MiB
MMC: Atmel mci: 0, Atmel mci: 1
Loading Environment from NAND... OK
In: serial@ffffee00
Out: serial@ffffee00
Err: serial@ffffee00
Net: eth0: ethernet@f0028000
Error: ethernet@f802c000 address not set.
▶ The U-Boot shell offers a set of commands. We will study the most important
ones, see the documentation for a complete reference or the help command.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 109/520
Information commands (1)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 110/520
Information commands (2)
Version details
=> version
U-Boot 2020.04 (May 26 2020 - 16:05:43 +0200)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 111/520
Important commands (1)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 112/520
Important commands (2)
▶ loadb, loads, loady, load a file from the serial line to RAM
▶ usb, to initialize and control the USB subsystem, mainly used for USB storage
devices such as USB keys
▶ mmc, to initialize and control the MMC subsystem, used for SD and microSD cards
▶ nand, to erase, read and write contents to NAND flash
▶ erase, protect, cp, to erase, modify protection and write to NOR flash
▶ md, displays memory contents. Can be useful to check the contents loaded in
memory, or to look at hardware registers.
▶ mm, modifies memory contents. Can be useful to modify directly hardware
registers, for testing purposes.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 113/520
Environment variables: principle
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 114/520
Environment variables commands (2)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 115/520
Environment variables commands - Example
u-boot # 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
u-boot # printenv serverip
serverip=10.0.0.1
u-boot # setenv serverip 10.0.0.100
u-boot # saveenv
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 116/520
Important U-Boot env variables
▶ bootcmd, specifies the commands that U-Boot will automatically execute at boot
time after a configurable delay (bootdelay), if the process is not interrupted
▶ bootargs, contains the arguments passed to the Linux kernel, covered later
▶ serverip, the IP address of the server that U-Boot will contact for network
related commands
▶ ipaddr, the IP address that U-Boot will use
▶ netmask, the network mask to contact the server
▶ ethaddr, the MAC address, can only be set once
▶ autostart, if set to yes, U-Boot automatically starts an image after loading it in
memory (tftp, fatload...)
▶ filesize, the size of the latest copy to memory (from tftp, fatload,
nand read...)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 117/520
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}
▶ Example
▶ 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 118/520
Transferring files to the target
▶ U-Boot is mostly used to load and boot a kernel image, but it also allows to
change the kernel image and the root filesystem stored in flash.
▶ Files must be exchanged between the target and the development workstation.
This is possible:
▶ Through the network if the target has an Ethernet connection, and U-Boot contains
a driver for the Ethernet chip. This is the fastest and most efficient solution.
▶ Through a USB key, if U-Boot supports the USB controller of your platform
▶ Through a SD or microSD card, if U-Boot supports the MMC controller of your
platform
▶ Through the serial port (loadb, loadx or loady command)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 119/520
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 /var/lib/tftpboot (in Ubuntu 18.04) or in /srv/tftp (in Ubuntu
20.04) 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 and serverip environment variables
▶ Use tftp <address> <filename> to load a file
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 120/520
Practical lab - U-Boot
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 121/520
Linux kernel introduction
Linux kernel
introduction
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 122/520
Linux kernel introduction
Linux features
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 123/520
History
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 124/520
Linux kernel key features
▶ Portability and hardware support. ▶ Security. It can’t hide its flaws. Its
Runs on most architectures. code is reviewed by many experts.
▶ Scalability. Can run on super ▶ Stability and reliability.
computers as well as on tiny devices ▶ Modularity. Can include only what a
(4 MB of RAM is enough). system needs even at run time.
▶ Compliance to standards and ▶ Easy to program. You can learn from
interoperability. existing code. Many useful resources
▶ Exhaustive networking support. on the net.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 125/520
Linux kernel in the system
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 126/520
Linux kernel main roles
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 127/520
System calls
▶ 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 set of devices and buses. Information about these
devices.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 129/520
Inside the Linux kernel
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 130/520
Linux license
▶ The whole Linux sources are Free Software released under the GNU General
Public License version 2 (GPL v2).
▶ For the Linux kernel, this basically implies that:
▶ When you receive or buy a device with Linux on it, you should receive the Linux
sources, with the right to study, modify and redistribute them.
▶ When you produce Linux based devices, you must release the sources to the
recipient, with the same rights, with no restriction.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 131/520
Supported hardware architectures
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 132/520
Linux kernel introduction
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 133/520
Linux versioning scheme
▶ Until 2003, there was a new stable release branch of Linux every 2 or 3 years (2.0,
2.2, 2.4). New development branches took 2-3 years to become stable (too slow!).
▶ Since 2003, there is a new stable 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)
▶ Version 5.0 was released in Mar. 2019.
▶ 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: 5.0.1, 5.0.2, etc.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 134/520
Linux development model
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 135/520
Need for long term support (1)
▶ Issue: bug and security fixes only released for most recent stable kernel versions.
Only LTS (Long Term Support) releases are supported for up to 6 years.
▶ Example at Google: starting from Android O (2017), all new Android devices will
have to run such an LTS kernel.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 136/520
Need for long term support (2)
▶ You could also get long term support from a commercial embedded Linux provider.
▶ ”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 selected LTS versions (starting with 4.4) much longer (> 10 years).
See https://bit.ly/2hy1QYC.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 137/520
What’s new in each Linux release? (1)
The official list of changes for each Linux release is just a huge list of individual
patches!
commit aa6e52a35d388e730f4df0ec2ec48294590cc459
Author: Thomas Petazzoni <[email protected]>
Date: Wed Jul 13 11:29:17 2011 +0200
Very difficult to find out the key changes and to get the global picture out of individual
changes.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 138/520
What’s new in each Linux release? (2)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 139/520
Linux kernel introduction
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 140/520
Location of kernel sources
▶ The official (mainline) versions of the Linux kernel, as released by Linus Torvalds,
are available at https://kernel.org
▶ These versions follow the development model of the kernel
▶ However, they may not contain the latest development from a specific area yet.
Some features in development might not be ready for mainline inclusion yet.
▶ Many chip vendors supply their own kernel sources
▶ Focusing on hardware support first
▶ Can have a very important delta with mainline Linux
▶ Useful only when mainline hasn’t caught up yet.
▶ Many kernel sub-communities maintain their own kernel, with usually newer but
less stable features
▶ Architecture communities (ARM, MIPS, PowerPC, etc.), device drivers communities
(I2C, SPI, USB, PCI, network, etc.), other communities (real-time, etc.)
▶ No official releases, only meant for sharing work and contributing to the mainline
version.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 141/520
Getting Linux sources
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 142/520
Working with git: SSD storage needed for serious work
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 143/520
Linux kernel size (1)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 144/520
Linux kernel size (2)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 145/520
Getting Linux sources
▶ Full tarballs
▶ Contain the complete kernel sources: long to download and uncompress, but must
be done at least once
▶ Example:
https://kernel.org/pub/linux/kernel/v4.x/linux-4.20.13.tar.xz
▶ Extract command:
tar xf linux-4.20.13.tar.xz
▶ Incremental patches between versions
▶ It assumes you already have a base version and you apply the correct patches in the
right order. Quick to download and apply
▶ Examples:
https://kernel.org/pub/linux/kernel/v4.x/patch-4.20.xz
(4.19 to 4.20)
https://kernel.org/pub/linux/kernel/v4.x/patch-4.20.13.xz
(4.20 to 4.20.13)
▶ All previous kernel versions are available in
https://kernel.org/pub/linux/kernel/
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 146/520
Patch
▶ A patch is the difference between two source trees
▶ Computed with the diff tool, or with more elaborate version control systems
▶ They are very common in the open-source community.
See https://en.wikipedia.org/wiki/Diff
▶ Excerpt from a patch:
diff -Nru a/Makefile b/Makefile
--- a/Makefile 2005-03-04 09:27:15 -08:00
+++ b/Makefile 2005-03-04 09:27:15 -08:00
@@ -1,7 +1,7 @@
VERSION = 2
PATCHLEVEL = 6
SUBLEVEL = 11
-EXTRAVERSION =
+EXTRAVERSION = .1
NAME=Woozy Numbat
# *DOCUMENTATION*
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 147/520
Contents of a patch
▶ One section per modified file, starting with a header
diff -Nru a/Makefile b/Makefile
--- a/Makefile 2005-03-04 09:27:15 -08:00
+++ b/Makefile 2005-03-04 09:27:15 -08:00
▶ One sub-section (hunk) per modified part of the file, starting with a header with the
starting line number and the number of lines the change hunk applies to
@@ -1,7 +1,7 @@
▶ Three lines of context before the change
VERSION = 2
PATCHLEVEL = 6
SUBLEVEL = 11
▶ The change itself
-EXTRAVERSION =
+EXTRAVERSION = .1
▶ Three lines of context after the change
NAME=Woozy Numbat
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 148/520
Using the patch command
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 149/520
Applying a Linux patch
▶ Two types of Linux patches:
▶ Either to be applied to the previous stable version
(from x.<y-1> to x.y)
▶ Or implementing fixes to the current stable version
(from x.y to x.y.z)
▶ Can be downloaded in gzip or xz (much smaller) compressed files.
▶ Always produced for n=1
(that’s what everybody does... do it too!)
▶ Need to run the patch command inside the toplevel kernel source directory
▶ Linux patch command line example:
cd linux-4.19
xzcat ../patch-4.20.xz | patch -p1
xzcat ../patch-4.20.13.xz | patch -p1
cd ..; mv linux-4.19 linux-4.20.13
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 150/520
Practical lab - Kernel sources
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 151/520
Linux kernel introduction
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 152/520
Linux kernel introduction
Kernel configuration
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 153/520
Kernel configuration
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 154/520
Specifying the target architecture
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 155/520
Kernel configuration and build system
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 156/520
Kernel configuration details
▶ The configuration is stored in the .config file at the root of kernel sources
▶ Simple text file, CONFIG_PARAM=value (included by the kernel Makefile)
▶ As options have dependencies, typically never edited by hand, but through
graphical or text interfaces:
▶ make xconfig, make gconfig (graphical)
▶ make menuconfig, make nconfig (text)
▶ You can switch from one to another, they all load/save the same .config file, and
show the same set of options
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 157/520
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, usually available in
/boot:
cp /boot/config-`uname -r` .config
▶ Embedded platform case:
▶ Default configuration files are available, usually for each CPU family.
▶ They are stored in arch/<arch>/configs/, and are just minimal .config files (only
settings different from default ones).
▶ Run make help to find if one is available for your platform
▶ To load a default configuration file, just run
make cpu_defconfig
▶ This will overwrite your existing .config file!
Now, you can make configuration changes (make menuconfig...).
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 158/520
Create your own default configuration
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 159/520
Kernel 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 included 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 160/520
Kernel option types
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 161/520
Kernel option dependencies
There are dependencies between kernel options
▶ For example, enabling a network driver requires the network stack to be enabled
▶ Two types of dependencies:
▶ depends on dependencies. In this case,
option B that depends on option A is not
visible until option A is enabled
▶ select dependencies. In this case, with
option B depending on option A, when
option A is enabled, option B is
automatically enabled. In particular, such
dependencies are used to declare what
features a hardware architecture supports.
▶ With the Show All Options option, make xconfig allows to see all options, even
the ones that cannot be selected because of missing dependencies. Values for
dependencies are shown.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 162/520
make xconfig
make xconfig
▶ The most common graphical interface to configure the kernel.
▶ File browser: easier to load configuration files
▶ Search interface to look for parameters
▶ Required Debian / Ubuntu packages: qt5-default
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 163/520
make xconfig screenshot
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 164/520
make xconfig search interface
Looks for a keyword in the parameter name (shortcut: [Ctrl] + [f]).
Allows to set values to found parameters.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 165/520
Kernel configuration options
Driver options
CONFIG_JOLIET=y
CONFIG_ZISOFS=y
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 166/520
Corresponding .config file excerpt
Options are grouped by sections and are prefixed with CONFIG_.
#
# CD-ROM/DVD Filesystems
#
CONFIG_ISO9660_FS=m
CONFIG_JOLIET=y
CONFIG_ZISOFS=y
CONFIG_UDF_FS=y
CONFIG_UDF_NLS=y
#
# DOS/FAT/NT Filesystems
#
# CONFIG_MSDOS_FS is not set
# CONFIG_VFAT_FS is not set
CONFIG_NTFS_FS=m
# CONFIG_NTFS_DEBUG is not set
CONFIG_NTFS_RW=y
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 167/520
make gconfig
make gconfig
▶ GTK based graphical configuration
interface. Functionality similar to that
of make xconfig.
▶ Just lacking a search functionality.
▶ Required Debian packages:
libglade2-dev
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 168/520
make menuconfig
make menuconfig
▶ Useful when no graphics are available.
Pretty convenient too!
▶ Same interface found in other tools:
BusyBox, Buildroot...
▶ Required Debian packages:
libncurses-dev
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 169/520
make nconfig
make nconfig
▶ A newer, similar text interface
▶ More user friendly (for example, easier
to access help information).
▶ However, lacking the shortcuts that
menuconfig offers in search results.
Therefore, much less convenient than
menuconfig.
▶ Required Debian packages:
libncurses-dev
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 170/520
make oldconfig
make oldconfig
▶ Needed very often!
▶ 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 171/520
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 interfaces of the kernel (xconfig, menuconfig,
oldconfig...) keep this .config.old backup copy.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 172/520
Linux kernel introduction
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 173/520
Choose a compiler
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 174/520
Specifying ARCH and CROSS_COMPILE
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 175/520
Kernel compilation
▶ make
▶ In the main kernel source directory!
▶ Remember to run multiple jobs in parallel if you have multiple CPU cores. Example:
make -j 8
▶ No need to run as root!
▶ Generates
▶ vmlinux, the raw uncompressed kernel image, in the ELF format, useful for
debugging purposes, but cannot be booted
▶ arch/<arch>/boot/*Image, the final, usually compressed, kernel image that can be
booted
▶ bzImage for x86, zImage for ARM, vmlinux.bin.gz for ARC, etc.
▶ arch/<arch>/boot/dts/*.dtb, compiled Device Tree files (on some architectures)
▶ All kernel modules, spread over the kernel source tree, as .ko (Kernel Object) files.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 176/520
Kernel installation: native case
▶ make install
▶ Does the installation for the host system by default, so needs to be run as root.
▶ Installs
▶ /boot/vmlinuz-<version>
Compressed kernel image. Same as the one in arch/<arch>/boot
▶ /boot/System.map-<version>
Stores kernel symbol addresses for debugging purposes (obsolete: such information is
usually stored in the kernel itself)
▶ /boot/config-<version>
Kernel configuration for this version
▶ In GNU/Linux distributions, typically re-runs the bootloader configuration utility
to make the new kernel available at the next boot.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 177/520
Kernel installation: embedded case
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 178/520
Module installation: native case
▶ make modules_install
▶ Does the installation for the host system by default, so needs to be run as root
▶ Installs all modules in /lib/modules/<version>/
▶ kernel/
Module .ko (Kernel Object) files, in the same directory structure as in the sources.
▶ modules.alias, modules.aliases.bin
Aliases for module loading utilities. Used to find drivers for devices. Example line:
alias usb:v066Bp20F9d*dc*dsc*dp*ic*isc*ip*in* asix
▶ modules.dep, modules.dep.bin
Module dependencies
▶ modules.symbols, modules.symbols.bin
Tells which module a given symbol belongs to.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 179/520
Module installation: embedded case
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 180/520
Kernel cleanup targets
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 181/520
Kernel building overview
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 182/520
Linux kernel introduction
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 183/520
Device Tree (DT)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 184/520
Customize your board device tree!
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 185/520
Booting with U-Boot
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 186/520
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
▶ Many more exist. The most important ones are documented in
admin-guide/kernel-parameters in kernel documentation.
▶ This kernel command line can be, in order of priority (highest to lowest):
▶ Passed by the bootloader. In U-Boot, the contents of the bootargs environment
variable is automatically passed to the kernel.
▶ Specified in the Device Tree (for architectures which use it)
▶ Built into the kernel, using the CONFIG_CMDLINE option.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 187/520
Practical lab - Kernel cross-compiling
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 188/520
Linux kernel introduction
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 189/520
Advantages of modules
▶ Modules make it easy to develop drivers without rebooting: load, test, unload,
rebuild, load...
▶ Useful to keep the kernel image size to the minimum (essential in GNU/Linux
distributions for PCs).
▶ Also useful to reduce boot time: you don’t spend time initializing devices and
kernel features that you only need later.
▶ Caution: once loaded, have full control and privileges in the system. No particular
protection. That’s why only the root user can load and unload modules.
▶ To increase security, possibility to allow only signed modules, or to disable module
support entirely.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 190/520
Module dependencies
▶ Some kernel modules can depend on other modules, which need to be loaded first.
▶ Example: the ubifs module depends on the ubi and mtd modules.
▶ Dependencies are described both in
/lib/modules/<kernel-version>/modules.dep and in
/lib/modules/<kernel-version>/modules.dep.bin (binary hashed format)
These files are generated when you run make modules_install.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 191/520
Kernel log
When a new module is loaded, related information is available in the kernel log.
▶ The kernel keeps its messages in a circular buffer (so that it doesn’t consume
more memory with many messages)
▶ Kernel log messages are available through the dmesg command (diagnostic
message)
▶ Kernel log messages are also displayed in the system console (console messages
can be filtered by level using the loglevel kernel command line parameter, or
completely disabled with the quiet parameter). Example:
console=ttyS0 root=/dev/mmcblk0p2 loglevel=5
▶ Note that you can write to the kernel log from user space too:
echo "<n>Debug info" > /dev/kmsg
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 192/520
Module utilities (1)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 193/520
Understanding module loading issues
▶ When loading a module fails, insmod often doesn’t give you enough details!
▶ Details are often available in the kernel log.
▶ Example:
$ sudo insmod ./intr_monitor.ko
insmod: error inserting './intr_monitor.ko': -1 Device or resource busy
$ dmesg
[17549774.552000] Failed to register handler for irq channel 2
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 194/520
Module utilities (2)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 195/520
Module utilities (3)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 196/520
Passing parameters to modules
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 197/520
Check module parameter values
How to find/edit the current values for the parameters of a loaded module?
▶ Check /sys/module/<name>/parameters.
▶ There is one file per parameter, containing the parameter value.
▶ Also possible to change parameter values if these files have write permissions
(depends on the module code).
▶ Example:
echo 0 > /sys/module/usb_storage/parameters/delay_use
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 198/520
Useful reading
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 199/520
Linux Root Filesystem
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 200/520
Linux Root Filesystem
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 201/520
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 reflects the contents of the storage device
▶ 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 202/520
Filesystems (2)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 203/520
mount / umount
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 204/520
Root filesystem
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 205/520
Location of the root filesystem
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 206/520
Mounting rootfs from storage devices
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 207/520
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.
Much faster than through the serial port.
▶ 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 208/520
Mounting rootfs over the network (2)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 209/520
Mounting rootfs over the network (3)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 210/520
Mounting rootfs over the network (4)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 211/520
rootfs in memory: initramfs (1)
▶ It is also possible to have the root filesystem integrated into the kernel image
▶ It is therefore loaded into memory together with the kernel
▶ This mechanism is called initramfs
▶ It integrates a compressed archive of the filesystem into the kernel image
▶ Variant: the compressed archive can also be loaded separately by the bootloader.
▶ It is useful for two cases
▶ Fast booting of very small root filesystems. As the filesystem is completely loaded at
boot time, application startup is very fast.
▶ As an intermediate step before switching to a real root filesystem, located on devices
for which drivers not part of the kernel image are needed (storage drivers, filesystem
drivers, network drivers). This is always used on the kernel of desktop/server
distributions to keep the kernel image size reasonable.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 212/520
rootfs in memory: initramfs (2)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 213/520
rootfs in memory: initramfs (3)
▶ The contents of an initramfs are defined at the kernel configuration level, with the
CONFIG_INITRAMFS_SOURCE option
▶ Can be the path to a directory containing the root filesystem contents
▶ Can be the path to a cpio archive
▶ Can be a text file describing the contents of the initramfs
(see documentation for details)
▶ The kernel build process will automatically take the contents of the
CONFIG_INITRAMFS_SOURCE option and integrate the root filesystem into the
kernel image
▶ Details (in kernel sources):
Documentation/filesystems/ramfs-rootfs-initramfs.txt
Documentation/early-userspace/README
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 214/520
Linux Root Filesystem
Contents
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 215/520
Root filesystem organization
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 216/520
Important directories (1)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 217/520
Important directories (2)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 218/520
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, ifconfig, cp, bash, etc.
▶ /lib contains the C library and sometimes a few other basic libraries
▶ All other programs and libraries are in /usr
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 219/520
Linux Root Filesystem
Device Files
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 220/520
Devices
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 221/520
Types of 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 222/520
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 223/520
Device files examples
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 224/520
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
▶ Needed 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 and contains all the
devices known to the kernel. The CONFIG_DEVTMPFS_MOUNT kernel configuration
option makes the kernel mount it automatically at boot time, except when
booting on an initramfs.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 225/520
Linux Root Filesystem
Pseudo Filesystems
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 226/520
proc virtual filesystem
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 227/520
proc contents
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 228/520
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.
▶ 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 229/520
Linux Root Filesystem
Minimal filesystem
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 230/520
Basic applications
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 231/520
Overall booting process
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 232/520
Overall booting process with initramfs
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 233/520
Busybox
Busybox
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 234/520
Why Busybox?
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 235/520
General purpose toolbox: BusyBox
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 236/520
BusyBox commands!
[, [[, acpid, add-shell, addgroup, adduser, adjtimex, ar, arch, arp, arping, awk, base64, basename, bbconfig, bc, beep,
blkdiscard, blkid, blockdev, bootchartd, brctl, bunzip2, busybox, bzcat, bzip2, cal, cat, chat, chattr, chcon, 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, 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, flash_eraseall, flash_lock,
flash_unlock, flashcp, flock, fold, free, freeramdisk, fsck, fsck.minix, fsfreeze, fstrim, fsync, ftpd, ftpget, ftpput, fuser,
getenforce, getopt, getsebool, getty, grep, groups, gunzip, gzip, halt, hd, hdparm, head, hexdump, hexedit, hostid, hostname,
httpd, hush, hwclock, 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, load_policy, loadfont, loadkmap, logger, login, logname, logread, losetup, lpd, lpq, lpr, ls,
lsattr, lsmod, lsof, lspci, lsscsi, lsusb, lzcat, lzma, lzop, lzopcat, makedevs, makemime, man, matchpathcon, md5sum, mdev, mesg,
microcom, minips, mkdir, mkdosfs, mke2fs, mkfifo, mkfs.ext2, mkfs.minix, mkfs.reiser, mkfs.vfat, mknod, mkpasswd, mkswap, mktemp,
modinfo, modprobe, more, mount, mountpoint, mpstat, mt, mv, nameif, nanddump, nandwrite, nbd-
client, nc, netcat, 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, printenv, printf,
ps, pscan, pstree, pwd, pwdx, raidautorun, rdate, rdev, readahead, readlink, readprofile, realpath, reboot, reformime, remove-
shell, renice, reset, resize, restorecon, resume, rev, rfkill, rm, rmdir, rmmod, route, rpm, rpm2cpio, rtcwake, run-init, run-
parts, runcon, runlevel, runsv, runsvdir, rx, script, scriptreplay, sed, selinuxenabled, sendmail, seq, sestatus, setarch,
setconsole, setenforce, setfattr, setfiles, setfont, setkeycodes, setlogcons, setpriv, setsebool, 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, tune2fs, ubiattach, ubidetach, ubimkvol, ubirename, ubirmvol, ubirsvol,
ubiupdatevol, udhcpc, udhcpd, udpsvd, uevent, umount, uname, uncompress, unexpand, uniq, unit, unix2dos, unlink, unlzma, unlzop,
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 237/520
Applet highlight: Busybox init
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 238/520
Applet highlight - BusyBox vi
▶ If you are using BusyBox, adding vi support only adds 20K. (built with shared
libraries, using uClibc).
▶ You can select which exact features to compile in.
▶ Users hardly realize that they are using a lightweight vi version!
▶ Tip: you can learn vi on the desktop, by running the vimtutor command.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 239/520
Configuring BusyBox
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 240/520
BusyBox make xconfig
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 241/520
Compiling BusyBox
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 242/520
Practical lab - A tiny embedded system
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 243/520
Block filesystems
Block filesystems
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 244/520
Block filesystems
Block devices
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 245/520
Block vs. flash
▶ Storage devices are classified in two main types: block devices and 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 card, 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 246/520
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 247/520
Partitioning
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 248/520
Transfering data to a block device
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 249/520
Block filesystems
Available filesystems
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 250/520
Standard Linux filesystem format: ext2, ext3, ext4
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 251/520
Journaled filesystems
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 252/520
Filesystem recovery after crashes
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 253/520
Other journaled Linux/UNIX filesystems
▶ btrfs, intended to become the next standard filesystem for Linux. Integrates
numerous features: data checksuming, integrated volume management,
snapshots, etc.
▶ XFS, high-performance filesystem inherited from SGI IRIX, still actively developed.
▶ JFS, inherited from IBM AIX. No longer actively developed, provided mainly for
compatibility.
▶ reiserFS, used to be a popular filesystem, but its latest version Reiser4 was
never merged upstream.
All those filesystems provide the necessary functionalities for Linux systems: symbolic
links, permissions, ownership, device files, etc.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 254/520
F2FS: filesystem for flash-based storage
https://en.wikipedia.org/wiki/F2FS
▶ Filesystem that takes into account the characteristics of flash-based storage:
eMMC, SD cards, SSD, etc.
▶ Developed and contributed by Samsung
▶ Available in the mainline Linux kernel
▶ For optimal results, need a number of details about the storage internal behavior
which may not easy to get
▶ Benchmarks: best performer on flash devices most of the time:
See https://lwn.net/Articles/520003/
▶ Technical details: https://lwn.net/Articles/518988/
▶ Not as widely used as ext3,4, even on flash-based storage.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 255/520
Squashfs: read-only filesystem
▶ Read-only, compressed filesystem for block devices. Fine for parts of a filesystem
which can be read-only (kernel, binaries...)
▶ Great compression rate, which generally brings improved read performance
▶ Used in most live CDs and live USB distributions
▶ Supports several compression algorithms (LZO, XZ, etc.)
▶ Benchmarks: roughly 3 times smaller than ext3, and 2-4 times faster
(https://elinux.org/Squash_Fs_Comparisons)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 256/520
Compatibility filesystems
Linux also supports several other filesystem formats, mainly to be interopable with
other operating systems:
▶ vfat 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 (exfat).
▶ ntfs for compatibility with the NTFS filesystem used on Windows
▶ hfs for compatibility with the HFS filesystem used on Mac OS
▶ iso9660, the filesystem format used on CD-ROMs, obviously a read-only
filesystem
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 257/520
tmpfs: filesystem in RAM
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 258/520
Block filesystems
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 259/520
Creating ext2/ext3/ext4 filesystems
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 260/520
Mounting filesystem images
▶ Once a filesystem image has been created, one can access and modifies its
contents from the development workstation, using the loop mechanism
▶ Example:
genext2fs -d rootfs/ rootfs.img
mkdir /tmp/tst
mount -t ext2 -o loop rootfs.img /tmp/tst
▶ In the /tmp/tst 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.
▶ Do not forget to run umount before using the filesystem image!
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 261/520
Creating squashfs filesystems
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 262/520
Mixing read-only and read-write filesystems
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 263/520
Issues with flash-based block storage
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 264/520
Practical lab - Block filesystems
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 265/520
Flash storage and filesystems
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 266/520
Block devices vs 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)
▶ Flash devices:
▶ Allow for random data access too
▶ Require special care before writing on the media (erasing the region you are about to
write on)
▶ Erase, write and read operation might not use the same block size
▶ Reliability depends on the flash technology
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 267/520
NAND flash chips: how they work?
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 268/520
NAND flash storage: organization
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 269/520
NAND flash storage: constraints
▶ Reliability
▶ Far less reliable than NOR flash
▶ Reliability depends on the NAND flash technology (SLC, MLC)
▶ Require additional mechanisms to recover from bit flips: ECC (Error Correcting
Code)
▶ ECC information stored in the OOB (Out-of-band area)
▶ Lifetime
▶ Short lifetime compared to other storage media
▶ Lifetime depends on the NAND flash technology (SLC, MLC): between 1000000 and
1000 erase cycles per block
▶ Wear leveling mechanisms are required
▶ Bad block detection/handling required too
▶ Despite the number of constraints brought by NAND they are widely used in
embedded systems for several reasons:
▶ Cheaper than other flash technologies
▶ Provide high capacity storage
▶ Provide good performance (both in read and write access)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 270/520
NAND flash: ECC
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 271/520
The MTD subsystem (1)
▶ MTD stands for Memory Technology Devices
▶ Generic subsystem in Linux dealing with all types of storage media that are not
fitting in the block subsystem
▶ Supported media types: RAM, ROM, NOR flash, NAND flash, Dataflash
▶ Independent of the communication interface (drivers available for parallel, SPI,
direct memory mapping, ...)
▶ Abstract storage media characteristics and provide a simple API to access MTD
devices
▶ MTD device characteristics exposed to users:
▶ erasesize: minimum erase size unit
▶ writesize: minimum write size unit
▶ oobsize: extra size to store metadata or ECC data
▶ size: device size
▶ flags: information about device type and capabilities
▶ Various kinds of MTD ”users” in the kernel: file-systems, block device emulation
layers, user space interfaces...
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 272/520
The MTD subsystem (2)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 273/520
MTD partitioning
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 274/520
Linux: definition of MTD partitions
The Device Tree is the standard place to define default MTD partitions for platforms with
Device Tree support.
Example from arch/arm/boot/dts/omap3-overo-base.dtsi:
nand@0,0 {
linux,mtd-name= "micron,mt29c4g96maz";
[...]
ti,nand-ecc-opt = "bch8"
[...]
partition@0 {
label = "SPL";
reg = <0 0x80000>; /* 512KiB */
};
partition@80000 {
label = "U-Boot";
reg = <0x80000 0x1C0000>; /* 1792KiB */
};
partition@1c0000 {
label = "Environment";
reg = <0x240000 0x40000>; /* 256KiB */
};
[...]
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 275/520
U-Boot: defining MTD partitions (1)
▶ U-Boot allows to define MTD partitions on flash devices, using the same syntax
as Linux for declaring them.
▶ Sharing definitions allows to eliminate the risk of mismatches between Linux and
U-Boot.
▶ Named partitions are also easier to use, and much less error prone than using
offsets.
▶ Use flash specific commands (detailed soon), and pass partition names instead of
numerical offsets
▶ Example: nand erase.part <partname>
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 276/520
U-Boot: defining MTD partitions (2)
▶ Example:
setenv mtdids nand0=omap2-nand.0
setenv mtdparts mtdparts=omap2-nand.0:512k(XLoader)ro,1536k(UBoot)ro,512k(Env),4m(Kernel),-(Root)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 277/520
U-Boot: defining MTD partitions (3)
mtdids associates a U-Boot flash device name to a Linux flash device name:
setenv mtdids <devid>=<mtdid>[,<devid>=<mtdid>]
That’s required because the Linux name is used in partition definitions.
▶ devid: U-Boot device identifier (from nand info or flinfo)
▶ mtdid: Linux mtd identifier. Displayed when booting the Linux kernel:
NAND device: Manufacturer ID: 0x2c, Chip ID: 0xbc (Micron NAND 512MiB 1,8V 16-bit)
Creating 5 MTD partitions on "omap2-nand.0":
0x000000000000-0x000000080000 : "X-Loader"
0x000000080000-0x000000200000 : "U-Boot"
0x000000200000-0x000000280000 : "Environment"
0x000000280000-0x000000580000 : "Kernel"
0x000000580000-0x000020000000 : "File System"
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 278/520
U-Boot: defining MTD partitions (4)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 279/520
U-Boot: sharing partition definitions with Linux
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 280/520
U-Boot: manipulating NAND devices
U-Boot provides a set of commands to manipulate NAND devices, grouped under the
nand command
▶ nand info
Show available NAND devices and characteristics
▶ nand device [dev]
Select or display the active NAND device
▶ nand read[.option] <addr> <offset|partname> <size>
Read data from NAND
▶ nand write[.option] <addr> <offset|partname> <size>
Write data on NAND
▶ Use nand write.trimffs to avoid writing empty pages (those filled with 0xff)
▶ nand erase <offset> <size>
Erase a NAND region
▶ nand erase.part <partname>
Erase a NAND partition
▶ More commands for debugging purposes
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 281/520
Linux: MTD devices interface with user space
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 282/520
Linux: user space flash management tools
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 283/520
Flash wear leveling (1)
▶ 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 284/520
Flash wear leveling (2)
Flash users should also take the limited lifetime of flash devices into account by taking
additional precautions
▶ Do not use your flash storage as swap area (rare in embedded systems anyway)
▶ Mount your filesystems as read-only whenever possible.
▶ Keep volatile files in RAM (tmpfs)
▶ Don’t use the sync mount option (commits writes immediately). Use the fsync()
system call for per-file synchronization.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 285/520
Flash file-systems
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 286/520
Legacy flash filesystems: JFFS2
Standard file
API
▶ Supports on the fly compression
▶ Wear leveling, power failure resistant
JFFS2
▶ Available in the official Linux kernel filesystem
▶ Boot time depends on the filesystem size: doesn’t scale
well for large partitions because needs to scan the whole
storage at boot time. Need to enable MTD
CONFIG_JFFS2_SUMMARY to address this issue. driver
▶ http://www.linux-
mtd.infradead.org/doc/jffs2.html
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 287/520
Legacy flash filesystems: YAFFS2
Standard file
API
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 288/520
UBI/UBIFS
Standard file
API
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 289/520
UBI (1)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 290/520
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 291/520
UBI: internals
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 292/520
UBI: good practice
▶ UBI is responsible for distributing writes all over the flash device: the more space
you assign to a partition attached to the UBI layer the more efficient the 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 and
bootloader environments
▶ If you need extra MTD partitions, try to group them at the end or the beginning
of the flash device
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 293/520
UBI layout: bad example
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 294/520
UBI layout: good example
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 295/520
UBIFS
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 296/520
Linux: UBI host tools
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 297/520
ubinize configuration file
[data-volume]
[rootfs-volume]
[kernel-volume] mode=ubi
mode=ubi
mode=ubi image=data.ubifs
image=rootfs.ubifs
image=zImage vol_id=3
vol_id=2
vol_id=1 vol_size=30MiB
vol_size=2MiB
vol_type=static vol_type=dynamic
vol_type=dynamic
vol_name=kernel vol_name=data
vol_name=rootfs
vol_flags=autoresize
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 298/520
U-Boot: UBI tools
Grouped under the ubi command
▶ ubi part <part-name>
Attach an MTD partition to the UBI layer
▶ ubi info [layout]
Display UBI device information
(or volume information if the layout string is passed)
▶ ubi check <vol-name>
Check if a volume exists
▶ ubi readvol <dest-addr> <vol-name> [<size>]
Read volume contents
▶ U-Boot also provides tools to update the UBI device contents
▶ Using them is highly discouraged (the U-Boot UBI implementation is not entirely
stable, and using commands that do not touch the UBI metadata is safer)
▶ ubi createvol <vol-name> [<size>] [<type>]
▶ ubi removevol <vol-name>
▶ ubi writevol <src-addr> <vol-name> <size>
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 299/520
Linux: UBI target tools (1)
▶ Tools used on the target to dynamically create and modify UBI elements
▶ UBI device management:
▶ ubiformat /dev/mtdx
Format an MTD partition and preserve Erase Counter information if any.
Example: ubiformat /dev/mtd1
▶ ubiattach -m <MTD-device-id> /dev/ubi_ctrl
Attach an MTD partition/device to the UBI layer, and create a UBI device
Example: ubiattach -m 1 /dev/ubi_ctrl
▶ ubidetach -m <MTD-device-id> /dev/ubi_ctrl
Detach an MTD partition/device from the UBI layer, and remove the associated UBI
device
Example: ubidetach -m 1 /dev/ubi_ctrl
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 300/520
Linux: UBI target tools (2)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 301/520
Linux tools: BusyBox UBI limitations
Beware that the implementation of UBI commands in BusyBox is still incomplete. For
example:
▶ ubirsvol doesn’t support -N <name>. You have to use specify the volume to
resize by its id (-n num):
ubirsvol /dev/ubi0 -n 4 -s 64 MiB
▶ Same constraint for ubirmvol:
ubirmvol /dev/ubi0 -n 4
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 302/520
Linux: UBIFS host tools
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 303/520
Linux: UBIFS target tools
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 304/520
Linux: UBI image creation workflow
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 305/520
Linux: Using a UBIFS filesystem as root filesystem
▶ You just have to pass the following information on the kernel command line:
▶ ubi.mtd=1
Attach /dev/mtd1 to the UBI layer and create ubi0
▶ rootfstype=ubifs root=ubi0:rootfs
Mount the rootfs volume on ubi0 as a UBIFS filesystem
▶ Example: rootfstype=ubifs ubi.mtd=1 root=ubi0:rootfs
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 306/520
Summary: how to boot on a UBIFS filesystem
In U-Boot:
▶ Define partitions:
setenv mtdids ...
setenv mtdparts ...
▶ Define the base Linux kernel bootargs, specifying booting on UBIFS, the UBI
volume used as root filesystem, and the MTD partition attached to UBI. Example:
setenv bootargs_base console=ttyS0 rootfstype=ubifs root=ubi0:rootfs ubi.mtd=2 ...
▶ Define the boot command sequence, loading the U-Boot partition definitions,
loading kernel and DTB images from UBI partitions, and adding mtdparts to the
kernel command line. Example:
setenv bootcmd 'mtdparts; ubi part UBI; ubi readvol 0x81000000
kernel; ubi readvol 0x82000000 dtb; setenv bootargs ${bootargs_base}
${mtdparts}; bootz 0x81000000 - 0x82000000'
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 307/520
Linux: Block emulation layers
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 308/520
Linux: mtdblock
▶ The mtdblock layer creates a block device for each MTD device of the system
▶ Usually named /dev/mtdblockX.
▶ Allows read/write block-level access. However bad blocks are not handled, and no
wear leveling is done for writes.
▶ For historical reasons, JFFS2 and YAFFS2 filesystems require a block device for
the mount command.
▶ Do not write on mtdblock devices
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 309/520
Linux: ubiblock
▶ CONFIG_MTD_UBI_BLOCK
▶ Implemented by Ezequiel Garcia from Bootlin.
▶ Preferred over mtdblock if UBI is available (UBI accounts for data retention and
wear leveling issues, while MTD does not)
▶ The ubiblock layer creates read-only block devices on demand
▶ The user specifies which static volumes (s)he would like to attach to ubiblock
▶ Through the kernel command line: by passing
ubi.block=<ubi-dev-id>,<volume-name>
Example: ubi.block=0,rootfs
▶ In Linux, using the ubiblock utility provided by mtd-utils:
ubiblock --create <ubi-volume-dev-file>
▶ 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 310/520
Useful reading
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 311/520
Practical lab - Flash Filesystems
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 312/520
Embedded Linux system development
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 313/520
Contents
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 314/520
Embedded Linux system development
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 315/520
Third party 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
▶ They are freely available, freely distributable, and thanks to their open-source
nature, they can be analyzed and modified according to the needs of the project
▶ However, efficiently re-using these components is not always easy. One must:
▶ Find these components
▶ Choose the most appropriate ones
▶ Cross-compile them
▶ Integrate them in the embedded system and with the other applications
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 316/520
Find existing components
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 317/520
Choosing components
Not all free software components are necessarily good to re-use. One must pay
attention to:
▶ Vitality of the developer and user communities. This vitality ensures long-term
maintenance of the component, and relatively good support. It can be measured
by looking at the mailing-list traffic and the version control system activity.
▶ Quality of the component. Typically, if a component is already available through
embedded build systems, and has a dynamic user community, it probably means
that the quality is relatively good.
▶ License. The license of the component must match your licensing constraints.
For example, GPL libraries cannot be used in proprietary applications.
▶ Technical requirements. Of course, the component must match your technical
requirements. But don’t forget that you can improve the existing components if a
feature is missing!
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 318/520
Licenses (1)
▶ All software that are under a free software license give four freedoms to all users
▶ 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 for a definition of Free
Software
▶ Open Source software, as per the definition of the Open Source Initiative, are
technically similar to Free Software in terms of freedoms
▶ See https://www.opensource.org/docs/osd for the definition of Open Source
Software
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 319/520
Licenses (2)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 320/520
GPL
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 321/520
GPL: redistribution
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 322/520
LGPL
▶ You make modifications to the Linux kernel (to add drivers or adapt to your
board), to Busybox, U-Boot or other GPL software
▶ You must release the modified versions under the same license, and be ready to
distribute the source code to your customers
▶ You make modifications to the C library or any other LGPL library
▶ You must release the modified versions under the same license
▶ You create an application that relies on LGPL libraries
▶ You can keep your application proprietary, but you must link dynamically with the
LGPL libraries
▶ You make modifications to a non-copyleft licensed software
▶ You can keep your modifications proprietary, but you must still credit the authors
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 324/520
Non-copyleft licenses
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 325/520
BSD license
[...]
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 326/520
Is this free software?
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 327/520
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 a complete list of the free software packages you use, the original
version numbers you used, and to keep your modifications and adaptations
well-separated from the original version.
▶ Buildroot and Yocto Project can generate this list for you!
▶ Conform to the license requirements before shipping the product to the customers.
▶ Free Software licenses have been enforced successfully in courts. Organizations
which can help:
▶ Software Freedom Law Center, https://www.softwarefreedom.org/
▶ Software Freedom Conservancy, https://sfconservancy.org/
▶ Ask your legal department!
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 328/520
Keeping changes separate (1)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 329/520
Keeping changes separate (2)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 330/520
Embedded Linux system development
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 331/520
ssh server and client: Dropbear
https://matt.ucc.asn.au/dropbear/dropbear.html
▶ Very small memory footprint ssh server for embedded systems
▶ Satisfies most needs. Both client and server!
▶ Size: 110 KB, statically compiled with uClibc on x86.
(OpenSSH client and server: approx 1200 KB, dynamically compiled with glibc on
x86)
▶ Useful to:
▶ Get a remote console on the target device
▶ Copy files to and from the target device (scp or rsync -e ssh).
▶ An alternative to OpenSSH, used on desktop and server systems.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 332/520
Benefits of a web server interface
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 333/520
Web servers
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 334/520
Network utilities (1)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 335/520
Network utilities (2)
▶ pppd, implementation of the Point to Point Protocol, used for dial-up connections
▶ samba, implements the SMB and CIFS protocols, used by Windows to share files
and printers
▶ coherence, a UPnP/DLNA implementation
▶ vsftpd, proftpd, FTP servers
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 336/520
Embedded Linux system development
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 337/520
System utilities
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 338/520
Embedded Linux system development
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 339/520
Language interpreters
▶ Interpreters for the most common scripting languages are available. Useful for
▶ Application development
▶ Web services development
▶ Scripting
▶ Languages supported
▶ Lua
▶ Python
▶ Perl
▶ Ruby
▶ TCL
▶ PHP
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 340/520
Embedded Linux system development
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 341/520
Audio, video and multimedia
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 342/520
Embedded Linux system development
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 343/520
Embedded Linux system development
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 344/520
X.org - KDrive
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 345/520
Kdrive: architecture
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 346/520
Kdrive: usage
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 347/520
Wayland
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 348/520
Wayland: architecture
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 349/520
Embedded Linux system development
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 350/520
Gtk
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 351/520
Gtk stack components
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 352/520
Gtk example
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 353/520
Qt (1)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 354/520
Qt (2)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 355/520
Qt’s usage
Source: https://www.qt.io/qt-for-device-creation/
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 356/520
Other less frequent solutions
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 357/520
Embedded Linux system development
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 358/520
Lightweight database - SQLite
https://www.sqlite.org
▶ SQLite is a small C library that implements a self-contained, embeddable,
lightweight, zero-configuration SQL database engine
▶ The database engine of choice for embedded Linux systems
▶ Can be used as a normal library
▶ Can be directly embedded into a application, even a proprietary one since SQLite is
released in the public domain
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 359/520
Embedded Linux system development
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 360/520
WebKit
https://webkit.org/
▶ Web browser engine. Application framework that can be used to
develop web browsers.
▶ License: portions in LGPL and others in BSD. Proprietary
applications allowed.
▶ Used by many web browsers: Safari, iPhone and Android default
browsers ... Google Chrome now uses a fork of its WebCore
component). Used by e-mail clients too to render HTML.
▶ Multiple graphical back-ends: Qt4, GTK, EFL...
▶ You could use it to create your custom browser.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 361/520
Embedded Linux system development
System building
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 362/520
System building: goal and solutions
▶ Goal
▶ Integrate all the software components, both
third-party and in-house, into a working root
filesystem
▶ It involves the download, extraction,
configuration, compilation and installation of
all components, and possibly fixing issues and
adapting configuration files
▶ Several solutions
▶ Manually
▶ System building tools
▶ Distributions or ready-made filesystems
Penguin picture: https://bit.ly/1PwDklz
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 363/520
System building: manually
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 364/520
System building: manually (2)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 365/520
System foundations
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 366/520
Target and build spaces
▶ The system foundation, Busybox and C library, are the core of the target root
filesystem
▶ However, when building other components, one must distinguish two directories
▶ The target space, which contains the target root filesystem, everything that is
needed for execution of the application
▶ The build space, which will contain a lot more files than the target space, since it is
used to keep everything needed to compile libraries and applications. So we must
keep the headers, documentation, and other configuration files
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 367/520
Build systems
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 368/520
Autotools and friends
▶ 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
▶ pkgconfig is used to ease compilation against already installed shared libraries
▶ 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 majority of free software packages today. One must have a basic understanding
of what they do and how they work.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 369/520
automake / autoconf / autoheader
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 370/520
automake / autoconf
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 371/520
Configuring and compiling: native case
▶ The traditional steps to configure and compile an autotools based package are
▶ Configuration of the package
./configure
▶ Compilation of the package
make
▶ Installation of the package
make install
▶ Additional arguments can be passed to the ./configure script to adjust the
component configuration (run ./configure --help)
▶ Only the make install target needs to be done as root if the installation should
take place system-wide
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 372/520
Configuring and compiling: cross case (1)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 373/520
Configuring and compiling: cross case (2)
▶ If one simply runs make install, the software will be installed in the directory
passed as --prefix. For cross-compiling, one must pass the DESTDIR argument
to specify where the software must be installed.
▶ Making the distinction between the prefix (as passed with --prefix at configure
time) and the destination directory (as passed with DESTDIR at installation time)
is very important.
▶ Example:
export PATH=/usr/local/arm-linux/bin:$PATH
export CC=arm-linux-gcc
export STRIP=arm-linux-strip
./configure --host=arm-linux --prefix=/usr
make
make DESTDIR=$HOME/work/rootfs install
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 374/520
Installation (1)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 375/520
Installation (2)
Contents of usr/lib after installation of libpng and zlib
▶ libpng libtool description files
./lib/libpng12.la
./lib/libpng.la -> libpng12.la
▶ libpng static version
./lib/libpng12.a
./lib/libpng.a -> libpng12.a
▶ libpng dynamic version
./lib/libpng.so.3.32.0
./lib/libpng12.so.0.32.0
./lib/libpng12.so.0 -> libpng12.so.0.32.0
./lib/libpng12.so -> libpng12.so.0.32.0
./lib/libpng.so -> libpng12.so
./lib/libpng.so.3 -> libpng.so.3.32.0
▶ libpng pkg-config description files
./lib/pkgconfig/libpng12.pc
./lib/pkgconfig/libpng.pc -> libpng12.pc
▶ zlib dynamic version
./lib/libz.so.1.2.3
./lib/libz.so -> libz.so.1.2.3
./lib/libz.so.1 -> libz.so.1.2.3
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 376/520
Installation in the build and target spaces
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 377/520
pkg-config
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 378/520
Let’s find the libraries
▶ When compiling an application or a library that relies on other libraries, the build
process by default looks in /usr/lib for libraries and /usr/include for headers.
▶ The first thing to do is to set the CFLAGS and LDFLAGS environment variables:
export CFLAGS=-I/my/build/space/usr/include/
export LDFLAGS=-L/my/build/space/usr/lib
▶ The libtool files (.la files) must be modified because they include the absolute
paths of the libraries:
- libdir='/usr/lib'
+ libdir='/my/build/space/usr/lib’
▶ The PKG_CONFIG_LIBDIR environment variable must be set to the location of the
.pc files and the PKG_CONFIG_SYSROOT_DIR variable must be set to the build space
directory.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 379/520
Further details about autotools
See our ”Demystification tutorial” about the GNU Autotools (Thomas Petazzoni,
2016)
▶ Presentation slides: https://bit.ly/2hjpojv
▶ Video: https://youtu.be/_zX8LJ9Xjyk
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 380/520
Practical lab - Third party libraries and applications
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 381/520
System building tools: principle
▶ Different tools are available to automate the process of building a target system,
including the kernel, and sometimes the toolchain.
▶ They automatically download, configure, compile and install all the components in
the right order, sometimes after applying patches to fix cross-compiling issues.
▶ They already contain a large number of packages, that should fit your main
requirements, and are easily extensible.
▶ The build becomes reproducible, which allows to easily change the configuration
of some components, upgrade them, fix bugs, etc.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 382/520
Available system building tools
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 383/520
Buildroot (1)
▶ Allows to build a toolchain, a root filesystem image with many applications and
libraries, a bootloader and a kernel image
▶ Or any combination of the previous items
▶ Supports building uClibc, glibc and musl toolchains, either built by Buildroot, or
external
▶ Over 2000+ applications or libraries integrated, from basic utilities to more
elaborate software stacks: X.org, GStreamer, Qt, Gtk, WebKit, Python, PHP, etc.
▶ Good for small to medium embedded systems, with a fixed set of features
▶ No support for generating packages (.deb or .ipk)
▶ Needs complete rebuild for most configuration changes.
▶ Active community, releases published every 3 months.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 384/520
Buildroot (2)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 385/520
Buildroot: adding a new package (1)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 386/520
Buildroot: adding a new package (2)
▶ For a simple package with a single configuration option to enable/disable it, the
Config.in file looks like:
config BR2_PACKAGE_GQVIEW
bool "gqview"
depends on BR2_PACKAGE_LIBGTK2
help
GQview is an image viewer for UNIX operating systems
http://prdownloads.sourceforge.net/gqview
▶ It must be sourced from package/Config.in:
source "package/gqview/Config.in"
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 387/520
Buildroot: adding new package (3)
GQVIEW_VERSION = 2.1.5
GQVIEW_SOURCE = gqview-$(GQVIEW_VERSION).tar.gz
GQVIEW_SITE = http://prdownloads.sourceforge.net/gqview
GQVIEW_DEPENDENCIES = host-pkgconf libgtk2
GQVIEW_CONF_ENV = LIBS="-lm"
GQVIEW_LICENSE = GPL-2.0
GQVIEW_LICENSE_FILES = COPYING
$(eval $(autotools-package))
▶ The package directory and the prefix of all variables must be identical to the suffix
of the main configuration option BR2_PACKAGE_GQVIEW
▶ The autotools-package infrastructure knows how to build autotools packages.
A more generic generic-package infrastructure is available for packages not
using the autotools as their build system.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 388/520
OpenEmbedded / Yocto Project
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 389/520
Distributions - Debian
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 390/520
Distributions - Others
Fedora
▶ https:
//fedoraproject.org/wiki/Architectures/ARM
Image credits: https://bit.ly/2EzmJLF
▶ Supported on various recent ARM boards (such as
Beaglebone Black and Raspberry Pi)
▶ Supports QEMU emulated ARM boards too (Versatile
Express board)
▶ Shipping the same version as for desktops!
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 391/520
Embedded distributions
Even if you don’t use them for final products, they can be useful to make demos quickly
▶ Android: https://www.android.com/
Google’s distribution for phones, tablets, TVs, cars...
Except the Linux kernel, very different user space than other Linux
distributions. Very successful, lots of applications available (many
proprietary).
▶ Alpine Linux: https://www.alpinelinux.org/
Security oriented distribution based on Musl and BusyBox, fitting in
about 130 MB of storage, supporting x86 and arm, both 32 and 64 bit.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 392/520
Application frameworks
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 393/520
Practical lab - Buildroot
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 394/520
Embedded Linux application development
Embedded Linux
application development
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 395/520
Contents
▶ Application development
▶ Developing applications on embedded Linux
▶ Building your applications
▶ Source management
▶ Integrated development environments (IDEs)
▶ Version control systems
▶ Debugging and analysis tools
▶ Debuggers
▶ Memory checkers
▶ System analysis
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 396/520
Embedded Linux application development
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 397/520
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 398/520
Programming language
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 399/520
C library or higher-level libraries?
▶ For many applications, the C library already provides a relatively large set of
features
▶ file and device I/O, networking, threads and synchronization, inter-process
communication
▶ Thoroughly described in the glibc manual, or in any Linux system programming book
▶ However, the API carries a lot of history and is not necessarily easy to grasp for new
comers
▶ Therefore, using a higher level framework, such as Qt or the Gtk/Glib stack,
might be a good idea
▶ These frameworks are not only graphical libraries, their core is separate from the
graphical part
▶ But of course, these libraries have some memory and storage footprint, in the order
of a few megabytes
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 400/520
Building your applications
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 401/520
Simple Makefile (1)
Case of an application that only uses the C library, contains two source files and
generates a single binary
CROSS_COMPILE?=arm-linux-
CC=$(CROSS_COMPILE)gcc
OBJS=foo.o bar.o
all: foobar
foobar: $(OBJS)
Tab $(CC) -o $@ $^
clean:
Tab $(RM) -f foobar $(OBJS)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 402/520
Simple Makefile (2)
Case of an application that uses the Glib and the GPS libraries
CROSS_COMPILE?=arm-linux-
LIBS=libgps glib-2.0
OBJS=foo.o bar.o
CC=$(CROSS_COMPILE)gcc
CFLAGS=$(shell pkg-config --cflags $(LIBS))
LDFLAGS=$(shell pkg-config --libs $(LIBS))
all: foobar
foobar: $(OBJS)
Tab $(CC) -o $@ $^ $(LDFLAGS)
clean:
Tab $(RM) -f foobar $(OBJS)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 403/520
Embedded Linux application development
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 404/520
Visual Studio Code
https://code.visualstudio.com/
▶ Created by Microsoft
▶ License: MIT
▶ Extensible, language agnostic text editor
▶ Built-in git commands
▶ The most popular IDE (open-source and proprietary)
according Stack Overflow’s 2019 survey
▶ Try it on Ubuntu: sudo snap install --classic code
▶ https://en.wikipedia.org/wiki/Visual_Studio_Code
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 405/520
Eclipse (1)
https://www.eclipse.org/
▶ An extensible, plug-in based software development kit, typically
used for creating IDEs.
▶ Supported by the Eclipse foundation, a non-profit consortium of image credits:
https://bit.ly/2Hntsvf
major software industry vendors (IBM, Intel, Borland, Nokia,
Wind River, Zend, Computer Associates...).
▶ Free Software license (Eclipse Public License). Incompatible with
the GPL.
▶ Supported platforms: GNU/Linux, UNIX, Windows
Extremely popular: created a lot of attraction.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 406/520
Eclipse (2)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 407/520
Other popular solutions
Vim
▶ Many embedded Linux developers simply use
Vim or Emacs. They can integrate with
debuggers, source code browsers such as
cscope, offer syntax highlighting and more.
▶ People also use QtCreator, even for non Qt
projects
▶ Atom is a very popular text editor too
▶ See Stack Overflow’s survey of most popular Emacs
IDEs (2019): https://frama.link/bfPgbb88
All these tools are available in most Linux
distributions, simply install them and try them out!
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 408/520
Embedded Linux application development
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 409/520
Version control systems
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 410/520
Traditional version control systems
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 411/520
Distributed source control systems (1)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 412/520
Distributed source control systems (2)
▶ Git
▶ Initially designed and developed by Linus Torvalds for Linux kernel development
▶ Extremely popular in the community, and used by more and more projects (kernel,
U-Boot, Barebox, uClibc, GNOME, X.org, etc.)
▶ Outstanding performance, in particular in big projects
▶ https://en.wikipedia.org/wiki/Git_(software)
▶ Mercurial
▶ Another system, created with the same goals as Git.
▶ Used by some big projects too
▶ https://en.wikipedia.org/wiki/Mercurial
https://en.wikipedia.org/wiki/Version_control_systems#Distributed_
revision_control
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 413/520
Embedded Linux application development
Debuggers
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 414/520
GDB
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 415/520
GDB crash course
A few useful GDB commands
▶ break foobar
puts a breakpoint at the entry of function foobar()
▶ break foobar.c:42
puts a breakpoint in foobar.c, line 42
▶ print var or print task->files[0].fd
prints the variable var, or a more complicated reference. GDB can also nicely
display structures with all their members
▶ continue
continue the execution
▶ next
continue to the next line, stepping over function calls
▶ step
continue to the next line, entering into subfunctions
▶ backtrace
display the program stack
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 416/520
GDB graphical front-ends
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 417/520
Embedded Linux application development
Remote debugging
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 418/520
Remote debugging
ARCH-linux-gdb
gdbserver
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 419/520
Remote debugging: architecture
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 420/520
Remote debugging: usage
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 421/520
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, and gdb can use this core file to
let us analyze the state of the crashed application
▶ On the target
▶ Use ulimit -c unlimited to enable the generation of a core file when a crash
occurs
▶ 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 422/520
Embedded Linux application development
Memory checkers
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 423/520
Valgrind (1)
https://valgrind.org/
▶ GNU GPL Software suite for debugging and profiling programs.
▶ Supported platforms: Linux on x86, x86_64, arm (armv7 only),
arm64, mips32, s390, ppc32 and ppc64. Also supported on other
operating systems (Android, Darwin, Illumos, Solaris...)
▶ Can detect many memory management and threading bugs.
▶ Profiler: provides information helpful to speed up your program
and reduce its memory usage.
▶ The most popular tool for this usage. Even used by projects with
hundreds of programmers.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 424/520
Valgrind (2)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 425/520
Embedded Linux application development
System analysis
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 426/520
strace
System call tracer
https://sourceforge.net/projects/strace/
▶ Available on all GNU/Linux systems
Can be built by your cross-compiling toolchain generator.
▶ Even easier: drop a ready-made static binary for your architecture, just when you
need it. See
https://github.com/bootlin/static-binaries/tree/master/strace
▶ 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 -p <pid> (tracing an existing process) strace -c <command> (statistics
of system calls taking most time)
See man strace for details.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 427/520
strace example output
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 429/520
ltrace
A tool to trace 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.
▶ Manual page: https://linux.die.net/man/1/ltrace
See https://en.wikipedia.org/wiki/Ltrace for details
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 430/520
ltrace example output
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 431/520
ltrace summary
Example summary at the end of the ltrace output (-c option)
Process 17019 detached
% time seconds usecs/call calls errors syscall
------ ----------- ----------- --------- --------- ----------------
100.00 0.000050 50 1 set_thread_area
0.00 0.000000 0 48 read
0.00 0.000000 0 44 write
0.00 0.000000 0 80 63 open
0.00 0.000000 0 19 close
0.00 0.000000 0 1 execve
0.00 0.000000 0 2 2 access
0.00 0.000000 0 3 brk
0.00 0.000000 0 1 munmap
0.00 0.000000 0 1 uname
0.00 0.000000 0 1 mprotect
0.00 0.000000 0 19 mmap2
0.00 0.000000 0 50 46 stat64
0.00 0.000000 0 18 fstat64
------ ----------- ----------- --------- --------- ----------------
100.00 0.000050 288 111 total
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 432/520
OProfile
http://oprofile.sourceforge.net
▶ A system-wide profiling tool
▶ Can collect statistics like the top users of the CPU.
▶ Works without having the sources.
▶ Requires a kernel patch to access all features, but is already available in a
standard kernel.
▶ Requires more investigation to see how it works.
▶ Ubuntu/Debian packages: oprofile, oprofile-gui
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 433/520
Practical lab - App. development and debugging
Application development
▶ Compile your own application with the ncurses
library
Remote debugging
▶ Set up remote debugging tools on the target:
strace, ltrace
and gdbserver.
▶ Debug a simple application running on the
target using remote debugging
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 434/520
Real-time in embedded Linux systems
Real-time in embedded
Linux systems
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 435/520
Real-time in embedded Linux systems
Introduction
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 436/520
Embedded Linux and real time
▶ Due to its advantages, Linux and open-source software are more and more
commonly used in embedded applications
▶ However, some applications also have real-time constraints
▶ They, at the same time, want to
▶ Get all the nice advantages of Linux: hardware support, components re-use, low
cost, etc.
▶ Get their real-time constraints met
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 437/520
Embedded Linux and real time
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 438/520
Linux and real-time approaches (1)
▶ Over time, two major approaches have been taken to bring real-time requirements
into Linux
▶ Approach 1
▶ Improve the Linux kernel itself so that it matches real-time requirements, by
providing bounded latencies, real-time APIs, etc.
▶ Approach taken by the mainline Linux kernel and the PREEMPT_RT project.
▶ Approach 2
▶ Add a layer below the Linux kernel that will handle all the real-time requirements, so
that the behavior of Linux doesn’t affect real-time tasks.
▶ Approach taken by RTLinux, RTAI and Xenomai
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 439/520
Linux and real-time approaches (2)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 440/520
Real-time in embedded Linux systems
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 441/520
Understanding latency
▶ When developing real-time applications with a system such as Linux, the typical
scenario is the following
▶ An event from the physical world happens and gets notified to the CPU by means of
an interrupt
▶ The interrupt handler recognizes and handles the event, and then wake-up the user
space task that will react to this event
▶ Some time later, the user space task will run and be able to react to the physical
world event
▶ Real-time is about providing guaranteed worst case latencies for this reaction
time, called latency
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 442/520
Linux kernel latency components
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 443/520
Interrupt latency
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 444/520
Sources of interrupt latency
▶ One of the concurrency prevention mechanism used in the kernel is the spinlock
▶ It has several variants, but one of the variant commonly used to prevent
concurrent accesses between a process context and an interrupt context works by
disabling interrupts
▶ Critical sections protected by spinlocks, or other section in which interrupts are
explicitly disabled will delay the beginning of the execution of the interrupt
handler
▶ The duration of these critical sections is unbounded
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 445/520
Interrupt handler duration
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 446/520
Interrupt handler implementation
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 447/520
Scheduler latency
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 448/520
Understanding preemption (1)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 449/520
Understanding preemption (2)
▶ However, when the interrupt comes while the task is executing a system call, this
system call has to finish before another task can be scheduled.
▶ By default, the Linux kernel does not do kernel preemption.
▶ This means that the time before which the scheduler will be called to schedule
another task is unbounded.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 450/520
Scheduler duration
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 451/520
Sources of scheduler duration
▶ Before Linux 2.6, the execution time of the scheduler depended on the number of
processes on the system.
▶ Since Linux 2.6, the Linux kernel chooses the next process to run in constant time
(”O(1) scheduler” feature).
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 452/520
Other non-deterministic mechanisms
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 453/520
Priority inversion
A process with a low priority might hold a lock needed by a higher priority process,
effectively reducing the priority of this process. Things can be even worse if a middle
priority process uses the CPU.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 454/520
Interrupt handler priority
In Linux, interrupt handlers are executed directly by the CPU interrupt mechanisms,
and not under control of the Linux scheduler. Therefore, all interrupt handlers have a
higher priority than all tasks running on the system.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 455/520
The PREEMPT_RT project
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 456/520
Improvements to the mainline kernel
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 457/520
Preemption options
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 458/520
1st option: no forced preemption
CONFIG_PREEMPT_NONE
Kernel code (interrupts, exceptions, system calls) never preempted. Default behavior in
standard kernels.
▶ Best for systems making intense computations, on which overall throughput is key.
▶ Best to reduce task switching to maximize CPU and cache usage (by reducing
context switching).
▶ Still benefits from some Linux real-time improvements: O(1) scheduler, increased
multiprocessor safety (work on RT preemption was useful to identify hard to find
SMP bugs).
▶ Can also benefit from a lower timer frequency (100 Hz instead of 250, 300 or
1000).
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 459/520
2nd option: voluntary kernel preemption
CONFIG_PREEMPT_VOLUNTARY
Kernel code can preempt itself
▶ Typically for desktop systems, for quicker application reaction to user input.
▶ Adds explicit rescheduling points (might_sleep()) throughout kernel code.
▶ Minor impact on throughput.
▶ Still used in: Ubuntu Desktop 20.04
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 460/520
3rd option: preemptible kernel
CONFIG_PREEMPT
Most kernel code can be involuntarily preempted at any time. When a process
becomes runnable, no more need to wait for kernel code (typically a system call) to
return before running the scheduler.
▶ Exception: kernel critical sections (holding spinlocks). In a case you hold a
spinlock on a uni-processor system, kernel preemption could run another process,
which would loop forever if it tried to acquire the same spinlock.
▶ Typically for desktop or embedded systems with latency requirements in the
milliseconds range.
▶ Still a relatively minor impact on throughput.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 461/520
Priority inheritance
▶ One classical solution to the priority inversion problem is called priority inheritance
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 462/520
High resolution timers
▶ The resolution of the timers used to be bound to the resolution of the regular
system tick
▶ Usually 100 Hz or 250 Hz, depending on the architecture and the configuration
▶ A resolution of only 10 ms or 4 ms.
▶ Increasing the regular system tick frequency is not an option as it would consume
too many resources
▶ The high-resolution timers infrastructure allows to use the available hardware
timers to program interrupts at the right moment.
▶ Hardware timers are multiplexed, so that a single hardware timer is sufficient to
handle a large number of software-programmed timers.
▶ Usable directly from user space using the usual timer APIs
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 463/520
Threaded interrupts
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 464/520
Real-time in embedded Linux systems
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 465/520
New real-time preemption option (1)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 466/520
New real-time preemption option (2)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 467/520
CONFIG_PREEMPT_RT (1)
This level of preemption replaces all kernel spinlocks by mutexes (or so-called sleeping
spinlocks)
▶ Instead of providing mutual exclusion by disabling interrupts and preemption, they
are just normal locks: when contention happens, the process is blocked and
another one is selected by the scheduler.
▶ Works well with threaded interrupts, since threads can block, while usual interrupt
handlers could not.
▶ Some core, carefully controlled, kernel spinlocks remain as normal spinlocks
(spinlock annotations: differentiation now in mainline between spinning spinlocks
and those that can be converted to sleeping spinlocks).
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 468/520
CONFIG_PREEMPT_RT (2)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 469/520
Real-time in embedded Linux systems
Setting up PREEMPT_RT
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 470/520
PREEMPT_RT setup (1)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 471/520
PREEMPT_RT setup (2)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 472/520
PREEMPT_RT mainlining status (1)
▶ The project is making good progress since it got funding from the Linux
foundation in 2015.
▶ However, a reduction of the size of the PREEMPT_RT patchset is not really
visible yet:
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 473/520
PREEMPT_RT mainlining status (2)
▶ However, the mainline Linux kernel is a moving target too, introducing new issues
for real-time (such as disabling preemption in BPF... see
https://lwn.net/Articles/802884/).
▶ A major achievement though in Linux 5.3: the CONFIG_PREEMPT_RT configuration
option is now in mainline, though it cannot be selected yet (missing
dependencies). This simplifies further mainlining work.
▶ See the latest status presentations:
▶ PREEMPT_RT: status and Q&A, Thomas Gleixner, Linux Plumbers 2019
Video: https://youtu.be/bpyFQJV5gCI?t=8489
▶ State of PREEMPT_RT - Sebastian Andrzej Siewior, Real-time Linux Summit 2019
https://bristot.me/wp-content/uploads/2019/11/rtls2019_07_rt_status.pdf
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 474/520
Useful resources
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 475/520
Real-time in embedded Linux systems
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 476/520
Development and compilation
▶ No special library is needed, the POSIX real-time API is part of the standard C
library
▶ The glibc C library is recommended, as support for some real-time features is not
mature in other C libraries
▶ Priority inheritance mutexes or NPTL on some architectures, for example
▶ Compile a program
▶ ARCH-linux-gcc -o myprog myprog.c -lrt
▶ To get the documentation of the POSIX API
▶ Install the manpages-posix-dev package
▶ Run man function-name
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 477/520
Process, thread?
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 478/520
Process, thread: kernel point of view
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 479/520
Creating threads
pthread_exit(void *value_ptr);
▶ Waiting for the termination of a thread
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 480/520
Scheduling classes (1)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 481/520
Scheduling classes (2)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 482/520
Using scheduling classes (1)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 483/520
Using scheduling classes (2)
pthread_attr_init(&attr);
pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
pthread_attr_setschedpolicy(&attr, SCHED_FIFO);
parm.sched_priority = 42;
pthread_attr_setschedparam(&attr, &parm);
▶ Then the thread can be created using pthread_create(), passing the attr
structure.
▶ Several other attributes can be defined this way: stack size, etc.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 484/520
Memory locking
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 485/520
Mutexes
▶ Allows mutual exclusion between two threads in the same address space
▶ Initialization/destruction
pthread_mutex_init(pthread_mutex_t *mutex,
const pthread_mutexattr_t *mutexattr);
pthread_mutex_destroy(pthread_mutex_t *mutex);
▶ Lock/unlock
pthread_mutex_lock(pthread_mutex_t *mutex);
pthread_mutex_unlock(pthread_mutex_t *mutex);
pthread_mutexattr_t attr;
pthread_mutexattr_init (&attr);
pthread_mutexattr_setprotocol(&attr, PTHREAD_PRIO_INHERIT);
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 486/520
Timers
▶ Timer creation
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 487/520
Timers (2)
▶ Delete a timer
timer_delete(timer_t timerid)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 488/520
Signals
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 489/520
Inter-process communication
▶ Semaphores
▶ Usable between different processes using named semaphores
▶ sem_open(), sem_close(), sem_unlink(), sem_init(), sem_destroy(),
sem_wait(), sem_post(), etc.
▶ Message queues
▶ Allows processes to exchange data in the form of messages.
▶ mq_open(), mq_close(), mq_unlink(), mq_send(), mq_receive(), etc.
▶ Shared memory
▶ Allows processes to communicate by sharing a segment of memory
▶ shm_open(), ftruncate(), mmap(), munmap(), close(), shm_unlink()
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 490/520
Real-time in embedded Linux systems
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 491/520
ftrace - Kernel function tracer
Infrastructure that can be used for debugging or analyzing latencies and performance
issues in the kernel.
▶ Very well documented in Documentation/trace/ftrace.txt
▶ Negligible overhead when tracing is not enabled at run-time.
▶ Can be used to trace any kernel function!
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 492/520
Using ftrace
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 493/520
Scheduling latency tracer
CONFIG_SCHED_TRACER (Kernel Hacking section)
▶ Maximum recorded time between waking up a top priority task and its scheduling on a CPU,
expressed in us.
▶ Check that wakeup is listed in /sys/kernel/tracing/available_tracers
▶ To select, reset and enable this tracer:
cat /sys/kernel/tracing/tracing_max_latency
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 494/520
Real-time in embedded Linux systems
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 495/520
Linux real-time extensions
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 496/520
RTLinux
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 497/520
RTAI
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 498/520
Xenomai project
https://www.xenomai.org/
▶ Started in 2001 as a project aiming at emulating traditional
RTOS.
▶ Initial goals: facilitate the porting of programs to GNU / Linux.
▶ Skins mimicking the APIs of the VxWorks and pSOS traditional
real-time operating systems, as well as the POSIX API, and
offering a “native” API too.
▶ Offers two solutions:
▶ Cobalt: (like Co-kernel), using a micro-kernel dedicated to
running real-time tasks. This will never be merged in the mainline
kernel.
▶ Mercury: (like Merge), based on the mainline kernel with
PREEMPT_RT.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 499/520
Xenomai Cobalt architecture
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 500/520
Xenomai user space real-time support
▶ Supported architectures are the ones supported by the ipipe patch: arm, arm64,
blackfin, powerpc, x86
▶ Two modes are defined for a thread
▶ the primary mode, where the thread is handled by the Xenomai scheduler
▶ the secondary mode, when it is handled by the Linux scheduler.
▶ Thanks to the services of the Adeos I-pipe service, Xenomai system calls are
defined.
▶ A thread migrates from secondary mode to primary mode when such a system call is
issued
▶ It migrates from primary mode to secondary mode when a Linux system call is
issued, or to handle gracefully exceptional events such as exceptions or Linux signals.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 501/520
Life of a Xenomai application
▶ Xenomai applications are started like normal Linux processes, they are initially
handled by the Linux scheduler and have access to all Linux services
▶ After their initialization, they declare themselves as real-time applications, which
migrates them to primary mode. In this mode:
▶ They are scheduled directly by the Xenomai scheduler, so they have the real-time
properties offered by Xenomai
▶ They don’t have access to any Linux service, otherwise they get migrated back to
secondary mode and loose all real-time properties
▶ They can only use device drivers that are implemented in Xenomai, not the ones of
the Linux kernel
▶ Need to implement device drivers in Xenomai, and to split real-time and non
real-time parts of your applications.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 502/520
Real Time Driver Model (RTDM)
▶ An approach to unify the interfaces for developing device drivers and associated
applications under real-time Linux
▶ An API very similar to the native Linux kernel driver API
▶ Allows to develop in kernel space:
▶ Character-style device drivers
▶ Network-style device drivers
▶ Current notable RTDM based drivers:
▶ Serial port controllers;
▶ RTnet UDP/IP stack;
▶ RT socket CAN, drivers for CAN controllers;
▶ Analogy, fork of the Comedi project, drivers for acquisition cards.
https://xenomai.org/documentation/xenomai-
3/html/xeno3prm/group__rtdm.html
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 503/520
Real-time in embedded Linux systems
Setting up Xenomai
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 504/520
How to build the Xenomai kernel (Cobalt)
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 505/520
Linux options for Xenomai configuration
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 506/520
How to build Xenomai user space
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 507/520
Real-time in embedded Linux systems
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 508/520
The POSIX skin
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 510/520
Alchemy: the native API
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 511/520
Benchmarks: mainline vs PREEMPT_RT vs Xenomai
cyclictest results on BeagleBone Black (ARM), with different kernel setups.
Wake-up time measured in µs.
http://wiki.csie.ncku.edu.tw/embedded/xenomai/rtlws_paper.pdf
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 512/520
Organizations
▶ https://www.osadl.org
Open Source Automation Development Lab (OSADL)
Targets machine and plant control systems. Most member companies are German
(Thomas Gleixner is on board). Supports the use of PREEMPT_RT and Xenomai
and contributes to these projects. Shares useful documentation and resources.
They also organize a yearly Real Time Linux Workshop.
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 513/520
Practical lab - Real-time - Scheduling latency
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 514/520
References
References
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 515/520
Books
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 516/520
Web sites
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 517/520
International conferences
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 518/520
Last slides
Last slides
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 519/520
Last slide
Thank you!
And may the Source be with you
- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 520/520