KEMBAR78
Embedded linux system development (slides) | PDF
Embedded Linux system development 
Embedded Linux system 
development 
Free Electrons 
©Copyright 2004-2014, Free Electrons. 
Creative Commons BY-SA 3.0 license. 
Latest update: October 20, 2014. 
Document updates and sources: 
http://free-electrons.com/doc/training/embedded-linux 
Corrections, suggestions, contributions and translations are welcome! 
Embedded Linux 
Developers 
Free Electrons 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 1/526
Rights to copy 
© Copyright 2004-2014, Free Electrons 
License: Creative Commons Attribution - Share Alike 3.0 
http://creativecommons.org/licenses/by-sa/3.0/legalcode 
You are free: 
I to copy, distribute, display, and perform the work 
I to make derivative works 
I to make commercial use of the work 
Under the following conditions: 
I Attribution. You must give the original author credit. 
I Share Alike. If you alter, transform, or build upon this work, you may distribute 
the resulting work only under a license identical to this one. 
I For any reuse or distribution, you must make clear to others the license terms of 
this work. 
I Any of these conditions can be waived if you get permission from the copyright 
holder. 
Your fair use and other rights are in no way aected by the above. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 2/526
Electronic copies of these documents 
I Electronic copies of your particular version of the materials are 
available on: 
http://free-electrons.com/doc/training/embedded-linux 
I Open the corresponding documents and use them throughout 
the course to look for explanations given earlier by the 
instructor. 
I You will need these electronic versions because we neither 
print any index nor any table of contents (quite high 
environmental cost for little added value) 
I For future reference, see the
rst slide to see where document 
updates will be available. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 3/526
Hyperlinks in the document 
There are many hyperlinks in the document 
I Regular hyperlinks: 
http://kernel.org/ 
I Kernel documentation links: 
Documentation/kmemcheck.txt 
I Links to kernel source
les and directories: 
drivers/input 
include/linux/fb.h 
I Links to the declarations, de
nitions and instances of kernel 
symbols (functions, types, data, structures): 
platform_get_irq() 
GFP_KERNEL 
struct file_operations 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 4/526
Free Electrons at a glance 
I Engineering company created in 2004 
(not a training company!) 
I Locations: Orange, Toulouse, Lyon (France) 
I Serving customers all around the world 
See http://free-electrons.com/company/customers/ 
I Head count: 9 
Only Free Software enthusiasts! 
I Focus: Embedded Linux, Linux kernel, Android Free Software 
/ Open Source for embedded and real-time systems. 
I Activities: development, training, consulting, technical 
support. 
I Added value: get the best of the user and development 
community and the resources it oers. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 5/526
Free Electrons on-line resources 
I All our training materials: 
http://free-electrons.com/docs/ 
I Technical blog: 
http://free-electrons.com/blog/ 
I Quarterly newsletter: 
http://lists.free-electrons. 
com/mailman/listinfo/newsletter 
I News and discussions (Google +): 
https://plus.google.com/+FreeElectronsDevelopers 
I News and discussions (LinkedIn): 
http://linkedin.com/groups/Free-Electrons-4501089 
I Quick news (Twitter): 
http://twitter.com/free_electrons 
I Linux Cross Reference - browse Linux kernel sources on-line: 
http://lxr.free-electrons.com 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 6/526
Mailing lists for session participants 
If you participated to a course taught by Free Electrons, you can 
subscribe to our mailing lists dedicated to our training customers: 
I To ask questions about the topics covered by the courses, and 
to get a
rst level of technical support for issues that your are 
facing in real life or with your own hardware. 
I To share your experience with other people having taken the 
same course and done the same practical labs. 
I To share further comments and suggestions about course 
contents. 
I To get news about important updates to the course materials. 
See http://free-electrons.com/training/mailing-lists 
A simple way to register:
ll the form on http://j.mp/1r1HhkZ 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 7/526
Generic course information 
Generic course 
information 
Free Electrons 
©Copyright 2004-2014, Free Electrons. 
Creative Commons BY-SA 3.0 license. 
Corrections, suggestions, contributions and translations are welcome! 
Embedded Linux 
Developers 
Free Electrons 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 8/526
Hardware used in this training session 
Using Atmel SAMA5D3 Xplained boards in all practical labs 
I SAMA5D36 (Cortex A5) CPU from Atmel 
I USB powered! 
I 256 MB DDR2 RAM, 256 MB NAND 

ash 
I 2 Ethernet ports (Gigabit + 100 Mbit) 
I 2 USB 2.0 host, 1 USB device 
I 1 MMC/SD slot 
I 3.3 V serial port (like Beaglebone Black) 
I Misc: Arduino R3-compatible header, 
JTAG, buttons, LEDs 
I Currently sold at 69 EUR by Mouser 
(V.A.T. not included) 
Board and CPU documentation, design
les, software: 
http://www.atmel.com/sama5d3xplained 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 9/526
Participate! 
During the lectures... 
I Don't hesitate to ask questions. Other people in the audience 
may have similar questions too. 
I This helps the trainer to detect any explanation that wasn't 
clear or detailed enough. 
I Don't hesitate to share your experience, for example to 
compare Linux / Android with other operating systems used 
in your company. 
I Your point of view is most valuable, because it can be similar 
to your colleagues' and dierent from the trainer's. 
I Your participation can make our session more interactive and 
make the topics easier to learn. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 10/526
Practical lab guidelines 
During practical labs... 
I We cannot support more than 8 workstations at once (each 
with its board and equipment). Having more would make the 
whole class progress slower, compromising the coverage of the 
whole training agenda (exception for public sessions: up to 10 
people). 
I So, if you are more than 8 participants, please form up to 8 
working groups. 
I Open the electronic copy of your lecture materials, and use it 
throughout the practical labs to
nd the slides you need again. 
I Don't copy and paste from the PDF slides. 
The slides contain UTF-8 characters that look the same as 
ASCII ones, but won't be understood by shells or compilers. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 11/526
Cooperate! 
As in the Free Software and Open Source community, cooperation 
during practical labs is valuable in this training session: 
I 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. 
I Explain what you understood to other participants when 
needed. It also helps to consolidate your knowledge. 
I Don't hesitate to report potential bugs to your instructor. 
I Don't hesitate to look for solutions on the Internet as well. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 12/526
Command memento sheet 
I This memento sheet gives 
command examples for the most 
typical needs (looking for
les, 
extracting a tar archive...) 
I It saves us 1 day of UNIX / Linux 
command line training. 
I Our best tip: in the command line 
shell, always hit the Tab key to 
complete command names and
le 
paths. This avoids 95% of typing 
mistakes. 
I Get an electronic copy on 
http://free-electrons.com/ 
doc/training/embedded-linux/ 
command_memento.pdf 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 13/526
vi basic commands 
I The vi editor is very useful to 
make quick changes to
les in an 
embedded target. 
I Though not very user friendly at
rst, vi is very powerful and its 
main 15 commands are easy to 
learn and are sucient for 99% of 
everyone's needs! 
I Get an electronic copy on 
http://free-electrons.com/ 
doc/training/embedded-linux/ 
vi_memento.pdf 
I You can also take the quick tutorial 
by running vimtutor. This is a 
worthy investment! 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 14/526
Introduction to Embedded Linux 
Introduction to 
Embedded Linux 
Free Electrons 
©Copyright 2004-2014, Free Electrons. 
Creative Commons BY-SA 3.0 license. 
Corrections, suggestions, contributions and translations are welcome! 
Embedded Linux 
Developers 
Free Electrons 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 15/526
Birth of free software 
I 1983, Richard Stallman, GNU project and the free software 
concept. Beginning of the development of gcc, gdb, glibc and 
other important tools 
I 1991, Linus Torvalds, Linux kernel project, a Unix-like 
operating system kernel. Together with GNU software and 
many other open-source components: a completely free 
operating system, GNU/Linux 
I 1995, Linux is more and more popular on server systems 
I 2000, Linux is more and more popular on embedded 
systems 
I 2008, Linux is more and more popular on mobile devices 
I 2010, Linux is more and more popular on phones 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 16/526
Free software? 
I A program is considered free when its license oers to all its 
users the following four freedoms 
I Freedom to run the software for any purpose 
I Freedom to study the software and to change it 
I Freedom to redistribute copies 
I Freedom to distribute copies of modi
ed versions 
I These freedoms are granted for both commercial and 
non-commercial use 
I They imply the availability of source code, software can be 
modi
ed and distributed to customers 
I Good match for embedded systems! 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 17/526
What is embedded Linux? 
Embedded Linux is the usage of the 
Linux kernel and various 
open-source components in 
embedded systems 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 18/526
Introduction to Embedded Linux 
Advantages of Linux and open-source 
for embedded systems 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 19/526
Re-using components 
I The key advantage of Linux and open-source in embedded 
systems is the ability to re-use components 
I The open-source ecosystem already provides many 
components for standard features, from hardware support to 
network protocols, going through multimedia, graphic, 
cryptographic libraries, etc. 
I 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. 
I Allows to quickly design and develop complicated products, 
based on existing components. 
I No-one should re-develop yet another operating system kernel, 
TCP/IP stack, USB stack or another graphical toolkit library. 
I Allows to focus on the added value of your product. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 20/526
Low cost 
I Free software can be duplicated on as many devices as you 
want, free of charge. 
I 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. 
I Allows to have a higher budget for the hardware or to 
increase the company's skills and knowledge 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 21/526
Full control 
I With open-source, you have the source code for all 
components in your system 
I Allows unlimited modi
cations, changes, tuning, debugging, 
optimization, for an unlimited period of time 
I Without lock-in or dependency from a third-party vendor 
I To be true, non open-source components must be avoided 
when the system is designed and developed 
I Allows to have full control over the software part of your 
system 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 22/526
Quality 
I Many open-source components are widely used, on millions of 
systems 
I Usually higher quality than what an in-house development can 
produce, or even proprietary vendors 
I Of course, not all open-source components are of good 
quality, but most of the widely-used ones are. 
I Allows to design your system with high-quality 
components at the foundations 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 23/526
Eases testing of new features 
I Open-source being freely available, it is easy to get a piece of 
software and evaluate it 
I Allows to easily study several options while making a choice 
I Much easier than purchasing and demonstration procedures 
needed with most proprietary products 
I Allows to easily explore new possibilities and solutions 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 24/526
Community support 
I Open-source software components are developed by 
communities of developers and users 
I This community can provide a high-quality support: you can 
directly contact the main developers of the component you 
are using. The likelyhood of getting an answer doesn't depend 
what company you work for. 
I Often better than traditional support, but one needs to 
understand how the community works to properly use the 
community support possibilities 
I Allows to speed up the resolution of problems when 
developing your system 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 25/526
Taking part into the community 
I Possibility of taking part into the development community of 
some of the components used in the embedded systems: bug 
reporting, test of new versions or features, patches that
x 
bugs or add new features, etc. 
I Most of the time the open-source components are not the 
core value of the product: it's the interest of everybody to 
contribute back. 
I For the engineers: a very motivating way of being recognized 
outside the company, communication with others in the same
eld, opening of new possibilities, etc. 
I For the managers: motivation factor for engineers, allows 
the company to be recognized in the open-source community 
and therefore get support more easily and be more attractive 
to open-source developers 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 26/526
Introduction to Embedded Linux 
A few examples of embedded systems 
running Linux 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 27/526
Personal routers 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 28/526
Television 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 29/526
Point of sale terminal 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 30/526
Laser cutting machine 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 31/526
Viticulture machine 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 32/526
Introduction to Embedded Linux 
Embedded hardware for Linux 
systems 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 33/526
Processor and architecture (1) 
I The Linux kernel and most other architecture-dependent 
component support a wide range of 32 and 64 bits 
architectures 
I x86 and x86-64, as found on PC platforms, but also embedded 
systems (multimedia, industrial) 
I ARM, with hundreds of dierent SoC (multimedia, industrial) 
I PowerPC (mainly real-time, industrial applications) 
I MIPS (mainly networking applications) 
I SuperH (mainly set top box and multimedia applications) 
I Black
n (DSP architecture) 
I Microblaze (soft-core for Xilinx FPGA) 
I Cold
re, SCore, Tile, Xtensa, Cris, FRV, AVR32, M32R 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 34/526
Processor and architecture (2) 
I Both MMU and no-MMU architectures are supported, even 
though no-MMU architectures have a few limitations. 
I Linux is not designed for small microcontrollers. 
I Besides the toolchain, the bootloader and the kernel, all other 
components are generally architecture-independent 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 35/526
RAM and storage 
I 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. 
I Storage: a very basic Linux system can work within 4 MB of 
storage, but usually more is needed. 
I Flash storage is supported, both NAND and NOR 
ash, with 
speci
c
lesystems 
I Block storage including SD/MMC cards and eMMC is 
supported 
I Not necessarily interesting to be too restrictive on the amount 
of RAM/storage: having 
exibility at this level allows to 
re-use as many existing components as possible. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 36/526
Communication 
I The Linux kernel has support for many common 
communication buses 
I I2C 
I SPI 
I CAN 
I 1-wire 
I SDIO 
I USB 
I And also extensive networking support 
I Ethernet, Wi
, Bluetooth, CAN, etc. 
I IPv4, IPv6, TCP, UDP, SCTP, DCCP, etc. 
I Firewalling, advanced routing, multicast 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 37/526
Types of hardware platforms 
I Evaluation platforms from the SoC vendor. Usually 
expensive, but many peripherals are built-in. Generally 
unsuitable for real products. 
I Component on Module, a small board with only 
CPU/RAM/
ash and a few other core components, with 
connectors to access all other peripherals. Can be used to 
build end products for small to medium quantities. 
I Community development platforms, a new trend to make a 
particular SoC popular and easily available. These are 
ready-to-use and low cost, but usually have less peripherals 
than evaluation platforms. To some extent, can also be used 
for real products. 
I Custom platform. Schematics for evaluation boards or 
development platforms are more and more commonly freely 
available, making it easier to develop custom platforms. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 38/526
Criteria for choosing the hardware 
I 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. 
I Having support in the ocial versions of the projects (kernel, 
bootloader) is a lot better: quality is better, and new versions 
are available. 
I 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 ocial one. 
I Between properly supported hardware in the ocial 
Linux kernel and poorly-supported hardware, there will 
be huge dierences in development time and cost. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 39/526
Introduction to Embedded Linux 
Embedded Linux system architecture 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 40/526
Global architecture 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 41/526
Software components 
I Cross-compilation toolchain 
I Compiler that runs on the development machine, but generates 
code for the target 
I Bootloader 
I Started by the hardware, responsible for basic initialization, 
loading and executing the kernel 
I Linux Kernel 
I Contains the process and memory management, network stack, 
device drivers and provides services to user space applications 
I C library 
I The interface between the kernel and the user space 
applications 
I Libraries and applications 
I Third-party or in-house 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 42/526
Embedded Linux work 
Several distinct tasks are needed when deploying embedded Linux 
in a product: 
I Board Support Package development 
I A BSP contains a bootloader and kernel with the suitable 
device drivers for the targeted hardware 
I Purpose of our Kernel Development training 
I System integration 
I Integrate all the components, bootloader, kernel, third-party 
libraries and applications and in-house applications into a 
working system 
I Purpose of this training 
I Development of applications 
I Normal Linux applications, but using speci
cally chosen 
libraries 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 43/526
Embedded Linux development environment 
Embedded Linux 
development 
environment 
Free Electrons 
©Copyright 2004-2014, Free Electrons. 
Creative Commons BY-SA 3.0 license. 
Corrections, suggestions, contributions and translations are welcome! 
Embedded Linux 
Developers 
Free Electrons 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 44/526
Embedded Linux solutions 
I Two ways to switch to embedded Linux 
I Use solutions provided and supported by vendors like 
MontaVista, Wind River or TimeSys. These solutions come 
with their own development tools and environment. They use 
a mix of open-source components and proprietary tools. 
I Use community solutions. They are completely open, 
supported by the community. 
I In Free Electrons training sessions, we do not promote a 
particular vendor, and therefore use community solutions 
I However, knowing the concepts, switching to vendor solutions 
will be easy 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 45/526
OS for Linux development 
I We strongly recommend to use Linux as the desktop operating 
system to embedded Linux developers, for multiple reasons. 
I 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. 
I 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. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 46/526
Desktop Linux distribution 
I Any good and suciently recent Linux 
desktop distribution can be used for the 
development workstation 
I Ubuntu, Debian, Fedora, openSUSE, 
Red Hat, etc. 
I We have chosen Ubuntu, as it is a widely 
used and easy to use desktop Linux 
distribution 
I The Ubuntu setup on the training laptops 
has intentionally been left untouched after 
the normal installation process. Learning 
embedded Linux is also about learning the 
tools needed on the development 
workstation! 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 47/526
Linux root and non-root users 
I Linux is a multi-user operating system 
I The root user is the administrator, and it can do privileged 
operations such as: mounting
lesystems, con
guring the 
network, creating device
les, changing the system 
con
guration, installing or removing software 
I All other users are unprivileged, and cannot perform these 
administrator-level operations 
I On an Ubuntu system, it is not possible to log in as root, 
only as a normal user. 
I The system has been con
gured so that the user account 
created
rst is allowed to run privileged operations through a 
program called sudo. 
I Example: sudo mount /dev/sda2 /mnt/disk 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 48/526
Software packages 
I The distribution mechanism for software in GNU/Linux is 
dierent from the one in Windows 
I Linux distributions provides a central and coherent way of 
installing, updating and removing applications and libraries: 
packages 
I Packages contains the application or library
les, and 
associated meta-information, such as the version and the 
dependencies 
I .deb on Debian and Ubuntu, .rpm on Red Hat, Fedora, 
openSUSE 
I Packages are stored in repositories, usually on HTTP or FTP 
servers 
I You should only use packages from ocial repositories for 
your distribution, unless strictly required. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 49/526
Managing software packages (1) 
Instructions for Debian based GNU/Linux systems 
(Debian, Ubuntu...) 
I Package repositories are speci
ed in 
/etc/apt/sources.list 
I To update package repository lists: 
sudo apt-get update 
I To
nd the name of a package to install, the best is to use the 
search engine on http://packages.debian.org or on 
http://packages.ubuntu.com. You may also use: 
apt-cache search keyword 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 50/526
Managing software packages (2) 
I To install a given package: 
sudo apt-get install package 
I To remove a given package: 
sudo apt-get remove package 
I To install all available package updates: 
sudo apt-get dist-upgrade 
I Get information about a package: 
apt-cache show package 
I Graphical interfaces 
I Synaptic for GNOME 
I KPackageKit for KDE 
Further details on package management: 
http://www.debian.org/doc/manuals/apt-howto/ 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 51/526
Host vs. target 
I When doing embedded development, there is always a split 
between 
I The host, the development workstation, which is typically a 
powerful PC 
I The target, which is the embedded system under development 
I They are connected by various means: almost always a serial 
line for debugging purposes, frequently an Ethernet 
connection, sometimes a JTAG interface for low-level 
debugging 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 52/526
Serial line communication program 
I An essential tool for embedded development is a serial line 
communication program, like HyperTerminal in Windows. 
I There are multiple options available in Linux: Minicom, 
Picocom, Gtkterm, Putty, etc. 
I In this training session, we recommend using the simplest of 
them: picocom 
I Installation with sudo apt-get install picocom 
I Run with picocom -b BAUD_RATE /dev/SERIAL_DEVICE 
I Exit with Control-A Control-X 
I SERIAL_DEVICE is typically 
I ttyUSBx for USB to serial converters 
I ttySx for real serial ports 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 53/526
Command line tips 
I Using the command line is mandatory for many operations 
needed for embedded Linux development 
I It is a very powerful way of interacting with the system, with 
which you can save a lot of time. 
I Some useful tips 
I You can use several tabs in the Gnome Terminal 
I Remember that you can use relative paths (for example: 
../../linux) in addition to absolute paths (for example: 
/home/user) 
I 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 
I You can copy/paste paths directly from the
le manager to the 
terminal by drag-and-drop. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 54/526
Practical lab - Training Setup 
Prepare your lab environment 
I Download the lab archive 
I Enforce correct permissions 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 55/526
Cross-compiling toolchains 
Cross-compiling 
toolchains 
Free Electrons 
©Copyright 2004-2014, Free Electrons. 
Creative Commons BY-SA 3.0 license. 
Corrections, suggestions, contributions and translations are welcome! 
Embedded Linux 
Developers 
Free Electrons 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 56/526
Cross-compiling toolchains 
De
nition and Components 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 57/526
De
nition (1) 
I The usual development tools available on a GNU/Linux 
workstation is a native toolchain 
I This toolchain runs on your workstation and generates code 
for your workstation, usually x86 
I For embedded system development, it is usually impossible or 
not interesting to use a native toolchain 
I The target is too restricted in terms of storage and/or memory 
I The target is very slow compared to your workstation 
I You may not want to install all development tools on your 
target. 
I Therefore, cross-compiling toolchains are generally used. 
They run on your workstation but generate code for your 
target. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 58/526
De
nition (2) 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 59/526
Machines in build procedures 
I Three machines must be distinguished when discussing 
toolchain creation 
I The build machine, where the toolchain is built. 
I The host machine, where the toolchain will be executed. 
I The target machine, where the binaries created by the 
toolchain are executed. 
I Four common build types are possible for toolchains 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 60/526
Dierent toolchain build procedures 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 61/526
Components 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 62/526
Binutils 
I Binutils is a set of tools to generate and manipulate binaries 
for a given CPU architecture 
I as, the assembler, that generates binary code from assembler 
source code 
I ld, the linker 
I ar, ranlib, to generate .a archives, used for libraries 
I objdump, readelf, size, nm, strings, to inspect binaries. 
Very useful analysis tools! 
I strip, to strip useless parts of binaries in order to reduce their 
size 
I http://www.gnu.org/software/binutils/ 
I GPL license 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 63/526
Kernel headers (1) 
I The C library and compiled 
programs needs to interact with 
the kernel 
I Available system calls and their 
numbers 
I Constant de
nitions 
I Data structures, etc. 
I Therefore, compiling the C library 
requires kernel headers, and many 
applications also require them. 
I Available in linux/... and 
asm/... and a few other 
directories corresponding to the 
ones visible in include/ in the 
kernel sources 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 64/526
Kernel headers (2) 
I System call numbers, in asm/unistd.h 
#define __NR_exit 1 
#define __NR_fork 2 
#define __NR_read 3 
I Constant de
nitions, here in asm-generic/fcntl.h, 
included from asm/fcntl.h, included from 
linux/fcntl.h 
#define O_RDWR 00000002 
I Data structures, here in asm/stat.h 
struct stat { 
unsigned long st_dev; 
unsigned long st_ino; 
[...] 
}; 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 65/526
Kernel headers (3) 
I The kernel to user space ABI is backward compatible 
I Binaries generated with a toolchain using kernel headers older 
than the running kernel will work without problem, but won't 
be able to use the new system calls, data structures, etc. 
I Binaries generated with a toolchain using kernel headers newer 
than the running kernel might work on if they don't use the 
recent features, otherwise they will break 
I Using the latest kernel headers is not necessary, unless access 
to the new kernel features is needed 
I The kernel headers are extracted from the kernel sources using 
the headers_install kernel Make
le target. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 66/526
GCC 
I GNU Compiler Collection, the famous free 
software compiler 
I Can compile C, C++, Ada, Fortran, Java, 
Objective-C, Objective-C++, and generate code 
for a large number of CPU architectures, 
including ARM, AVR, Black
n, CRIS, FRV, 
M32, MIPS, MN10300, PowerPC, SH, v850, 
i386, x86 64, IA64, Xtensa, etc. 
I http://gcc.gnu.org/ 
I Available under the GPL license, libraries under 
the LGPL. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 67/526
C library 
I The C library is an essential component of 
a Linux system 
I Interface between the applications and 
the kernel 
I Provides the well-known standard C API 
to ease application development 
I Several C libraries are available: 
glibc, uClibc, eglibc, dietlibc, newlib, etc. 
I The choice of the C library must be made 
at the time of the cross-compiling 
toolchain generation, as the GCC compiler 
is compiled against a speci
c C library. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 68/526
Cross-compiling toolchains 
C Libraries 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 69/526
glibc 
I License: LGPL 
I C library from the GNU project 
I Designed for performance, standards 
compliance and portability 
I Found on all GNU / Linux host systems 
I Of course, actively maintained 
I Quite big for small embedded systems: 
approx 2.5 MB on ARM (version 2.9 - 
libc: 1.5 MB, libm: 750 KB) 
I http://www.gnu.org/software/libc/ 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 70/526
uClibc 
I License: LGPL 
I Lightweight C library for small embedded systems 
I High con
gurability: many features can be enabled or disabled 
through a menucon
g interface 
I Works only with Linux/uClinux, works on most embedded 
architectures 
I No guaranteed binary compatibility. May need to recompile 
applications when the library con
guration changes. 
I Focus on size rather than performance 
I Small compile time 
I http://www.uclibc.org/ 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 71/526
uClibc (2) 
I Most of the applications compile with uClibc. This applies to 
all applications used in embedded systems. 
I Size (arm): 4 times smaller than glibc! 
I uClibc 0.9.30.1: approx. 600 KB (libuClibc: 460 KB, libm: 
96KB) 
I glibc 2.9: approx 2.5 MB 
I Some features not available or limited: priority-inheritance 
mutexes, NPTL support is very new,
xed Name Service 
Switch functionality, etc. 
I Used on a large number of production embedded products, 
including consumer electronic devices 
I Supported by all commercial embedded Linux providers (proof 
of maturity). 
I Warning: though some development is still happening, the 
maintainers have stopped making releases since May 2012. 
The project is in trouble. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 72/526
eglibc 
I Embedded glibc, LGPL license too 
I Was a variant of glibc, better adapted to the 
needs of embedded systems, because of past 
disagreement with glibc maintainers. 
I eglibc's goals included reduced footprint, 
con
gurable components, better support for 
cross-compilation and cross-testing. 
I Could be built without support for NIS, locales, 
IPv6, and many other features. 
I Fortunately for eglibc, the glibc maintainer has 
changed and its features are now included in 
glibc. Version 2.19 (Feb. 2014) was the last 
release. 
I http: 
//en.wikipedia.org/wiki/Embedded_GLIBC 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 73/526
Honey, I shrunk the programs! 
I Executable size comparison on ARM, tested with eglibc 2.15 
and uClibc 0.9.33.2 
I Plain hello world program (stripped): 
helloworld static dynamic 
uClibc 18kB 2.5kB 
uClibc with Thumb-2 14kB 2.4kB 
eglibc with Thumb-2 361kB 2.7kB 
I Busybox (stripped): 
busybox static dynamic 
uClibc 750kB 603kB 
uClibc with Thumb-2 533kB 439kB 
eglibc with Thumb-2 934kB 444kB 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 74/526
Other smaller C libraries 
I Several other smaller C libraries have been developed, but 
none of them have the goal of allowing the compilation of 
large existing applications 
I They need specially written programs and applications 
I Choices: 
I Dietlibc, http://www.fefe.de/dietlibc/. Approximately 
70 KB. 
I Newlib, http://sourceware.org/newlib/ 
I Klibc, http://www.kernel.org/pub/linux/libs/klibc/, 
designed for use in an initramfs or initrd at boot time. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 75/526
Cross-compiling toolchains 
Toolchain Options 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 76/526
ABI 
I When building a toolchain, the ABI used to generate binaries 
needs to be de
ned 
I ABI, for Application Binary Interface, de
nes 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.) 
I All binaries in a system must be compiled with the same ABI, 
and the kernel must understand this ABI. 
I On ARM, two main ABIs: OABI and EABI 
I Nowadays everybody uses EABI 
I On MIPS, several ABIs: o32, o64, n32, n64 
I http://en.wikipedia.org/wiki/Application_Binary_ 
Interface 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 77/526
Floating point support 
I Some processors have a 
oating point unit, some others do 
not. 
I For example, many ARMv4 and ARMv5 CPUs do not have a 

oating point unit. Since ARMv7, a VFP unit is mandatory. 
I For processors having a 
oating point unit, the toolchain 
should generate hard 
oat code, in order to use the 
oating 
point instructions directly 
I For processors without a 
oating point unit, two solutions 
I Generate hard 
oat code and rely on the kernel to emulate the 

oating point instructions. This is very slow. 
I Generate soft 
oat code, so that instead of generating 
oating 
point instructions, calls to a user space library are generated 
I Decision taken at toolchain con
guration time 
I Also possible to con
gure which 
oating point unit should be 
used 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 78/526
CPU optimization 
ags 
I A set of cross-compiling tools is speci
c to a CPU 
architecture (ARM, x86, MIPS, PowerPC) 
I However, with the -march=, -mcpu=, -mtune= options, one 
can select more precisely the target CPU type 
I For example, -march=armv7 -mcpu=cortex-a8 
I At the toolchain compilation time, values can be chosen. 
They are used: 
I As the default values for the cross-compiling tools, when no 
other -march, -mcpu, -mtune options are passed 
I To compile the C library 
I Even if the C library has been compiled for armv5t, it doesn't 
prevent from compiling other programs for armv7 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 79/526
Cross-compiling toolchains 
Obtaining a Toolchain 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 80/526
Building a toolchain manually 
Building a cross-compiling toolchain by yourself is a dicult and 
painful task! Can take days or weeks! 
I Lots of details to learn: many components to build, 
complicated con
guration 
I Lots of decisions to make (such as C library version, ABI, 

oating point mechanisms, component versions) 
I Need kernel headers and C library sources 
I Need to be familiar with current gcc issues and patches on 
your platform 
I Useful to be familiar with building and con
guring tools 
I See the Crosstool-NG docs/ directory for details on how 
toolchains are built. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 81/526
Get a pre-compiled toolchain 
I Solution that many people choose 
I Advantage: it is the simplest and most convenient solution 
I Drawback: you can't
ne tune the toolchain to your needs 
I Determine what toolchain you need: CPU, endianism, C 
library, component versions, ABI, soft 
oat or hard 
oat, etc. 
I Check whether the available toolchains match your 
requirements. 
I Possible choices 
I Sourcery CodeBench toolchains 
I Linaro toolchains 
I More references at http://elinux.org/Toolchains 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 82/526
Sourcery CodeBench 
I CodeSourcery was a company with an extended expertise on 
free software toolchains: gcc, gdb, binutils and glibc. It has 
been bought by Mentor Graphics, which continues to provide 
similar services and products 
I They sell toolchains with support, but they also provide a 
Lite version, which is free and usable for commercial 
products 
I They have toolchains available for 
I ARM 
I MIPS 
I PowerPC 
I SuperH 
I x86 
I Be sure to use the GNU/Linux versions. The EABI versions 
are for bare-metal development (no operating system) 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 83/526
Linaro toolchains 
I Linaro contributes to improving mainline gcc on ARM, 
in particular by hiring CodeSourcery developers. 
I For people who can't wait for the next releases of gcc, 
Linaro releases modi
ed sources of stable releases of 
gcc, with these optimizations for ARM (mainly for 
recent Cortex A CPUs). 
I As any gcc release, these sources can be used by build 
tools to build their own binary toolchains (Buildroot, 
OpenEmbedded...) This allows to support glibc, uClibc 
and eglibc. 
I https://wiki.linaro.org/WorkingGroups/ToolChain 
I Binary packages are available for Ubuntu users, 
https://launchpad.net/~linaro-maintainers/+ 
archive/toolchain 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 84/526
Installing and using a pre-compiled toolchain 
I Follow the installation procedure proposed by the vendor 
I Usually, it is simply a matter of extracting a tarball wherever 
you want. 
I Then, add the path to toolchain binaries in your PATH: 
export PATH=/path/to/toolchain/bin/:$PATH 
I Finally, compile your applications 
PREFIX-gcc -o foobar foobar.c 
I PREFIX depends on the toolchain con
guration, and allows to 
distinguish cross-compilation tools from native compilation 
utilities 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 85/526
Toolchain building utilities 
Another solution is to use utilities that automate the process of 
building the toolchain 
I Same advantage as the pre-compiled toolchains: you don't 
need to mess up with all the details of the build process 
I But also oers more 
exibility in terms of toolchain 
con
guration, component version selection, etc. 
I They also usually contain several patches that
x known 
issues with the dierent components on some architectures 
I Multiple tools with identical principle: shell scripts or Make
le 
that automatically fetch, extract, con
gure, compile and 
install the dierent components 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 86/526
Toolchain building utilities (2) 
I Crosstool-ng 
I Rewrite of the older Crosstool, with a menucon
g-like 
con
guration system 
I Feature-full: supports uClibc, glibc, eglibc, hard and soft 
oat, 
many architectures 
I Actively maintained 
I http://crosstool-ng.org/ 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 87/526
Toolchain building utilities (3) 
Many root
lesystem build systems also allow the construction of a 
cross-compiling toolchain 
I Buildroot 
I Make
le-based, has a Crosstool-NG back-end, maintained by 
the community 
I http://www.buildroot.net 
I PTXdist 
I Make
le-based, uClibc or glibc, maintained mainly by 
Pengutronix 
I http://pengutronix.de/software/ptxdist/ 
I OpenEmbedded / Yocto 
I A featureful, but more complicated build system 
I http://www.openembedded.org/ 
I https://www.yoctoproject.org/ 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 88/526
Crosstool-NG: installation and usage 
I 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 
I Some sample con
gurations for various architectures are 
available in samples, they can be listed using 
./ct-ng list-samples 
I To load a sample con
guration 
./ct-ng sample-name 
I To adjust the con
guration 
./ct-ng menuconfig 
I To build the toolchain 
./ct-ng build 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 89/526
Toolchain contents 
I The cross compilation tool binaries, in bin/ 
I This directory can be added to your PATH to ease usage of the 
toolchain 
I One or several sysroot, each containing 
I The C library and related libraries, compiled for the target 
I The C library headers and kernel headers 
I There is one sysroot for each variant: toolchains can be 
multilib if they have several copies of the C library for dierent 
con
gurations (for example: ARMv4T, ARMv5T, etc.) 
I CodeSourcery ARM toolchain are multilib, the sysroots are in 
arm-none-linux-gnueabi/libc/, 
arm-none-linux-gnueabi/libc/armv4t/, 
arm-none-linux-gnueabi/libc/thumb2 
I Crosstool-NG toolchains can be multilib too (still 
experimental), otherwise the sysroot is in 
arm-unknown-linux-uclibcgnueabi/sysroot 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 90/526
Practical lab - Using Crosstool-NG 
Time to build your toolchain 
I Con
gure Crosstool-NG 
I Run it to build your own 
cross-compiling toolchain 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 91/526
Bootloaders 
Bootloaders 
Free Electrons 
©Copyright 2004-2014, Free Electrons. 
Creative Commons BY-SA 3.0 license. 
Corrections, suggestions, contributions and translations are welcome! 
Embedded Linux 
Developers 
Free Electrons 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 92/526
Bootloaders 
Boot Sequence 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 93/526
Bootloaders 
I The bootloader is a piece of code responsible for 
I Basic hardware initialization 
I Loading of an application binary, usually an operating system 
kernel, from 
ash storage, from the network, or from another 
type of non-volatile storage. 
I Possibly decompression of the application binary 
I Execution of the application 
I Besides these basic functions, most bootloaders provide a shell 
with various commands implementing dierent operations. 
I Loading of data from storage or network, memory inspection, 
hardware diagnostics and testing, etc. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 94/526
Bootloaders on x86 (1) 
I The x86 processors are typically bundled on a 
board with a non-volatile memory containing a 
program, the BIOS. 
I This program gets executed by the CPU after 
reset, and is responsible for basic hardware 
initialization and loading of a small piece of code 
from non-volatile storage. 
I This piece of code is usually the
rst 512 bytes 
of a storage device 
I This piece of code is usually a 1st stage 
bootloader, which will load the full bootloader 
itself. 
I The bootloader can then oer all its features. It 
typically understands
lesystem formats so that 
the kernel
le can be loaded directly from a 
normal
lesystem. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 95/526
Bootloaders on x86 (2) 
I GRUB, Grand Uni
ed Bootloader, the most powerful one. 
http://www.gnu.org/software/grub/ 
I Can read many
lesystem formats to load the kernel image and 
the con
guration, provides a powerful shell with various 
commands, can load kernel images over the network, etc. 
I See our dedicated presentation for details: 
http://free-electrons.com/docs/grub/ 
I Syslinux, for network and removable media booting (USB key, 
CD-ROM) 
http://www.kernel.org/pub/linux/utils/boot/syslinux/ 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 96/526
Booting on embedded CPUs: case 1 
I When powered, the CPU starts executing code 
at a
xed address 
I There is no other booting mechanism provided 
by the CPU 
I The hardware design must ensure that a NOR 

ash chip is wired so that it is accessible at the 
address at which the CPU starts executing 
instructions 
I The
rst stage bootloader must be programmed 
at this address in the NOR 
I NOR is mandatory, because it allows random 
access, which NAND doesn't allow 
I Not very common anymore (unpractical, and 
requires NOR 
ash) 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 97/526
Booting on embedded CPUs: case 2 
I The CPU has an integrated boot code in ROM 
I BootROM on AT91 CPUs, ROM code on OMAP, etc. 
I Exact details are CPU-dependent 
I This boot code is able to load a
rst stage bootloader from a 
storage device into an internal SRAM (DRAM not initialized 
yet) 
I Storage device can typically be: MMC, NAND, SPI 
ash, 
UART, etc. 
I The
rst stage bootloader is 
I Limited in size due to hardware constraints (SRAM size) 
I Provided either by the CPU vendor or through community 
projects 
I This
rst stage bootloader must initialize DRAM and other 
hardware devices and load a second stage bootloader into 
RAM 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 98/526
Booting on ARM Atmel AT91 
I RomBoot: tries to
nd a valid bootstrap image 
from various storage sources, and load it into 
SRAM (DRAM not initialized yet). Size limited 
to 4 KB. No user interaction possible in standard 
boot mode. 
I AT91Bootstrap: runs from SRAM. Initializes the 
DRAM, the NAND or SPI controller, and loads 
the secondary bootloader into RAM and starts it. 
No user interaction possible. 
I U-Boot: runs from RAM. Initializes some other 
hardware devices (network, USB, etc.). Loads the 
kernel image from storage or network to RAM 
and starts it. Shell with commands provided. 
I Linux Kernel: runs from RAM. Takes over the 
system completely (bootloaders no longer exists). 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 99/526
Booting on ARM TI OMAP3 
I ROM Code: tries to
nd a valid bootstrap image 
from various storage sources, and load it into 
SRAM or RAM (RAM can be initialized by ROM 
code through a con
guration header). Size 
limited to 64 KB. No user interaction possible. 
I X-Loader or U-Boot: runs from SRAM. 
Initializes the DRAM, the NAND or MMC 
controller, and loads the secondary bootloader 
into RAM and starts it. No user interaction 
possible. File called MLO. 
I U-Boot: runs from RAM. Initializes some other 
hardware devices (network, USB, etc.). Loads the 
kernel image from storage or network to RAM 
and starts it. Shell with commands provided. File 
called u-boot.bin or u-boot.img. 
I Linux Kernel: runs from RAM. Takes over the 
system completely (bootloaders no longer exists). 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 100/526
Booting on Marvell SoC 
I ROM Code: tries to
nd a valid bootstrap image 
from various storage sources, and load it into 
RAM. The RAM con
guration is described in a 
CPU-speci
c header, prepended to the bootloader 
image. 
I U-Boot: runs from RAM. Initializes some other 
hardware devices (network, USB, etc.). Loads the 
kernel image from storage or network to RAM 
and starts it. Shell with commands provided. File 
called u-boot.kwb. 
I Linux Kernel: runs from RAM. Takes over the 
system completely (bootloaders no longer exists). 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 101/526
Generic bootloaders for embedded CPUs 
I We will focus on the generic part, the main bootloader, 
oering the most important features. 
I There are several open-source generic bootloaders. 
Here are the most popular ones: 
I U-Boot, the universal bootloader by Denx 
The most used on ARM, also used on PPC, MIPS, x86, m68k, 
NIOS, etc. The de-facto standard nowadays. We will study it 
in detail. 
http://www.denx.de/wiki/U-Boot 
I Barebox, a new architecture-neutral bootloader, written as a 
successor of U-Boot. Better design, better code, active 
development, but doesn't yet have as much hardware support 
as U-Boot. 
http://www.barebox.org 
I There are also a lot of other open-source or proprietary 
bootloaders, often architecture-speci
c 
I RedBoot, Yaboot, PMON, etc. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 102/526
Bootloaders 
The U-boot bootloader 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 103/526
U-Boot 
U-Boot is a typical free software project 
I License: GPLv2 (same as Linux) 
I Freely available at http://www.denx.de/wiki/U-Boot 
I Documentation available at 
http://www.denx.de/wiki/U-Boot/Documentation 
I The latest development source code is available in a Git 
repository: 
http://git.denx.de/?p=u-boot.git;a=summary 
I Development and discussions happen around an open 
mailing-list http://lists.denx.de/pipermail/u-boot/ 
I Since the end of 2008, it follows a
xed-interval release 
schedule. Every two months, a new version is released. 
Versions are named YYYY.MM. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 104/526
U-Boot con
guration 
I Get the source code from the website, and uncompress it 
I The include/configs/ directory contains one con
guration
le for each supported board 
I It de
nes the CPU type, the peripherals and their 
con
guration, the memory mapping, the U-Boot features that 
should be compiled in, etc. 
I It is a simple .h
le that sets C pre-processor constants. See 
the README
le for the documentation of these constants. This
le can also be adjusted to add or remove features from 
U-Boot (commands, etc.). 
I Assuming that your board is already supported by U-Boot, 
there should be one entry corresponding to your board in the 
boards.cfg
le. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 105/526
U-Boot con
guration
le excerpt 
/* CPU configuration */ 
#define CONFIG_ARMV7 1 
#define CONFIG_OMAP 1 
#define CONFIG_OMAP34XX 1 
#define CONFIG_OMAP3430 1 
#define CONFIG_OMAP3_IGEP0020 1 
[...] 
/* Memory configuration */ 
#define CONFIG_NR_DRAM_BANKS 2 
#define PHYS_SDRAM_1 OMAP34XX_SDRC_CS0 
#define PHYS_SDRAM_1_SIZE (32  20) 
#define PHYS_SDRAM_2 OMAP34XX_SDRC_CS1 
[...] 
/* USB configuration */ 
#define CONFIG_MUSB_UDC 1 
#define CONFIG_USB_OMAP3 1 
#define CONFIG_TWL4030_USB 1 
[...] 
/* Available commands and features */ 
#define CONFIG_CMD_CACHE 
#define CONFIG_CMD_EXT2 
#define CONFIG_CMD_FAT 
#define CONFIG_CMD_I2C 
#define CONFIG_CMD_MMC 
#define CONFIG_CMD_NAND 
#define CONFIG_CMD_NET 
#define CONFIG_CMD_DHCP 
#define CONFIG_CMD_PING 
#define CONFIG_CMD_NFS 
#define CONFIG_CMD_MTDPARTS 
[...] 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 106/526
Con
guring and compiling U-Boot 
I U-Boot must be con
gured before being compiled 
I make BOARDNAME_config 
I Where BOARDNAME is the name of the board, as visible in the 
boards.cfg
le (
rst column). 
I Make sure that the cross-compiler is available in PATH 
I Compile U-Boot, by specifying the cross-compiler pre
x. 
Example, if your cross-compiler executable is arm-linux-gcc: 
make CROSS_COMPILE=arm-linux- 
I The main result is a u-boot.bin
le, which is the U-Boot 
image. Depending on your speci
c platform, there may be 
other specialized images: u-boot.img, u-boot.kwb, MLO, 
etc. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 107/526
Installing U-Boot 
I U-Boot must usually be installed in 
ash memory to be 
executed by the hardware. Depending on the hardware, the 
installation of U-Boot is done in a dierent way: 
I The CPU provides some kind of speci
c boot monitor with 
which you can communicate through serial port or USB using 
a speci
c protocol 
I The CPU boots
rst on removable media (MMC) before 
booting from
xed media (NAND). In this case, boot from 
MMC to re
ash a new version 
I U-Boot is already installed, and can be used to 
ash a new 
version of U-Boot. However, be careful: if the new version of 
U-Boot doesn't work, the board is unusable 
I The board provides a JTAG interface, which allows to write to 
the 
ash memory remotely, without any system running on the 
board. It also allows to rescue a board if the bootloader 
doesn't work. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 108/526
U-boot prompt 
I Connect the target to the host through a serial console 
I Power-up the board. On the serial console, you will see 
something like: 
U-Boot 2013.04 (May 29 2013 - 10:30:21) 
OMAP36XX/37XX-GP ES1.2, CPU-OPP2, L3-165MHz, Max CPU Clock 1 Ghz 
IGEPv2 + LPDDR/NAND 
I2C: ready 
DRAM: 512 MiB 
NAND: 512 MiB 
MMC: OMAP SD/MMC: 0 
Die ID #255000029ff800000168580212029011 
Net: smc911x-0 
U-Boot # 
I The U-Boot shell oers a set of commands. We will study the 
most important ones, see the documentation for a complete 
reference or the help command. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 109/526
Information commands 
Flash information (NOR and SPI 
ash) 
U-Boot flinfo 
DataFlash:AT45DB021 
Nb pages: 1024 
Page Size: 264 
Size= 270336 bytes 
Logical address: 0xC0000000 
Area 0: C0000000 to C0001FFF (RO) Bootstrap 
Area 1: C0002000 to C0003FFF Environment 
Area 2: C0004000 to C0041FFF (RO) U-Boot 
NAND 
ash information 
U-Boot nand info 
Device 0: nand0, sector size 128 KiB 
Page size 2048 b 
OOB size 64 b 
Erase size 131072 b 
Version details 
U-Boot version 
U-Boot 2013.04 (May 29 2013 - 10:30:21) 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 110/526
Important commands (1) 
I The exact set of commands depends on the U-Boot 
con
guration 
I help and help command 
I boot, runs the default boot command, stored in bootcmd 
I bootm address, starts a kernel image loaded at the given 
address in RAM 
I ext2load, loads a
le from an ext2
lesystem to RAM 
I And also ext2ls to list
les, ext2info for information 
I fatload, loads a
le from a FAT
lesystem to RAM 
I And also fatls and fatinfo 
I tftp, loads a
le from the network to RAM 
I ping, to test the network 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 111/526
Important commands (2) 
I loadb, loads, loady, load a
le from the serial line to RAM 
I usb, to initialize and control the USB subsystem, mainly used 
for USB storage devices such as USB keys 
I mmc, to initialize and control the MMC subsystem, used for 
SD and microSD cards 
I nand, to erase, read and write contents to NAND 
ash 
I erase, protect, cp, to erase, modify protection and write to 
NOR 
ash 
I md, displays memory contents. Can be useful to check the 
contents loaded in memory, or to look at hardware registers. 
I mm, modi
es memory contents. Can be useful to modify 
directly hardware registers, for testing purposes. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 112/526
Environment variables commands (1) 
I U-Boot can be con
gured through environment variables, 
which aect the behavior of the dierent commands. 
I Environment variables are loaded from 
ash to RAM at 
U-Boot startup, can be modi
ed and saved back to 
ash for 
persistence 
I There is a dedicated location in 
ash (or in MMC storage) to 
store the U-Boot environment, de
ned in the board 
con
guration
le 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 113/526
Environment variables commands (2) 
Commands to manipulate environment variables: 
I printenv 
Shows all variables 
I printenv variable-name 
Shows the value of a variable 
I setenv variable-name variable-value 
Changes the value of a variable, only in RAM 
I editenv variable-name 
Edits the value of a variable, only in RAM 
I saveenv 
Saves the current state of the environment to 
ash 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 114/526
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 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 115/526
Important U-Boot env variables 
I bootcmd, contains the command that U-Boot will 
automatically execute at boot time after a con
gurable delay, 
if the process is not interrupted 
I bootargs, contains the arguments passed to the Linux kernel, 
covered later 
I serverip, the IP address of the server that U-Boot will 
contact for network related commands 
I ipaddr, the IP address that U-Boot will use 
I netmask, the network mask to contact the server 
I ethaddr, the MAC address, can only be set once 
I bootdelay, the delay in seconds before which U-Boot runs 
bootcmd 
I autostart, if yes, U-Boot starts automatically an image that 
has been loaded into memory 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 116/526
Scripts in environment variables 
I Environment variables can contain small scripts, to execute 
several commands and test the results of commands. 
I Useful to automate booting or upgrade processes 
I Several commands can be chained using the ; operator 
I Tests can be done using 
if command ; then ... ; else ... ; fi 
I Scripts are executed using run variable-name 
I You can reference other variables using ${variable-name} 
I Example 
I setenv mmc-boot 'if fatload mmc 0 80000000 boot. 
ini; then source; else if fatload mmc 0 80000000 
uImage; then run mmc-bootargs; bootm; fi; fi' 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 117/526
Transferring
les to the target 
I U-Boot is mostly used to load and boot a kernel image, but it 
also allows to change the kernel image and the root
lesystem 
stored in 
ash. 
I Files must be exchanged between the target and the 
development workstation. This is possible: 
I 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 ecient solution. 
I Through a USB key, if U-Boot supports the USB controller of 
your platform 
I Through a SD or microSD card, if U-Boot supports the MMC 
controller of your platform 
I Through the serial port 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 118/526
TFTP 
I Network transfer from the development workstation and 
U-Boot on the target takes place through TFTP 
I Trivial File Transfer Protocol 
I Somewhat similar to FTP, but without authentication and over 
UDP 
I A TFTP server is needed on the development workstation 
I sudo apt-get install tftpd-hpa 
I All
les in /var/lib/tftpboot are then visible through TFTP 
I A TFTP client is available in the tftp-hpa package, for 
testing 
I A TFTP client is integrated into U-Boot 
I Con
gure the ipaddr and serverip environment variables 
I Use tftp address filename to load a
le 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 119/526
U-boot mkimage 
I The kernel image that U-Boot loads and boots must be 
prepared, so that a U-Boot speci
c header is added in front of 
the image 
I This header gives details such as the image size, the expected 
load address, the compression type, etc. 
I This is done with a tool that comes in U-Boot, mkimage 
I Debian / Ubuntu: just install the u-boot-tools package. 
I Or, compile it by yourself: simply con
gure U-Boot for any 
board of any architecture and compile it. Then install 
mkimage: 
cp tools/mkimage /usr/local/bin/ 
I The special target uImage of the kernel Make
le can then be 
used to generate a kernel image suitable for U-Boot. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 120/526
Practical lab - U-Boot 
Time to start the practical lab! 
I Communicate with the board using 
a serial console 
I Con
gure, build and install U-Boot 
I Learn U-Boot commands 
I Set up TFTP communication with 
the board 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 121/526
Linux kernel introduction 
Linux kernel 
introduction 
Free Electrons 
©Copyright 2004-2014, Free Electrons. 
Creative Commons BY-SA 3.0 license. 
Corrections, suggestions, contributions and translations are welcome! 
Embedded Linux 
Developers 
Free Electrons 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 122/526
Linux kernel introduction 
Linux features 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 123/526
History 
I The Linux kernel is one component of a system, which also 
requires libraries and applications to provide features to end 
users. 
I The Linux kernel was created as a hobby in 1991 by a Finnish 
student, Linus Torvalds. 
I Linux quickly started to be used as the kernel for free software 
operating systems 
I Linus Torvalds has been able to create a large and dynamic 
developer and user community around Linux. 
I Nowadays, more than one thousand people contribute to each 
kernel release, individuals or companies big and small. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 124/526
Linux kernel key features 
I Portability and hardware 
support. Runs on most 
architectures. 
I Scalability. Can run on 
super computers as well as 
on tiny devices (4 MB of 
RAM is enough). 
I Compliance to standards 
and interoperability. 
I Exhaustive networking 
support. 
I Security. It can't hide its 

aws. Its code is reviewed 
by many experts. 
I Stability and reliability. 
I Modularity. Can include 
only what a system needs 
even at run time. 
I Easy to program. You can 
learn from existing code. 
Many useful resources on 
the net. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 125/526
Linux kernel in the system 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 126/526
Linux kernel main roles 
I Manage all the hardware resources: CPU, memory, I/O. 
I Provide a set of portable, architecture and hardware 
independent APIs to allow user space applications and 
libraries to use the hardware resources. 
I Handle concurrent accesses and usage of hardware 
resources from dierent applications. 
I Example: a single network interface is used by multiple user 
space applications through various network connections. The 
kernel is responsible to multiplex the hardware resource. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 127/526
System calls 
I The main interface between the kernel and user space is the 
set of system calls 
I About 300 system calls that provide the main kernel services 
I File and device operations, networking operations, 
inter-process communication, process management, memory 
mapping, timers, threads, synchronization primitives, etc. 
I This interface is stable over time: only new system calls can 
be added by the kernel developers 
I This system call interface is wrapped by the C library, and 
user space applications usually never make a system call 
directly but rather use the corresponding C library function 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 128/526
Pseudo
lesystems 
I Linux makes system and kernel information available in user 
space through pseudo
lesystems, sometimes also called 
virtual
lesystems 
I Pseudo
lesystems allow applications to see directories and
les that do not exist on any real storage: they are created 
and updated on the 
y by the kernel 
I The two most important pseudo
lesystems are 
I proc, usually mounted on /proc: 
Operating system related information (processes, memory 
management parameters...) 
I sysfs, usually mounted on /sys: 
Representation of the system as a set of devices and buses. 
Information about these devices. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 129/526
Inside the Linux kernel 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 130/526
Linux license 
I The whole Linux sources are Free Software released under the 
GNU General Public License version 2 (GPL v2). 
I For the Linux kernel, this basically implies that: 
I 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. 
I When you produce Linux based devices, you must release the 
sources to the recipient, with the same rights, with no 
restriction. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 131/526
Supported hardware architectures 
I See the arch/ directory in the kernel sources 
I Minimum: 32 bit processors, with or without MMU, and gcc 
support 
I 32 bit architectures (arch/ subdirectories) 
Examples: arm, avr32, blackfin, c6x, m68k, microblaze, 
mips, score, sparc, um 
I 64 bit architectures: 
Examples: alpha, arm64, ia64, tile 
I 32/64 bit architectures 
Examples: powerpc, x86, sh, sparc 
I Find details in kernel sources: arch/arch/Kconfig, 
arch/arch/README, or Documentation/arch/ 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 132/526
Linux kernel introduction 
Linux versioning scheme and 
development process 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 133/526
Until 2.6 (1) 
I One stable major branch every 2 or 3 years 
I Identi
ed by an even middle number 
I Examples: 1.0.x, 2.0.x, 2.2.x, 2.4.x 
I One development branch to integrate new functionalities and 
major changes 
I Identi
ed by an odd middle number 
I Examples: 2.1.x, 2.3.x, 2.5.x 
I After some time, a development version becomes the new base 
version for the stable branch 
I Minor releases once in while: 2.2.23, 2.5.12, etc. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 134/526
Until 2.6 (2) 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 135/526
Changes since Linux 2.6 
I Since 2.6.0, kernel developers have been able to introduce 
lots of new features one by one on a steady pace, without 
having to make disruptive changes to existing subsystems. 
I Since then, there has been no need to create a new 
development branch massively breaking compatibility with the 
stable branch. 
I Thanks to this, more features are released to users at a 
faster pace. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 136/526
3.x stable branch 
I From 2003 to 2011, the ocial kernel versions were named 
2.6.x. 
I Linux 3.0 was released in July 2011 
I This is only a change to the numbering scheme 
I Ocial kernel versions are now named 3.x (3.0, 3.1, 3.2, 
etc.) 
I Stabilized versions are named 3.x.y (3.0.2, 3.4.3, etc.) 
I It eectively only removes a digit compared to the previous 
numbering scheme 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 137/526
New development model 
Using merge and bug
xing windows 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 138/526
New development model - Details 
I After the release of a 3.x version (for example), a two-weeks 
merge window opens, during which major additions are 
merged. 
I The merge window is closed by the release of test version 
3.(x+1)-rc1 
I The bug
xing period opens, for 6 to 10 weeks. 
I At regular intervals during the bug
xing period, 
3.(x+1)-rcY test versions are released. 
I When considered suciently stable, kernel 3.(x+1) is 
released, and the process starts again. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 139/526
More stability for the kernel source tree 
I Issue: bug and security
xes only released 
for most recent stable kernel versions. 
I Some people need to have a recent kernel, 
but with long term support for security 
updates. 
I You could get long term support from a 
commercial embedded Linux provider. 
I You could reuse sources for the kernel 
used in Ubuntu Long Term Support 
releases (5 years of free security updates). 
I The http://kernel.org front page 
shows which versions will be supported for 
some time (up to 2 or 3 years), and which 
ones won't be supported any more 
(EOL: End Of Life) 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 140/526
What's new in each Linux release? 
I The ocial list of changes for each Linux release is just a 
huge list of individual patches! 
commit aa6e52a35d388e730f4df0ec2ec48294590cc459 
Author: Thomas Petazzoni thomas.petazzoni@free-electrons.com 
Date: Wed Jul 13 11:29:17 2011 +0200 
at91: at91-ohci: support overcurrent notification 
Several USB power switches (AIC1526 or MIC2026) have a digital output 
that is used to notify that an overcurrent situation is taking 
place. This digital outputs are typically connected to GPIO inputs of 
the processor and can be used to be notified of these overcurrent 
situations. 
Therefore, we add a new overcurrent_pin[] array in the at91_usbh_data 
structure so that boards can tell the AT91 OHCI driver which pins are 
used for the overcurrent notification, and an overcurrent_supported 
boolean to tell the driver whether overcurrent is supported or not. 
The code has been largely borrowed from ohci-da8xx.c and 
ohci-s3c2410.c. 
Signed-off-by: Thomas Petazzoni thomas.petazzoni@free-electrons.com 
Signed-off-by: Nicolas Ferre nicolas.ferre@atmel.com 
I Very dicult to
nd out the key changes and to get the global 
picture out of individual changes. 
I Fortunately, there are some useful resources available 
I http://wiki.kernelnewbies.org/LinuxChanges 
I http://lwn.net 
I http://linuxfr.org, for French readers 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 141/526
Linux kernel introduction 
Linux kernel sources 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 142/526
Location of kernel sources 
I The ocial versions of the Linux kernel, as released by Linus 
Torvalds, are available at http://www.kernel.org 
I These versions follow the development model of the kernel 
I However, they may not contain the latest development from a 
speci
c area yet. Some features in development might not be 
ready for mainline inclusion yet. 
I Many chip vendors supply their own kernel sources 
I Focusing on hardware support
rst 
I Can have a very important delta with mainline Linux 
I Useful only when mainline hasn't caught up yet. 
I Many kernel sub-communities maintain their own kernel, with 
usually newer but less stable features 
I Architecture communities (ARM, MIPS, PowerPC, etc.), 
device drivers communities (I2C, SPI, USB, PCI, network, 
etc.), other communities (real-time, etc.) 
I No ocial releases, only development trees are available. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 143/526
Getting Linux sources 
I The kernel sources are available from 
http://kernel.org/pub/linux/kernel as full tarballs 
(complete kernel sources) and patches (dierences between 
two kernel versions). 
I However, more and more people use the git version control 
system. Absolutely needed for kernel development! 
I Fetch the entire kernel sources and history 
git clone git://git.kernel.org/pub/scm/linux/ 
kernel/git/torvalds/linux.git 
I Create a branch that starts at a speci
c stable version 
git checkout -b name-of-branch v3.11 
I Web interface available at http://git.kernel.org/cgit/ 
linux/kernel/git/torvalds/linux.git/tree/. 
I Read more about Git at http://git-scm.com/ 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 144/526
Linux kernel size (1) 
I Linux 3.10 sources: 
Raw size: 573 MB (43,000
les, approx 15,800,000 lines) 
gzip compressed tar archive: 105 MB 
bzip2 compressed tar archive: 83 MB (better) 
xz compressed tar archive: 69 MB (best) 
I Minimum Linux 3.17 compiled kernel size, booting on the 
ARM Versatile board (hard drive on PCI, ext2
lesystem, ELF 
executable support, framebuer console and input devices): 
876 KB (compressed), 2.3 MB (raw) 
I Why are these sources so big? 
Because they include thousands of device drivers, many 
network protocols, support many architectures and
lesystems... 
I The Linux core (scheduler, memory management...) is pretty 
small! 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 145/526
Linux kernel size (2) 
As of kernel version 3.10. 
I drivers/: 49.4% 
I arch/: 21.9% 
I fs/: 6.0% 
I include/: 4.7% 
I sound/: 4.4% 
I Documentation/: 4.0% 
I net/: 3.9% 
I firmware/: 1.0% 
I kernel/: 1.0% 
I tools/: 0.9% 
I scripts/: 0.5% 
I mm/: 0.5% 
I crypto/: 0.4% 
I security/: 0.4% 
I lib/: 0.4% 
I block/: 0.2% 
I ... 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 146/526
Getting Linux sources 
I Full tarballs 
I Contain the complete kernel sources: long to download and 
uncompress, but must be done at least once 
I Example: 
http://www.kernel.org/pub/linux/kernel/v3.x/linux- 
3.10.9.tar.xz 
I Extract command: 
tar xf linux-3.10.9.tar.xz 
I Incremental patches between versions 
I It assumes you already have a base version and you apply the 
correct patches in the right order. Quick to download and 
apply 
I Examples: 
http://www.kernel.org/pub/linux/kernel/v3.x/patch-3.10.xz 
(3.9 to 3.10) 
http://www.kernel.org/pub/linux/kernel/v3.x/patch-3.10.9.xz 
(3.10 to 3.10.9) 
I All previous kernel versions are available in 
http://kernel.org/pub/linux/kernel/ 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 147/526
Patch 
I A patch is the dierence between two source trees 
I Computed with the diff tool, or with more elaborate version 
control systems 
I They are very common in the open-source community 
I 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* 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 148/526
Contents of a patch 
I One section per modi
ed
le, 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 
I One sub-section per modi
ed part of the
le, starting with 
header with the aected line numbers 
@@ -1,7 +1,7 @@ 
I Three lines of context before the change 
VERSION = 2 
PATCHLEVEL = 6 
SUBLEVEL = 11 
I The change itself 
-EXTRAVERSION = 
+EXTRAVERSION = .1 
I Three lines of context after the change 
NAME=Woozy Numbat 
# *DOCUMENTATION* 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 149/526
Using the patch command 
The patch command: 
I Takes the patch contents on its standard input 
I Applies the modi
cations described by the patch into the 
current directory 
patch usage examples: 
I patch -pn  diff_file 
I cat diff_file | patch -pn 
I xzcat diff_file.xz | patch -pn 
I bzcat diff_file.bz2 | patch -pn 
I zcat diff_file.gz | patch -pn 
I Notes: 
I n: number of directory levels to skip in the
le paths 
I You can reverse apply a patch with the -R option 
I You can test a patch with --dry-run option 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 150/526
Applying a Linux patch 
I Two types of Linux patches: 
I Either to be applied to the previous stable version 
(from 3.x-1 to 3.x) 
I Or implementing
xes to the current stable version 
(from 3.x to 3.x.y) 
I Can be downloaded in gzip, bzip2 or xz (much smaller) 
compressed
les. 
I Always produced for n=1 
(that's what everybody does... do it too!) 
I Need to run the patch command inside the kernel source 
directory 
I Linux patch command line example: 
cd linux-3.9 
xzcat ../patch-3.10.xz | patch -p1 
xzcat ../patch-3.10.9.xz | patch -p1 
cd ..; mv linux-3.9 linux-3.10.9 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 151/526
Practical lab - Kernel sources 
Time to start the practical lab! 
I Get the Linux kernel sources 
I Apply patches 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 152/526
Linux kernel introduction 
Kernel con
guration 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 153/526
Kernel con
guration and build system 
I The kernel con
guration and build system is based on 
multiple Make
les 
I One only interacts with the main Makefile, present at the 
top directory of the kernel source tree 
I Interaction takes place 
I using the make tool, which parses the Make
le 
I through various targets, de
ning which action should be done 
(con
guration, compilation, installation, etc.). Run 
make help to see all available targets. 
I Example 
I cd linux-3.6.x/ 
I make target 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 154/526
Kernel con
guration (1) 
I The kernel contains thousands of device drivers,
lesystem 
drivers, network protocols and other con
gurable items 
I Thousands of options are available, that are used to 
selectively compile parts of the kernel source code 
I The kernel con
guration is the process of de
ning the set of 
options with which you want your kernel to be compiled 
I The set of options depends 
I On your hardware (for device drivers, etc.) 
I On the capabilities you would like to give to your kernel 
(network capabilities,
lesystems, real-time, etc.) 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 155/526
Kernel con
guration (2) 
I The con
guration is stored in the .config
le at the root of 
kernel sources 
I Simple text
le, key=value style 
I As options have dependencies, typically never edited by hand, 
but through graphical or text interfaces: 
I make xconfig, make gconfig (graphical) 
I make menuconfig, make nconfig (text) 
I You can switch from one to another, they all load/save the 
same .config
le, and show the same set of options 
I To modify a kernel in a GNU/Linux distribution: the 
con
guration
les are usually released in /boot/, together 
with kernel images: /boot/config-3.2.0-31-generic 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 156/526
Kernel or module? 
I The kernel image is a single
le, resulting from the linking 
of all object
les that correspond to features enabled in the 
con
guration 
I This is the
le that gets loaded in memory by the bootloader 
I All included features are therefore available as soon as the 
kernel starts, at a time where no
lesystem exists 
I Some features (device drivers,
lesystems, etc.) can however 
be compiled as modules 
I These are plugins that can be loaded/unloaded dynamically to 
add/remove features to the kernel 
I Each module is stored as a separate
le in the
lesystem, 
and therefore access to a
lesystem is mandatory to use 
modules 
I This is not possible in the early boot procedure of the kernel, 
because no
lesystem is available 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 157/526
Kernel option types 
I There are dierent types of options 
I bool options, they are either 
I true (to include the feature in the kernel) or 
I false (to exclude the feature from the kernel) 
I tristate options, they are either 
I true (to include the feature in the kernel image) or 
I module (to include the feature as a kernel module) or 
I false (to exclude the feature) 
I int options, to specify integer values 
I hex options, to specify hexadecimal values 
I string options, to specify string values 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 158/526
Kernel option dependencies 
I There are dependencies between kernel options 
I For example, enabling a network driver requires the network 
stack to be enabled 
I Two types of dependencies 
I depends on dependencies. In this case, option A that depends 
on option B is not visible until option B is enabled 
I select dependencies. In this case, with option A depending 
on option B, when option A is enabled, option B is 
automatically enabled 
I make xconfig allows to see all options, even the ones that 
cannot be selected because of missing dependencies. In this 
case, they are displayed in gray 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 159/526
make xcon
g 
make xconfig 
I The most common graphical interface to con
gure the kernel. 
I Make sure you read 
help - introduction: useful options! 
I File browser: easier to load con
guration
les 
I Search interface to look for parameters 
I Required Debian / Ubuntu packages: libqt4-dev g++ 
(libqt3-mt-dev for older kernel releases) 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 160/526
make xcon
g screenshot 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 161/526
make xcon
g search interface 
Looks for a keyword in the parameter name. Allows to select or 
unselect found parameters. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 162/526
Kernel con
guration options 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 163/526
Corresponding .con
g
le excerpt 
Options are grouped by sections and are pre
xed 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 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 164/526
make gcon
g 
make gconfig 
I GTK based graphical 
con
guration interface. 
Functionality similar to that 
of make xconfig. 
I Just lacking a search 
functionality. 
I Required Debian packages: 
libglade2-dev 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 165/526
make menucon
g 
make menuconfig 
I Useful when no graphics are 
available. Pretty convenient 
too! 
I Same interface found in 
other tools: BusyBox, 
Buildroot... 
I Required Debian packages: 
libncurses-dev 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 166/526
make ncon
g 
make nconfig 
I A newer, similar text 
interface 
I More user friendly (for 
example, easier to access 
help information). 
I Required Debian packages: 
libncurses-dev 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 167/526
make oldcon
g 
make oldconfig 
I Needed very often! 
I Useful to upgrade a .config
le from an earlier kernel release 
I Issues warnings for con
guration parameters that no longer 
exist in the new kernel. 
I Asks for values for new parameters 
If you edit a .config
le by hand, it's strongly recommended to 
run make oldconfig afterwards! 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 168/526
Undoing con
guration changes 
A frequent problem: 
I After changing several kernel con
guration settings, your 
kernel no longer works. 
I If you don't remember all the changes you made, you can get 
back to your previous con
guration: 
$ cp .config.old .config 
I All the con
guration interfaces of the kernel (xconfig, 
menuconfig, oldconfig...) keep this .config.old backup 
copy. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 169/526
Con
guration per architecture 
I The set of con
guration options is architecture dependent 
I Some con
guration options are very architecture-speci
c 
I Most of the con
guration options (global kernel options, 
network subsystem,
lesystems, most of the device drivers) are 
visible in all architectures. 
I By default, the kernel build system assumes that the kernel is 
being built for the host architecture, i.e. native compilation 
I The architecture is not de
ned inside the con
guration, but at 
a higher level 
I We will see later how to override this behaviour, to allow the 
con
guration of kernels for a dierent architecture 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 170/526
Linux kernel introduction 
Compiling and installing the kernel 
for the host system 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 171/526
Kernel compilation 
I make 
I in the main kernel source directory 
I Remember to run multiple jobs in parallel if you have multiple 
CPU cores. Example: make -j 4 
I No need to run as root! 
I Generates 
I vmlinux, the raw uncompressed kernel image, at the ELF 
format, useful for debugging purposes, but cannot be booted 
I arch/arch/boot/*Image, the
nal, usually compressed, 
kernel image that can be booted 
I bzImage for x86, zImage for ARM, vmImage.gz for Black
n, 
etc. 
I arch/arch/boot/dts/*.dtb, compiled Device Tree
les 
(on some architectures) 
I All kernel modules, spread over the kernel source tree, as .ko
les. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 172/526
Kernel installation 
I make install 
I Does the installation for the host system by default, so needs 
to be run as root. Generally not used when compiling for an 
embedded system, and it installs
les on the development 
workstation. 
I Installs 
I /boot/vmlinuz-version 
Compressed kernel image. Same as the one in 
arch/arch/boot 
I /boot/System.map-version 
Stores kernel symbol addresses 
I /boot/config-version 
Kernel con
guration for this version 
I Typically re-runs the bootloader con
guration utility to take 
the new kernel into account. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 173/526
Module installation 
I make modules_install 
I Does the installation for the host system by default, so needs 
to be run as root 
I Installs all modules in /lib/modules/version/ 
I kernel/ 
Module .ko (Kernel Object)
les, in the same directory 
structure as in the sources. 
I modules.alias 
Module aliases for module loading utilities. Example line: 
alias sound-service-?-0 snd_mixer_oss 
I modules.dep 
Module dependencies 
I modules.symbols 
Tells which module a given symbol belongs to. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 174/526
Kernel cleanup targets 
I Clean-up generated
les (to force 
re-compilation): 
make clean 
I Remove all generated
les. Needed when 
switching from one architecture to another. 
Caution: it also removes your .config
le! 
make mrproper 
I Also remove editor backup and patch reject
les 
(mainly to generate patches): 
make distclean 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 175/526
Linux kernel introduction 
Cross-compiling the kernel 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 176/526
Cross-compiling the kernel 
When you compile a Linux kernel for another CPU architecture 
I Much faster than compiling natively, when the target system 
is much slower than your GNU/Linux workstation. 
I Much easier as development tools for your GNU/Linux 
workstation are much easier to
nd. 
I To make the dierence with a native compiler, cross-compiler 
executables are pre
xed by the name of the target system, 
architecture and sometimes library. Examples: 
mips-linux-gcc, the pre
x is mips-linux-arm- 
linux-gnueabi-gcc, the pre
x is arm-linux-gnueabi- 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 177/526
Specifying cross-compilation (1) 
The CPU architecture and cross-compiler pre
x are de
ned through 
the ARCH and CROSS_COMPILE variables in the toplevel Make
le. 
I ARCH is the name of the architecture. It is de
ned by the 
name of the subdirectory in arch/ in the kernel sources 
I Example: arm if you want to compile a kernel for the arm 
architecture. 
I CROSS_COMPILE is the pre
x of the cross compilation tools 
I Example: arm-linux- if your compiler is arm-linux-gcc 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 178/526
Specifying cross-compilation (2) 
Two solutions to de
ne ARCH and CROSS_COMPILE: 
I Pass ARCH and CROSS_COMPILE on the make command line: 
make ARCH=arm CROSS_COMPILE=arm-linux- ... 
Drawback: it is easy to forget to pass these variables when 
you run any make command, causing your build and 
con
guration to be screwed up. 
I De
ne ARCH and CROSS_COMPILE as environment variables: 
export ARCH=arm 
export CROSS_COMPILE=arm-linux- 
Drawback: it only works inside the current shell or terminal. 
You could put these settings in a
le that you source every 
time you start working on the project. If you only work on a 
single architecture with always the same toolchain, you could 
even put these settings in your ~/.bashrc
le to make them 
permanent and visible from any terminal. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 179/526
Prede
ned con
guration
les 
I Default con
guration
les available, per board or per-CPU 
family 
I They are stored in arch/arch/configs/, and are just 
minimal .config
les 
I This is the most common way of con
guring a kernel for 
embedded platforms 
I Run make help to
nd if one is available for your platform 
I To load a default con
guration
le, just run 
make acme_defconfig 
I This will overwrite your existing .config
le! 
I To create your own default con
guration
le 
I make savedefconfig, to create a minimal con
guration
le 
I mv defconfig arch/arch/configs/myown_defconfig 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 180/526
Con
guring the kernel 
I After loading a default con
guration
le, you can adjust the 
con
guration to your needs with the normal xconfig, 
gconfig or menuconfig interfaces 
I You can also start the con
guration from scratch without 
loading a default con
guration
le 
I As the architecture is dierent from your host architecture 
I Some options will be dierent from the native con
guration 
(processor and architecture speci
c options, speci
c drivers, 
etc.) 
I Many options will be identical (
lesystems, network protocols, 
architecture-independent drivers, etc.) 
I Make sure you have the support for the right CPU, the right 
board and the right device drivers. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 181/526
Device Tree 
I Many embedded architectures have a lot of non-discoverable 
hardware. 
I Depending on the architecture, such hardware is either 
described using C code directly within the kernel, or using a 
special hardware description language in a Device Tree. 
I ARM, PowerPC, OpenRISC, ARC, Microblaze are examples of 
architectures using the Device Tree. 
I A Device Tree Source, written by kernel developers, is 
compiled into a binary Device Tree Blob, passed at boot time 
to the kernel. 
I There is one dierent Device Tree for each board/platform 
supported by the kernel, available in 
arch/arm/boot/dts/board.dtb. 
I The bootloader must load both the kernel image and the 
Device Tree Blob in memory before starting the kernel. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 182/526
Building and installing the kernel 
I Run make 
I Copy the
nal kernel image to the target storage 
I can be uImage, zImage, vmlinux, bzImage in 
arch/arch/boot 
I copying the Device Tree Blob might be necessary as well, they 
are available in arch/arch/boot/dts 
I make install is rarely used in embedded development, as 
the kernel image is a single
le, easy to handle 
I It is however possible to customize the make install behaviour 
in arch/arch/boot/install.sh 
I make modules_install is used even in embedded 
development, as it installs many modules and description
les 
I make INSTALL_MOD_PATH=dir/ modules_install 
I The INSTALL_MOD_PATH variable is needed to install the 
modules in the target root
lesystem instead of your host root
lesystem. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 183/526
Booting with U-Boot 
I Recent versions of U-Boot can boot the zImage binary. 
I Older versions require a special kernel image format: uImage 
I uImage is generated from zImage using the mkimage tool. It 
is done automatically by the kernel make uImage target. 
I On some ARM platforms, make uImage requires passing a 
LOADADDR environment variable, which indicates at which 
physical memory address the kernel will be executed. 
I In addition to the kernel image, U-Boot can also pass a 
Device Tree Blob to the kernel. 
I The typical boot process is therefore: 
1. Load zImage or uImage at address X in memory 
2. Load board.dtb at address Y in memory 
3. Start the kernel with bootz X - Y or bootm X - Y 
The - in the middle indicates no initramfs 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 184/526
Kernel command line 
I In addition to the compile time con
guration, the kernel 
behaviour can be adjusted with no recompilation using the 
kernel command line 
I The kernel command line is a string that de
nes various 
arguments to the kernel 
I It is very important for system con
guration 
I root= for the root
lesystem (covered later) 
I console= for the destination of kernel messages 
I Many more exist. The most important ones are documented in 
Documentation/kernel-parameters.txt in kernel sources. 
I This kernel command line is either 
I Passed by the bootloader. In U-Boot, the contents of the 
bootargs environment variable is automatically passed to the 
kernel 
I Built into the kernel, using the CONFIG_CMDLINE option. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 185/526
Practical lab - Kernel cross-compiling 
I Set up the cross-compiling 
environment 
I Con
gure the kernel Makefile 
accordingly 
I Cross-compile the kernel for an arm 
platform 
I On this platform, interact with the 
bootloader and boot your kernel 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 186/526
Linux kernel introduction 
Using kernel modules 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 187/526
Advantages of modules 
I Modules make it easy to develop drivers without rebooting: 
load, test, unload, rebuild, load... 
I Useful to keep the kernel image size to the minimum 
(essential in GNU/Linux distributions for PCs). 
I Also useful to reduce boot time: you don't spend time 
initializing devices and kernel features that you only need later. 
I 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. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 188/526
Module dependencies 
I Some kernel modules can depend on other modules, which 
need to be loaded
rst. 
I Example: the usb-storage module depends on the 
scsi_mod, libusual and usbcore modules. 
I Dependencies are described in 
/lib/modules/kernel-version/modules.dep 
This
le is generated when you run make modules_install. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 189/526
Kernel log 
When a new module is loaded, related information is available in 
the kernel log. 
I The kernel keeps its messages in a circular buer (so that it 
doesn't consume more memory with many messages) 
I Kernel log messages are available through the dmesg 
command (diagnostic message) 
I Kernel log messages are also displayed in the system console 
(console messages can be
ltered by level using the loglevel 
kernel parameter, or completely disabled with the quiet 
parameter). 
I Note that you can write to the kernel log from user space too: 
echo nDebug info  /dev/kmsg 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 190/526
Module utilities (1) 
I modinfo module_name 
modinfo module_path.ko 
Gets information about a module: parameters, license, 
description and dependencies. 
Very useful before deciding to load a module or not. 
I sudo insmod module_path.ko 
Tries to load the given module. The full path to the module 
object
le must be given. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 191/526
Understanding module loading issues 
I When loading a module fails, insmod often doesn't give you 
enough details! 
I Details are often available in the kernel log. 
I 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 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 192/526
Module utilities (2) 
I sudo modprobe module_name 
Most common usage of modprobe: tries to load all the 
modules the given module depends on, and then this module. 
Lots of other options are available. modprobe automatically 
looks in /lib/modules/version/ for the object
le 
corresponding to the given module name. 
I lsmod 
Displays the list of loaded modules 
Compare its output with the contents of /proc/modules! 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 193/526
Module utilities (3) 
I sudo rmmod module_name 
Tries to remove the given module. 
Will only be allowed if the module is no longer in use (for 
example, no more processes opening a device
le) 
I sudo modprobe -r module_name 
Tries to remove the given module and all dependent modules 
(which are no longer needed after removing the module) 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 194/526
Passing parameters to modules 
I Find available parameters: 
modinfo snd-intel8x0m 
I Through insmod: 
sudo insmod ./snd-intel8x0m.ko index=-2 
I Through modprobe: 
Set parameters in /etc/modprobe.conf or in any
le in 
/etc/modprobe.d/: 
options snd-intel8x0m index=-2 
I Through the kernel command line, when the driver is built 
statically into the kernel: 
snd-intel8x0m.index=-2 
I snd-intel8x0m is the driver name 
I index is the driver parameter name 
I -2 is the driver parameter value 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 195/526
Check module parameter values 
How to
nd the current values for the parameters of a loaded 
module? 
I Check /sys/module/name/parameters. 
I There is one
le per parameter, containing the parameter 
value. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 196/526
Useful reading 
Linux Kernel in a Nutshell, Dec 2006 
I By Greg Kroah-Hartman, O'Reilly 
http://www.kroah.com/lkn/ 
I A good reference book and guide on 
con
guring, compiling and managing the 
Linux kernel sources. 
I Freely available on-line! 
Great companion to the printed book for 
easy electronic searches! 
Available as single PDF
le on 
http://free-electrons.com/ 
community/kernel/lkn/ 
I Our rating: 2 stars 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 197/526
Linux Root Filesystem 
Linux Root 
Filesystem 
Free Electrons 
©Copyright 2004-2014, Free Electrons. 
Creative Commons BY-SA 3.0 license. 
Corrections, suggestions, contributions and translations are welcome! 
Embedded Linux 
Developers 
Free Electrons 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 198/526
Linux Root Filesystem 
Principle and solutions 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 199/526
Filesystems 
I Filesystems are used to organize data in directories and
les 
on storage devices or on the network. The directories and
les 
are organized as a hierarchy 
I In Unix systems, applications and users see a single global 
hierarchy of
les and directories, which can be composed of 
several
lesystems. 
I Filesystems are mounted in a speci
c location in this 
hierarchy of directories 
I When a
lesystem is mounted in a directory (called mount 
point), the contents of this directory re
ects the contents of 
the storage device 
I When the
lesystem is unmounted, the mount point is empty 
again. 
I This allows applications to access
les and directories easily, 
regardless of their exact storage location 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 200/526
Filesystems (2) 
I Create a mount point, which is just a directory 
$ mkdir /mnt/usbkey 
I It is empty 
$ ls /mnt/usbkey 
$ 
I Mount a storage device in this mount point 
$ mount -t vfat /dev/sda1 /mnt/usbkey 
$ 
I You can access the contents of the USB key 
$ ls /mnt/usbkey 
docs prog.c picture.png movie.avi 
$ 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 201/526
mount / umount 
I mount allows to mount
lesystems 
I mount -t type device mountpoint 
I type is the type of
lesystem 
I device is the storage device, or network location to mount 
I mountpoint is the directory where
les of the storage device 
or network location will be accessible 
I mount with no arguments shows the currently mounted
lesystems 
I umount allows to unmount
lesystems 
I This is needed before rebooting, or before unplugging a USB 
key, because the Linux kernel caches writes in memory to 
increase performance. umount makes sure that these writes are 
committed to the storage. 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 202/526
Root
lesystem 
I A particular
lesystem is mounted at the root of the hierarchy, 
identi
ed by / 
I This
lesystem is called the root
lesystem 
I As mount and umount are programs, they are
les inside a
lesystem. 
I They are not accessible before mounting at least one
lesystem. 
I As the root
lesystem is the
rst mounted
lesystem, it 
cannot be mounted with the normal mount command 
I It is mounted directly by the kernel, according to the root= 
kernel option 
I When no root
lesystem is available, the kernel panics 
Please append a correct root= boot option 
Kernel panic - not syncing: VFS: Unable to mount root fs on unknown block(0,0) 
Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 203/526
Location of the root
lesystem 
I It can be mounted from dierent locations 
I From the partition of a hard disk 
I From the partition of a USB key 
I From the partition of an SD card 
I From the partition of a NAND 
ash chip or similar type of 
storage device 
I From the network, using the NFS protocol 
I From memory, using a pre-loaded

Embedded linux system development (slides)

  • 1.
    Embedded Linux systemdevelopment Embedded Linux system development Free Electrons ©Copyright 2004-2014, Free Electrons. Creative Commons BY-SA 3.0 license. Latest update: October 20, 2014. Document updates and sources: http://free-electrons.com/doc/training/embedded-linux Corrections, suggestions, contributions and translations are welcome! Embedded Linux Developers Free Electrons Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 1/526
  • 2.
    Rights to copy © Copyright 2004-2014, Free Electrons License: Creative Commons Attribution - Share Alike 3.0 http://creativecommons.org/licenses/by-sa/3.0/legalcode You are free: I to copy, distribute, display, and perform the work I to make derivative works I to make commercial use of the work Under the following conditions: I Attribution. You must give the original author credit. I Share Alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under a license identical to this one. I For any reuse or distribution, you must make clear to others the license terms of this work. I Any of these conditions can be waived if you get permission from the copyright holder. Your fair use and other rights are in no way aected by the above. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 2/526
  • 3.
    Electronic copies ofthese documents I Electronic copies of your particular version of the materials are available on: http://free-electrons.com/doc/training/embedded-linux I Open the corresponding documents and use them throughout the course to look for explanations given earlier by the instructor. I You will need these electronic versions because we neither print any index nor any table of contents (quite high environmental cost for little added value) I For future reference, see the
  • 4.
    rst slide tosee where document updates will be available. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 3/526
  • 5.
    Hyperlinks in thedocument There are many hyperlinks in the document I Regular hyperlinks: http://kernel.org/ I Kernel documentation links: Documentation/kmemcheck.txt I Links to kernel source
  • 6.
    les and directories: drivers/input include/linux/fb.h I Links to the declarations, de
  • 7.
    nitions and instancesof kernel symbols (functions, types, data, structures): platform_get_irq() GFP_KERNEL struct file_operations Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 4/526
  • 8.
    Free Electrons ata glance I Engineering company created in 2004 (not a training company!) I Locations: Orange, Toulouse, Lyon (France) I Serving customers all around the world See http://free-electrons.com/company/customers/ I Head count: 9 Only Free Software enthusiasts! I Focus: Embedded Linux, Linux kernel, Android Free Software / Open Source for embedded and real-time systems. I Activities: development, training, consulting, technical support. I Added value: get the best of the user and development community and the resources it oers. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 5/526
  • 9.
    Free Electrons on-lineresources I All our training materials: http://free-electrons.com/docs/ I Technical blog: http://free-electrons.com/blog/ I Quarterly newsletter: http://lists.free-electrons. com/mailman/listinfo/newsletter I News and discussions (Google +): https://plus.google.com/+FreeElectronsDevelopers I News and discussions (LinkedIn): http://linkedin.com/groups/Free-Electrons-4501089 I Quick news (Twitter): http://twitter.com/free_electrons I Linux Cross Reference - browse Linux kernel sources on-line: http://lxr.free-electrons.com Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 6/526
  • 10.
    Mailing lists forsession participants If you participated to a course taught by Free Electrons, you can subscribe to our mailing lists dedicated to our training customers: I To ask questions about the topics covered by the courses, and to get a
  • 11.
    rst level oftechnical support for issues that your are facing in real life or with your own hardware. I To share your experience with other people having taken the same course and done the same practical labs. I To share further comments and suggestions about course contents. I To get news about important updates to the course materials. See http://free-electrons.com/training/mailing-lists A simple way to register:
  • 12.
    ll the formon http://j.mp/1r1HhkZ Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 7/526
  • 13.
    Generic course information Generic course information Free Electrons ©Copyright 2004-2014, Free Electrons. Creative Commons BY-SA 3.0 license. Corrections, suggestions, contributions and translations are welcome! Embedded Linux Developers Free Electrons Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 8/526
  • 14.
    Hardware used inthis training session Using Atmel SAMA5D3 Xplained boards in all practical labs I SAMA5D36 (Cortex A5) CPU from Atmel I USB powered! I 256 MB DDR2 RAM, 256 MB NAND ash I 2 Ethernet ports (Gigabit + 100 Mbit) I 2 USB 2.0 host, 1 USB device I 1 MMC/SD slot I 3.3 V serial port (like Beaglebone Black) I Misc: Arduino R3-compatible header, JTAG, buttons, LEDs I Currently sold at 69 EUR by Mouser (V.A.T. not included) Board and CPU documentation, design
  • 15.
    les, software: http://www.atmel.com/sama5d3xplained Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 9/526
  • 16.
    Participate! During thelectures... I Don't hesitate to ask questions. Other people in the audience may have similar questions too. I This helps the trainer to detect any explanation that wasn't clear or detailed enough. I Don't hesitate to share your experience, for example to compare Linux / Android with other operating systems used in your company. I Your point of view is most valuable, because it can be similar to your colleagues' and dierent from the trainer's. I Your participation can make our session more interactive and make the topics easier to learn. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 10/526
  • 17.
    Practical lab guidelines During practical labs... I We cannot support more than 8 workstations at once (each with its board and equipment). Having more would make the whole class progress slower, compromising the coverage of the whole training agenda (exception for public sessions: up to 10 people). I So, if you are more than 8 participants, please form up to 8 working groups. I Open the electronic copy of your lecture materials, and use it throughout the practical labs to
  • 18.
    nd the slidesyou need again. I Don't copy and paste from the PDF slides. The slides contain UTF-8 characters that look the same as ASCII ones, but won't be understood by shells or compilers. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 11/526
  • 19.
    Cooperate! As inthe Free Software and Open Source community, cooperation during practical labs is valuable in this training session: I 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. I Explain what you understood to other participants when needed. It also helps to consolidate your knowledge. I Don't hesitate to report potential bugs to your instructor. I Don't hesitate to look for solutions on the Internet as well. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 12/526
  • 20.
    Command memento sheet I This memento sheet gives command examples for the most typical needs (looking for
  • 21.
    les, extracting atar archive...) I It saves us 1 day of UNIX / Linux command line training. I Our best tip: in the command line shell, always hit the Tab key to complete command names and
  • 22.
    le paths. Thisavoids 95% of typing mistakes. I Get an electronic copy on http://free-electrons.com/ doc/training/embedded-linux/ command_memento.pdf Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 13/526
  • 23.
    vi basic commands I The vi editor is very useful to make quick changes to
  • 24.
    les in an embedded target. I Though not very user friendly at
  • 25.
    rst, vi isvery powerful and its main 15 commands are easy to learn and are sucient for 99% of everyone's needs! I Get an electronic copy on http://free-electrons.com/ doc/training/embedded-linux/ vi_memento.pdf I You can also take the quick tutorial by running vimtutor. This is a worthy investment! Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 14/526
  • 26.
    Introduction to EmbeddedLinux Introduction to Embedded Linux Free Electrons ©Copyright 2004-2014, Free Electrons. Creative Commons BY-SA 3.0 license. Corrections, suggestions, contributions and translations are welcome! Embedded Linux Developers Free Electrons Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 15/526
  • 27.
    Birth of freesoftware I 1983, Richard Stallman, GNU project and the free software concept. Beginning of the development of gcc, gdb, glibc and other important tools I 1991, Linus Torvalds, Linux kernel project, a Unix-like operating system kernel. Together with GNU software and many other open-source components: a completely free operating system, GNU/Linux I 1995, Linux is more and more popular on server systems I 2000, Linux is more and more popular on embedded systems I 2008, Linux is more and more popular on mobile devices I 2010, Linux is more and more popular on phones Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 16/526
  • 28.
    Free software? IA program is considered free when its license oers to all its users the following four freedoms I Freedom to run the software for any purpose I Freedom to study the software and to change it I Freedom to redistribute copies I Freedom to distribute copies of modi
  • 29.
    ed versions IThese freedoms are granted for both commercial and non-commercial use I They imply the availability of source code, software can be modi
  • 30.
    ed and distributedto customers I Good match for embedded systems! Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 17/526
  • 31.
    What is embeddedLinux? Embedded Linux is the usage of the Linux kernel and various open-source components in embedded systems Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 18/526
  • 32.
    Introduction to EmbeddedLinux Advantages of Linux and open-source for embedded systems Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 19/526
  • 33.
    Re-using components IThe key advantage of Linux and open-source in embedded systems is the ability to re-use components I The open-source ecosystem already provides many components for standard features, from hardware support to network protocols, going through multimedia, graphic, cryptographic libraries, etc. I 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. I Allows to quickly design and develop complicated products, based on existing components. I No-one should re-develop yet another operating system kernel, TCP/IP stack, USB stack or another graphical toolkit library. I Allows to focus on the added value of your product. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 20/526
  • 34.
    Low cost IFree software can be duplicated on as many devices as you want, free of charge. I 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. I Allows to have a higher budget for the hardware or to increase the company's skills and knowledge Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 21/526
  • 35.
    Full control IWith open-source, you have the source code for all components in your system I Allows unlimited modi
  • 36.
    cations, changes, tuning,debugging, optimization, for an unlimited period of time I Without lock-in or dependency from a third-party vendor I To be true, non open-source components must be avoided when the system is designed and developed I Allows to have full control over the software part of your system Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 22/526
  • 37.
    Quality I Manyopen-source components are widely used, on millions of systems I Usually higher quality than what an in-house development can produce, or even proprietary vendors I Of course, not all open-source components are of good quality, but most of the widely-used ones are. I Allows to design your system with high-quality components at the foundations Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 23/526
  • 38.
    Eases testing ofnew features I Open-source being freely available, it is easy to get a piece of software and evaluate it I Allows to easily study several options while making a choice I Much easier than purchasing and demonstration procedures needed with most proprietary products I Allows to easily explore new possibilities and solutions Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 24/526
  • 39.
    Community support IOpen-source software components are developed by communities of developers and users I This community can provide a high-quality support: you can directly contact the main developers of the component you are using. The likelyhood of getting an answer doesn't depend what company you work for. I Often better than traditional support, but one needs to understand how the community works to properly use the community support possibilities I Allows to speed up the resolution of problems when developing your system Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 25/526
  • 40.
    Taking part intothe community I Possibility of taking part into the development community of some of the components used in the embedded systems: bug reporting, test of new versions or features, patches that
  • 41.
    x bugs oradd new features, etc. I Most of the time the open-source components are not the core value of the product: it's the interest of everybody to contribute back. I For the engineers: a very motivating way of being recognized outside the company, communication with others in the same
  • 42.
    eld, opening ofnew possibilities, etc. I For the managers: motivation factor for engineers, allows the company to be recognized in the open-source community and therefore get support more easily and be more attractive to open-source developers Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 26/526
  • 43.
    Introduction to EmbeddedLinux A few examples of embedded systems running Linux Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 27/526
  • 44.
    Personal routers FreeElectrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 28/526
  • 45.
    Television Free Electrons- Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 29/526
  • 46.
    Point of saleterminal Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 30/526
  • 47.
    Laser cutting machine Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 31/526
  • 48.
    Viticulture machine FreeElectrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 32/526
  • 49.
    Introduction to EmbeddedLinux Embedded hardware for Linux systems Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 33/526
  • 50.
    Processor and architecture(1) I The Linux kernel and most other architecture-dependent component support a wide range of 32 and 64 bits architectures I x86 and x86-64, as found on PC platforms, but also embedded systems (multimedia, industrial) I ARM, with hundreds of dierent SoC (multimedia, industrial) I PowerPC (mainly real-time, industrial applications) I MIPS (mainly networking applications) I SuperH (mainly set top box and multimedia applications) I Black
  • 51.
    n (DSP architecture) I Microblaze (soft-core for Xilinx FPGA) I Cold
  • 52.
    re, SCore, Tile,Xtensa, Cris, FRV, AVR32, M32R Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 34/526
  • 53.
    Processor and architecture(2) I Both MMU and no-MMU architectures are supported, even though no-MMU architectures have a few limitations. I Linux is not designed for small microcontrollers. I Besides the toolchain, the bootloader and the kernel, all other components are generally architecture-independent Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 35/526
  • 54.
    RAM and storage I 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. I Storage: a very basic Linux system can work within 4 MB of storage, but usually more is needed. I Flash storage is supported, both NAND and NOR ash, with speci
  • 55.
  • 56.
    lesystems I Blockstorage including SD/MMC cards and eMMC is supported I Not necessarily interesting to be too restrictive on the amount of RAM/storage: having exibility at this level allows to re-use as many existing components as possible. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 36/526
  • 57.
    Communication I TheLinux kernel has support for many common communication buses I I2C I SPI I CAN I 1-wire I SDIO I USB I And also extensive networking support I Ethernet, Wi
  • 58.
    , Bluetooth, CAN,etc. I IPv4, IPv6, TCP, UDP, SCTP, DCCP, etc. I Firewalling, advanced routing, multicast Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 37/526
  • 59.
    Types of hardwareplatforms I Evaluation platforms from the SoC vendor. Usually expensive, but many peripherals are built-in. Generally unsuitable for real products. I Component on Module, a small board with only CPU/RAM/ ash and a few other core components, with connectors to access all other peripherals. Can be used to build end products for small to medium quantities. I Community development platforms, a new trend to make a particular SoC popular and easily available. These are ready-to-use and low cost, but usually have less peripherals than evaluation platforms. To some extent, can also be used for real products. I Custom platform. Schematics for evaluation boards or development platforms are more and more commonly freely available, making it easier to develop custom platforms. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 38/526
  • 60.
    Criteria for choosingthe hardware I 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. I Having support in the ocial versions of the projects (kernel, bootloader) is a lot better: quality is better, and new versions are available. I 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 ocial one. I Between properly supported hardware in the ocial Linux kernel and poorly-supported hardware, there will be huge dierences in development time and cost. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 39/526
  • 61.
    Introduction to EmbeddedLinux Embedded Linux system architecture Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 40/526
  • 62.
    Global architecture FreeElectrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 41/526
  • 63.
    Software components ICross-compilation toolchain I Compiler that runs on the development machine, but generates code for the target I Bootloader I Started by the hardware, responsible for basic initialization, loading and executing the kernel I Linux Kernel I Contains the process and memory management, network stack, device drivers and provides services to user space applications I C library I The interface between the kernel and the user space applications I Libraries and applications I Third-party or in-house Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 42/526
  • 64.
    Embedded Linux work Several distinct tasks are needed when deploying embedded Linux in a product: I Board Support Package development I A BSP contains a bootloader and kernel with the suitable device drivers for the targeted hardware I Purpose of our Kernel Development training I System integration I Integrate all the components, bootloader, kernel, third-party libraries and applications and in-house applications into a working system I Purpose of this training I Development of applications I Normal Linux applications, but using speci
  • 65.
    cally chosen libraries Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 43/526
  • 66.
    Embedded Linux developmentenvironment Embedded Linux development environment Free Electrons ©Copyright 2004-2014, Free Electrons. Creative Commons BY-SA 3.0 license. Corrections, suggestions, contributions and translations are welcome! Embedded Linux Developers Free Electrons Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 44/526
  • 67.
    Embedded Linux solutions I Two ways to switch to embedded Linux I Use solutions provided and supported by vendors like MontaVista, Wind River or TimeSys. These solutions come with their own development tools and environment. They use a mix of open-source components and proprietary tools. I Use community solutions. They are completely open, supported by the community. I In Free Electrons training sessions, we do not promote a particular vendor, and therefore use community solutions I However, knowing the concepts, switching to vendor solutions will be easy Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 45/526
  • 68.
    OS for Linuxdevelopment I We strongly recommend to use Linux as the desktop operating system to embedded Linux developers, for multiple reasons. I 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. I 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. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 46/526
  • 69.
    Desktop Linux distribution I Any good and suciently recent Linux desktop distribution can be used for the development workstation I Ubuntu, Debian, Fedora, openSUSE, Red Hat, etc. I We have chosen Ubuntu, as it is a widely used and easy to use desktop Linux distribution I The Ubuntu setup on the training laptops has intentionally been left untouched after the normal installation process. Learning embedded Linux is also about learning the tools needed on the development workstation! Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 47/526
  • 70.
    Linux root andnon-root users I Linux is a multi-user operating system I The root user is the administrator, and it can do privileged operations such as: mounting
  • 71.
  • 72.
    guring the network,creating device
  • 73.
  • 74.
    guration, installing orremoving software I All other users are unprivileged, and cannot perform these administrator-level operations I On an Ubuntu system, it is not possible to log in as root, only as a normal user. I The system has been con
  • 75.
    gured so thatthe user account created
  • 76.
    rst is allowedto run privileged operations through a program called sudo. I Example: sudo mount /dev/sda2 /mnt/disk Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 48/526
  • 77.
    Software packages IThe distribution mechanism for software in GNU/Linux is dierent from the one in Windows I Linux distributions provides a central and coherent way of installing, updating and removing applications and libraries: packages I Packages contains the application or library
  • 78.
    les, and associatedmeta-information, such as the version and the dependencies I .deb on Debian and Ubuntu, .rpm on Red Hat, Fedora, openSUSE I Packages are stored in repositories, usually on HTTP or FTP servers I You should only use packages from ocial repositories for your distribution, unless strictly required. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 49/526
  • 79.
    Managing software packages(1) Instructions for Debian based GNU/Linux systems (Debian, Ubuntu...) I Package repositories are speci
  • 80.
    ed in /etc/apt/sources.list I To update package repository lists: sudo apt-get update I To
  • 81.
    nd the nameof a package to install, the best is to use the search engine on http://packages.debian.org or on http://packages.ubuntu.com. You may also use: apt-cache search keyword Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 50/526
  • 82.
    Managing software packages(2) I To install a given package: sudo apt-get install package I To remove a given package: sudo apt-get remove package I To install all available package updates: sudo apt-get dist-upgrade I Get information about a package: apt-cache show package I Graphical interfaces I Synaptic for GNOME I KPackageKit for KDE Further details on package management: http://www.debian.org/doc/manuals/apt-howto/ Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 51/526
  • 83.
    Host vs. target I When doing embedded development, there is always a split between I The host, the development workstation, which is typically a powerful PC I The target, which is the embedded system under development I They are connected by various means: almost always a serial line for debugging purposes, frequently an Ethernet connection, sometimes a JTAG interface for low-level debugging Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 52/526
  • 84.
    Serial line communicationprogram I An essential tool for embedded development is a serial line communication program, like HyperTerminal in Windows. I There are multiple options available in Linux: Minicom, Picocom, Gtkterm, Putty, etc. I In this training session, we recommend using the simplest of them: picocom I Installation with sudo apt-get install picocom I Run with picocom -b BAUD_RATE /dev/SERIAL_DEVICE I Exit with Control-A Control-X I SERIAL_DEVICE is typically I ttyUSBx for USB to serial converters I ttySx for real serial ports Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 53/526
  • 85.
    Command line tips I Using the command line is mandatory for many operations needed for embedded Linux development I It is a very powerful way of interacting with the system, with which you can save a lot of time. I Some useful tips I You can use several tabs in the Gnome Terminal I Remember that you can use relative paths (for example: ../../linux) in addition to absolute paths (for example: /home/user) I 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 I You can copy/paste paths directly from the
  • 86.
    le manager tothe terminal by drag-and-drop. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 54/526
  • 87.
    Practical lab -Training Setup Prepare your lab environment I Download the lab archive I Enforce correct permissions Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 55/526
  • 88.
    Cross-compiling toolchains Cross-compiling toolchains Free Electrons ©Copyright 2004-2014, Free Electrons. Creative Commons BY-SA 3.0 license. Corrections, suggestions, contributions and translations are welcome! Embedded Linux Developers Free Electrons Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 56/526
  • 89.
  • 90.
    nition and Components Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 57/526
  • 91.
  • 92.
    nition (1) IThe usual development tools available on a GNU/Linux workstation is a native toolchain I This toolchain runs on your workstation and generates code for your workstation, usually x86 I For embedded system development, it is usually impossible or not interesting to use a native toolchain I The target is too restricted in terms of storage and/or memory I The target is very slow compared to your workstation I You may not want to install all development tools on your target. I Therefore, cross-compiling toolchains are generally used. They run on your workstation but generate code for your target. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 58/526
  • 93.
  • 94.
    nition (2) FreeElectrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 59/526
  • 95.
    Machines in buildprocedures I Three machines must be distinguished when discussing toolchain creation I The build machine, where the toolchain is built. I The host machine, where the toolchain will be executed. I The target machine, where the binaries created by the toolchain are executed. I Four common build types are possible for toolchains Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 60/526
  • 96.
    Dierent toolchain buildprocedures Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 61/526
  • 97.
    Components Free Electrons- Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 62/526
  • 98.
    Binutils I Binutilsis a set of tools to generate and manipulate binaries for a given CPU architecture I as, the assembler, that generates binary code from assembler source code I ld, the linker I ar, ranlib, to generate .a archives, used for libraries I objdump, readelf, size, nm, strings, to inspect binaries. Very useful analysis tools! I strip, to strip useless parts of binaries in order to reduce their size I http://www.gnu.org/software/binutils/ I GPL license Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 63/526
  • 99.
    Kernel headers (1) I The C library and compiled programs needs to interact with the kernel I Available system calls and their numbers I Constant de
  • 100.
    nitions I Datastructures, etc. I Therefore, compiling the C library requires kernel headers, and many applications also require them. I Available in linux/... and asm/... and a few other directories corresponding to the ones visible in include/ in the kernel sources Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 64/526
  • 101.
    Kernel headers (2) I System call numbers, in asm/unistd.h #define __NR_exit 1 #define __NR_fork 2 #define __NR_read 3 I Constant de
  • 102.
    nitions, here inasm-generic/fcntl.h, included from asm/fcntl.h, included from linux/fcntl.h #define O_RDWR 00000002 I Data structures, here in asm/stat.h struct stat { unsigned long st_dev; unsigned long st_ino; [...] }; Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 65/526
  • 103.
    Kernel headers (3) I The kernel to user space ABI is backward compatible I Binaries generated with a toolchain using kernel headers older than the running kernel will work without problem, but won't be able to use the new system calls, data structures, etc. I Binaries generated with a toolchain using kernel headers newer than the running kernel might work on if they don't use the recent features, otherwise they will break I Using the latest kernel headers is not necessary, unless access to the new kernel features is needed I The kernel headers are extracted from the kernel sources using the headers_install kernel Make
  • 104.
    le target. FreeElectrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 66/526
  • 105.
    GCC I GNUCompiler Collection, the famous free software compiler I Can compile C, C++, Ada, Fortran, Java, Objective-C, Objective-C++, and generate code for a large number of CPU architectures, including ARM, AVR, Black
  • 106.
    n, CRIS, FRV, M32, MIPS, MN10300, PowerPC, SH, v850, i386, x86 64, IA64, Xtensa, etc. I http://gcc.gnu.org/ I Available under the GPL license, libraries under the LGPL. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 67/526
  • 107.
    C library IThe C library is an essential component of a Linux system I Interface between the applications and the kernel I Provides the well-known standard C API to ease application development I Several C libraries are available: glibc, uClibc, eglibc, dietlibc, newlib, etc. I The choice of the C library must be made at the time of the cross-compiling toolchain generation, as the GCC compiler is compiled against a speci
  • 108.
    c C library. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 68/526
  • 109.
    Cross-compiling toolchains CLibraries Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 69/526
  • 110.
    glibc I License:LGPL I C library from the GNU project I Designed for performance, standards compliance and portability I Found on all GNU / Linux host systems I Of course, actively maintained I Quite big for small embedded systems: approx 2.5 MB on ARM (version 2.9 - libc: 1.5 MB, libm: 750 KB) I http://www.gnu.org/software/libc/ Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 70/526
  • 111.
    uClibc I License:LGPL I Lightweight C library for small embedded systems I High con
  • 112.
    gurability: many featurescan be enabled or disabled through a menucon
  • 113.
    g interface IWorks only with Linux/uClinux, works on most embedded architectures I No guaranteed binary compatibility. May need to recompile applications when the library con
  • 114.
    guration changes. IFocus on size rather than performance I Small compile time I http://www.uclibc.org/ Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 71/526
  • 115.
    uClibc (2) IMost of the applications compile with uClibc. This applies to all applications used in embedded systems. I Size (arm): 4 times smaller than glibc! I uClibc 0.9.30.1: approx. 600 KB (libuClibc: 460 KB, libm: 96KB) I glibc 2.9: approx 2.5 MB I Some features not available or limited: priority-inheritance mutexes, NPTL support is very new,
  • 116.
    xed Name Service Switch functionality, etc. I Used on a large number of production embedded products, including consumer electronic devices I Supported by all commercial embedded Linux providers (proof of maturity). I Warning: though some development is still happening, the maintainers have stopped making releases since May 2012. The project is in trouble. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 72/526
  • 117.
    eglibc I Embeddedglibc, LGPL license too I Was a variant of glibc, better adapted to the needs of embedded systems, because of past disagreement with glibc maintainers. I eglibc's goals included reduced footprint, con
  • 118.
    gurable components, bettersupport for cross-compilation and cross-testing. I Could be built without support for NIS, locales, IPv6, and many other features. I Fortunately for eglibc, the glibc maintainer has changed and its features are now included in glibc. Version 2.19 (Feb. 2014) was the last release. I http: //en.wikipedia.org/wiki/Embedded_GLIBC Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 73/526
  • 119.
    Honey, I shrunkthe programs! I Executable size comparison on ARM, tested with eglibc 2.15 and uClibc 0.9.33.2 I Plain hello world program (stripped): helloworld static dynamic uClibc 18kB 2.5kB uClibc with Thumb-2 14kB 2.4kB eglibc with Thumb-2 361kB 2.7kB I Busybox (stripped): busybox static dynamic uClibc 750kB 603kB uClibc with Thumb-2 533kB 439kB eglibc with Thumb-2 934kB 444kB Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 74/526
  • 120.
    Other smaller Clibraries I Several other smaller C libraries have been developed, but none of them have the goal of allowing the compilation of large existing applications I They need specially written programs and applications I Choices: I Dietlibc, http://www.fefe.de/dietlibc/. Approximately 70 KB. I Newlib, http://sourceware.org/newlib/ I Klibc, http://www.kernel.org/pub/linux/libs/klibc/, designed for use in an initramfs or initrd at boot time. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 75/526
  • 121.
    Cross-compiling toolchains ToolchainOptions Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 76/526
  • 122.
    ABI I Whenbuilding a toolchain, the ABI used to generate binaries needs to be de
  • 123.
    ned I ABI,for Application Binary Interface, de
  • 124.
    nes 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.) I All binaries in a system must be compiled with the same ABI, and the kernel must understand this ABI. I On ARM, two main ABIs: OABI and EABI I Nowadays everybody uses EABI I On MIPS, several ABIs: o32, o64, n32, n64 I http://en.wikipedia.org/wiki/Application_Binary_ Interface Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 77/526
  • 125.
    Floating point support I Some processors have a oating point unit, some others do not. I For example, many ARMv4 and ARMv5 CPUs do not have a oating point unit. Since ARMv7, a VFP unit is mandatory. I For processors having a oating point unit, the toolchain should generate hard oat code, in order to use the oating point instructions directly I For processors without a oating point unit, two solutions I Generate hard oat code and rely on the kernel to emulate the oating point instructions. This is very slow. I Generate soft oat code, so that instead of generating oating point instructions, calls to a user space library are generated I Decision taken at toolchain con
  • 126.
    guration time IAlso possible to con
  • 127.
    gure which oatingpoint unit should be used Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 78/526
  • 128.
    CPU optimization ags I A set of cross-compiling tools is speci
  • 129.
    c to aCPU architecture (ARM, x86, MIPS, PowerPC) I However, with the -march=, -mcpu=, -mtune= options, one can select more precisely the target CPU type I For example, -march=armv7 -mcpu=cortex-a8 I At the toolchain compilation time, values can be chosen. They are used: I As the default values for the cross-compiling tools, when no other -march, -mcpu, -mtune options are passed I To compile the C library I Even if the C library has been compiled for armv5t, it doesn't prevent from compiling other programs for armv7 Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 79/526
  • 130.
    Cross-compiling toolchains Obtaininga Toolchain Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 80/526
  • 131.
    Building a toolchainmanually Building a cross-compiling toolchain by yourself is a dicult and painful task! Can take days or weeks! I Lots of details to learn: many components to build, complicated con
  • 132.
    guration I Lotsof decisions to make (such as C library version, ABI, oating point mechanisms, component versions) I Need kernel headers and C library sources I Need to be familiar with current gcc issues and patches on your platform I Useful to be familiar with building and con
  • 133.
    guring tools ISee the Crosstool-NG docs/ directory for details on how toolchains are built. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 81/526
  • 134.
    Get a pre-compiledtoolchain I Solution that many people choose I Advantage: it is the simplest and most convenient solution I Drawback: you can't
  • 135.
    ne tune thetoolchain to your needs I Determine what toolchain you need: CPU, endianism, C library, component versions, ABI, soft oat or hard oat, etc. I Check whether the available toolchains match your requirements. I Possible choices I Sourcery CodeBench toolchains I Linaro toolchains I More references at http://elinux.org/Toolchains Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 82/526
  • 136.
    Sourcery CodeBench ICodeSourcery was a company with an extended expertise on free software toolchains: gcc, gdb, binutils and glibc. It has been bought by Mentor Graphics, which continues to provide similar services and products I They sell toolchains with support, but they also provide a Lite version, which is free and usable for commercial products I They have toolchains available for I ARM I MIPS I PowerPC I SuperH I x86 I Be sure to use the GNU/Linux versions. The EABI versions are for bare-metal development (no operating system) Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 83/526
  • 137.
    Linaro toolchains ILinaro contributes to improving mainline gcc on ARM, in particular by hiring CodeSourcery developers. I For people who can't wait for the next releases of gcc, Linaro releases modi
  • 138.
    ed sources ofstable releases of gcc, with these optimizations for ARM (mainly for recent Cortex A CPUs). I As any gcc release, these sources can be used by build tools to build their own binary toolchains (Buildroot, OpenEmbedded...) This allows to support glibc, uClibc and eglibc. I https://wiki.linaro.org/WorkingGroups/ToolChain I Binary packages are available for Ubuntu users, https://launchpad.net/~linaro-maintainers/+ archive/toolchain Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 84/526
  • 139.
    Installing and usinga pre-compiled toolchain I Follow the installation procedure proposed by the vendor I Usually, it is simply a matter of extracting a tarball wherever you want. I Then, add the path to toolchain binaries in your PATH: export PATH=/path/to/toolchain/bin/:$PATH I Finally, compile your applications PREFIX-gcc -o foobar foobar.c I PREFIX depends on the toolchain con
  • 140.
    guration, and allowsto distinguish cross-compilation tools from native compilation utilities Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 85/526
  • 141.
    Toolchain building utilities Another solution is to use utilities that automate the process of building the toolchain I Same advantage as the pre-compiled toolchains: you don't need to mess up with all the details of the build process I But also oers more exibility in terms of toolchain con
  • 142.
    guration, component versionselection, etc. I They also usually contain several patches that
  • 143.
    x known issueswith the dierent components on some architectures I Multiple tools with identical principle: shell scripts or Make
  • 144.
    le that automaticallyfetch, extract, con
  • 145.
    gure, compile and install the dierent components Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 86/526
  • 146.
    Toolchain building utilities(2) I Crosstool-ng I Rewrite of the older Crosstool, with a menucon
  • 147.
  • 148.
    guration system IFeature-full: supports uClibc, glibc, eglibc, hard and soft oat, many architectures I Actively maintained I http://crosstool-ng.org/ Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 87/526
  • 149.
  • 150.
    lesystem build systemsalso allow the construction of a cross-compiling toolchain I Buildroot I Make
  • 151.
    le-based, has aCrosstool-NG back-end, maintained by the community I http://www.buildroot.net I PTXdist I Make
  • 152.
    le-based, uClibc orglibc, maintained mainly by Pengutronix I http://pengutronix.de/software/ptxdist/ I OpenEmbedded / Yocto I A featureful, but more complicated build system I http://www.openembedded.org/ I https://www.yoctoproject.org/ Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 88/526
  • 153.
    Crosstool-NG: installation andusage I 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 I Some sample con
  • 154.
    gurations for variousarchitectures are available in samples, they can be listed using ./ct-ng list-samples I To load a sample con
  • 155.
    guration ./ct-ng sample-name I To adjust the con
  • 156.
    guration ./ct-ng menuconfig I To build the toolchain ./ct-ng build Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 89/526
  • 157.
    Toolchain contents IThe cross compilation tool binaries, in bin/ I This directory can be added to your PATH to ease usage of the toolchain I One or several sysroot, each containing I The C library and related libraries, compiled for the target I The C library headers and kernel headers I There is one sysroot for each variant: toolchains can be multilib if they have several copies of the C library for dierent con
  • 158.
    gurations (for example:ARMv4T, ARMv5T, etc.) I CodeSourcery ARM toolchain are multilib, the sysroots are in arm-none-linux-gnueabi/libc/, arm-none-linux-gnueabi/libc/armv4t/, arm-none-linux-gnueabi/libc/thumb2 I Crosstool-NG toolchains can be multilib too (still experimental), otherwise the sysroot is in arm-unknown-linux-uclibcgnueabi/sysroot Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 90/526
  • 159.
    Practical lab -Using Crosstool-NG Time to build your toolchain I Con
  • 160.
    gure Crosstool-NG IRun it to build your own cross-compiling toolchain Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 91/526
  • 161.
    Bootloaders Bootloaders FreeElectrons ©Copyright 2004-2014, Free Electrons. Creative Commons BY-SA 3.0 license. Corrections, suggestions, contributions and translations are welcome! Embedded Linux Developers Free Electrons Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 92/526
  • 162.
    Bootloaders Boot Sequence Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 93/526
  • 163.
    Bootloaders I Thebootloader is a piece of code responsible for I Basic hardware initialization I Loading of an application binary, usually an operating system kernel, from ash storage, from the network, or from another type of non-volatile storage. I Possibly decompression of the application binary I Execution of the application I Besides these basic functions, most bootloaders provide a shell with various commands implementing dierent operations. I Loading of data from storage or network, memory inspection, hardware diagnostics and testing, etc. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 94/526
  • 164.
    Bootloaders on x86(1) I The x86 processors are typically bundled on a board with a non-volatile memory containing a program, the BIOS. I This program gets executed by the CPU after reset, and is responsible for basic hardware initialization and loading of a small piece of code from non-volatile storage. I This piece of code is usually the
  • 165.
    rst 512 bytes of a storage device I This piece of code is usually a 1st stage bootloader, which will load the full bootloader itself. I The bootloader can then oer all its features. It typically understands
  • 166.
    lesystem formats sothat the kernel
  • 167.
    le can beloaded directly from a normal
  • 168.
    lesystem. Free Electrons- Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 95/526
  • 169.
    Bootloaders on x86(2) I GRUB, Grand Uni
  • 170.
    ed Bootloader, themost powerful one. http://www.gnu.org/software/grub/ I Can read many
  • 171.
    lesystem formats toload the kernel image and the con
  • 172.
    guration, provides apowerful shell with various commands, can load kernel images over the network, etc. I See our dedicated presentation for details: http://free-electrons.com/docs/grub/ I Syslinux, for network and removable media booting (USB key, CD-ROM) http://www.kernel.org/pub/linux/utils/boot/syslinux/ Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 96/526
  • 173.
    Booting on embeddedCPUs: case 1 I When powered, the CPU starts executing code at a
  • 174.
    xed address IThere is no other booting mechanism provided by the CPU I The hardware design must ensure that a NOR ash chip is wired so that it is accessible at the address at which the CPU starts executing instructions I The
  • 175.
    rst stage bootloadermust be programmed at this address in the NOR I NOR is mandatory, because it allows random access, which NAND doesn't allow I Not very common anymore (unpractical, and requires NOR ash) Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 97/526
  • 176.
    Booting on embeddedCPUs: case 2 I The CPU has an integrated boot code in ROM I BootROM on AT91 CPUs, ROM code on OMAP, etc. I Exact details are CPU-dependent I This boot code is able to load a
  • 177.
    rst stage bootloaderfrom a storage device into an internal SRAM (DRAM not initialized yet) I Storage device can typically be: MMC, NAND, SPI ash, UART, etc. I The
  • 178.
    rst stage bootloaderis I Limited in size due to hardware constraints (SRAM size) I Provided either by the CPU vendor or through community projects I This
  • 179.
    rst stage bootloadermust initialize DRAM and other hardware devices and load a second stage bootloader into RAM Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 98/526
  • 180.
    Booting on ARMAtmel AT91 I RomBoot: tries to
  • 181.
    nd a validbootstrap image from various storage sources, and load it into SRAM (DRAM not initialized yet). Size limited to 4 KB. No user interaction possible in standard boot mode. I AT91Bootstrap: runs from SRAM. Initializes the DRAM, the NAND or SPI controller, and loads the secondary bootloader into RAM and starts it. No user interaction possible. I U-Boot: runs from RAM. Initializes some other hardware devices (network, USB, etc.). Loads the kernel image from storage or network to RAM and starts it. Shell with commands provided. I Linux Kernel: runs from RAM. Takes over the system completely (bootloaders no longer exists). Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 99/526
  • 182.
    Booting on ARMTI OMAP3 I ROM Code: tries to
  • 183.
    nd a validbootstrap image from various storage sources, and load it into SRAM or RAM (RAM can be initialized by ROM code through a con
  • 184.
    guration header). Size limited to 64 KB. No user interaction possible. I X-Loader or U-Boot: runs from SRAM. Initializes the DRAM, the NAND or MMC controller, and loads the secondary bootloader into RAM and starts it. No user interaction possible. File called MLO. I U-Boot: runs from RAM. Initializes some other hardware devices (network, USB, etc.). Loads the kernel image from storage or network to RAM and starts it. Shell with commands provided. File called u-boot.bin or u-boot.img. I Linux Kernel: runs from RAM. Takes over the system completely (bootloaders no longer exists). Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 100/526
  • 185.
    Booting on MarvellSoC I ROM Code: tries to
  • 186.
    nd a validbootstrap image from various storage sources, and load it into RAM. The RAM con
  • 187.
    guration is describedin a CPU-speci
  • 188.
    c header, prependedto the bootloader image. I U-Boot: runs from RAM. Initializes some other hardware devices (network, USB, etc.). Loads the kernel image from storage or network to RAM and starts it. Shell with commands provided. File called u-boot.kwb. I Linux Kernel: runs from RAM. Takes over the system completely (bootloaders no longer exists). Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 101/526
  • 189.
    Generic bootloaders forembedded CPUs I We will focus on the generic part, the main bootloader, oering the most important features. I There are several open-source generic bootloaders. Here are the most popular ones: I U-Boot, the universal bootloader by Denx The most used on ARM, also used on PPC, MIPS, x86, m68k, NIOS, etc. The de-facto standard nowadays. We will study it in detail. http://www.denx.de/wiki/U-Boot I Barebox, a new architecture-neutral bootloader, written as a successor of U-Boot. Better design, better code, active development, but doesn't yet have as much hardware support as U-Boot. http://www.barebox.org I There are also a lot of other open-source or proprietary bootloaders, often architecture-speci
  • 190.
    c I RedBoot,Yaboot, PMON, etc. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 102/526
  • 191.
    Bootloaders The U-bootbootloader Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 103/526
  • 192.
    U-Boot U-Boot isa typical free software project I License: GPLv2 (same as Linux) I Freely available at http://www.denx.de/wiki/U-Boot I Documentation available at http://www.denx.de/wiki/U-Boot/Documentation I The latest development source code is available in a Git repository: http://git.denx.de/?p=u-boot.git;a=summary I Development and discussions happen around an open mailing-list http://lists.denx.de/pipermail/u-boot/ I Since the end of 2008, it follows a
  • 193.
    xed-interval release schedule.Every two months, a new version is released. Versions are named YYYY.MM. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 104/526
  • 194.
  • 195.
    guration I Getthe source code from the website, and uncompress it I The include/configs/ directory contains one con
  • 196.
  • 197.
    le for eachsupported board I It de
  • 198.
    nes the CPUtype, the peripherals and their con
  • 199.
    guration, the memorymapping, the U-Boot features that should be compiled in, etc. I It is a simple .h
  • 200.
    le that setsC pre-processor constants. See the README
  • 201.
    le for thedocumentation of these constants. This
  • 202.
    le can alsobe adjusted to add or remove features from U-Boot (commands, etc.). I Assuming that your board is already supported by U-Boot, there should be one entry corresponding to your board in the boards.cfg
  • 203.
    le. Free Electrons- Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 105/526
  • 204.
  • 205.
  • 206.
    le excerpt /*CPU configuration */ #define CONFIG_ARMV7 1 #define CONFIG_OMAP 1 #define CONFIG_OMAP34XX 1 #define CONFIG_OMAP3430 1 #define CONFIG_OMAP3_IGEP0020 1 [...] /* Memory configuration */ #define CONFIG_NR_DRAM_BANKS 2 #define PHYS_SDRAM_1 OMAP34XX_SDRC_CS0 #define PHYS_SDRAM_1_SIZE (32 20) #define PHYS_SDRAM_2 OMAP34XX_SDRC_CS1 [...] /* USB configuration */ #define CONFIG_MUSB_UDC 1 #define CONFIG_USB_OMAP3 1 #define CONFIG_TWL4030_USB 1 [...] /* Available commands and features */ #define CONFIG_CMD_CACHE #define CONFIG_CMD_EXT2 #define CONFIG_CMD_FAT #define CONFIG_CMD_I2C #define CONFIG_CMD_MMC #define CONFIG_CMD_NAND #define CONFIG_CMD_NET #define CONFIG_CMD_DHCP #define CONFIG_CMD_PING #define CONFIG_CMD_NFS #define CONFIG_CMD_MTDPARTS [...] Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 106/526
  • 207.
  • 208.
    guring and compilingU-Boot I U-Boot must be con
  • 209.
    gured before beingcompiled I make BOARDNAME_config I Where BOARDNAME is the name of the board, as visible in the boards.cfg
  • 210.
  • 211.
    rst column). IMake sure that the cross-compiler is available in PATH I Compile U-Boot, by specifying the cross-compiler pre
  • 212.
    x. Example, ifyour cross-compiler executable is arm-linux-gcc: make CROSS_COMPILE=arm-linux- I The main result is a u-boot.bin
  • 213.
    le, which isthe U-Boot image. Depending on your speci
  • 214.
    c platform, theremay be other specialized images: u-boot.img, u-boot.kwb, MLO, etc. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 107/526
  • 215.
    Installing U-Boot IU-Boot must usually be installed in ash memory to be executed by the hardware. Depending on the hardware, the installation of U-Boot is done in a dierent way: I The CPU provides some kind of speci
  • 216.
    c boot monitorwith which you can communicate through serial port or USB using a speci
  • 217.
    c protocol IThe CPU boots
  • 218.
    rst on removablemedia (MMC) before booting from
  • 219.
    xed media (NAND).In this case, boot from MMC to re ash a new version I U-Boot is already installed, and can be used to ash a new version of U-Boot. However, be careful: if the new version of U-Boot doesn't work, the board is unusable I The board provides a JTAG interface, which allows to write to the ash memory remotely, without any system running on the board. It also allows to rescue a board if the bootloader doesn't work. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 108/526
  • 220.
    U-boot prompt IConnect the target to the host through a serial console I Power-up the board. On the serial console, you will see something like: U-Boot 2013.04 (May 29 2013 - 10:30:21) OMAP36XX/37XX-GP ES1.2, CPU-OPP2, L3-165MHz, Max CPU Clock 1 Ghz IGEPv2 + LPDDR/NAND I2C: ready DRAM: 512 MiB NAND: 512 MiB MMC: OMAP SD/MMC: 0 Die ID #255000029ff800000168580212029011 Net: smc911x-0 U-Boot # I The U-Boot shell oers a set of commands. We will study the most important ones, see the documentation for a complete reference or the help command. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 109/526
  • 221.
    Information commands Flashinformation (NOR and SPI ash) U-Boot flinfo DataFlash:AT45DB021 Nb pages: 1024 Page Size: 264 Size= 270336 bytes Logical address: 0xC0000000 Area 0: C0000000 to C0001FFF (RO) Bootstrap Area 1: C0002000 to C0003FFF Environment Area 2: C0004000 to C0041FFF (RO) U-Boot NAND ash information U-Boot nand info Device 0: nand0, sector size 128 KiB Page size 2048 b OOB size 64 b Erase size 131072 b Version details U-Boot version U-Boot 2013.04 (May 29 2013 - 10:30:21) Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 110/526
  • 222.
    Important commands (1) I The exact set of commands depends on the U-Boot con
  • 223.
    guration I helpand help command I boot, runs the default boot command, stored in bootcmd I bootm address, starts a kernel image loaded at the given address in RAM I ext2load, loads a
  • 224.
  • 225.
    lesystem to RAM I And also ext2ls to list
  • 226.
    les, ext2info forinformation I fatload, loads a
  • 227.
  • 228.
    lesystem to RAM I And also fatls and fatinfo I tftp, loads a
  • 229.
    le from thenetwork to RAM I ping, to test the network Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 111/526
  • 230.
    Important commands (2) I loadb, loads, loady, load a
  • 231.
    le from theserial line to RAM I usb, to initialize and control the USB subsystem, mainly used for USB storage devices such as USB keys I mmc, to initialize and control the MMC subsystem, used for SD and microSD cards I nand, to erase, read and write contents to NAND ash I erase, protect, cp, to erase, modify protection and write to NOR ash I md, displays memory contents. Can be useful to check the contents loaded in memory, or to look at hardware registers. I mm, modi
  • 232.
    es memory contents.Can be useful to modify directly hardware registers, for testing purposes. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 112/526
  • 233.
    Environment variables commands(1) I U-Boot can be con
  • 234.
    gured through environmentvariables, which aect the behavior of the dierent commands. I Environment variables are loaded from ash to RAM at U-Boot startup, can be modi
  • 235.
    ed and savedback to ash for persistence I There is a dedicated location in ash (or in MMC storage) to store the U-Boot environment, de
  • 236.
    ned in theboard con
  • 237.
  • 238.
    le Free Electrons- Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 113/526
  • 239.
    Environment variables commands(2) Commands to manipulate environment variables: I printenv Shows all variables I printenv variable-name Shows the value of a variable I setenv variable-name variable-value Changes the value of a variable, only in RAM I editenv variable-name Edits the value of a variable, only in RAM I saveenv Saves the current state of the environment to ash Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 114/526
  • 240.
    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 Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 115/526
  • 241.
    Important U-Boot envvariables I bootcmd, contains the command that U-Boot will automatically execute at boot time after a con
  • 242.
    gurable delay, ifthe process is not interrupted I bootargs, contains the arguments passed to the Linux kernel, covered later I serverip, the IP address of the server that U-Boot will contact for network related commands I ipaddr, the IP address that U-Boot will use I netmask, the network mask to contact the server I ethaddr, the MAC address, can only be set once I bootdelay, the delay in seconds before which U-Boot runs bootcmd I autostart, if yes, U-Boot starts automatically an image that has been loaded into memory Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 116/526
  • 243.
    Scripts in environmentvariables I Environment variables can contain small scripts, to execute several commands and test the results of commands. I Useful to automate booting or upgrade processes I Several commands can be chained using the ; operator I Tests can be done using if command ; then ... ; else ... ; fi I Scripts are executed using run variable-name I You can reference other variables using ${variable-name} I Example I setenv mmc-boot 'if fatload mmc 0 80000000 boot. ini; then source; else if fatload mmc 0 80000000 uImage; then run mmc-bootargs; bootm; fi; fi' Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 117/526
  • 244.
  • 245.
    les to thetarget I U-Boot is mostly used to load and boot a kernel image, but it also allows to change the kernel image and the root
  • 246.
    lesystem stored in ash. I Files must be exchanged between the target and the development workstation. This is possible: I 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 ecient solution. I Through a USB key, if U-Boot supports the USB controller of your platform I Through a SD or microSD card, if U-Boot supports the MMC controller of your platform I Through the serial port Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 118/526
  • 247.
    TFTP I Networktransfer from the development workstation and U-Boot on the target takes place through TFTP I Trivial File Transfer Protocol I Somewhat similar to FTP, but without authentication and over UDP I A TFTP server is needed on the development workstation I sudo apt-get install tftpd-hpa I All
  • 248.
    les in /var/lib/tftpbootare then visible through TFTP I A TFTP client is available in the tftp-hpa package, for testing I A TFTP client is integrated into U-Boot I Con
  • 249.
    gure the ipaddrand serverip environment variables I Use tftp address filename to load a
  • 250.
    le Free Electrons- Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 119/526
  • 251.
    U-boot mkimage IThe kernel image that U-Boot loads and boots must be prepared, so that a U-Boot speci
  • 252.
    c header isadded in front of the image I This header gives details such as the image size, the expected load address, the compression type, etc. I This is done with a tool that comes in U-Boot, mkimage I Debian / Ubuntu: just install the u-boot-tools package. I Or, compile it by yourself: simply con
  • 253.
    gure U-Boot forany board of any architecture and compile it. Then install mkimage: cp tools/mkimage /usr/local/bin/ I The special target uImage of the kernel Make
  • 254.
    le can thenbe used to generate a kernel image suitable for U-Boot. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 120/526
  • 255.
    Practical lab -U-Boot Time to start the practical lab! I Communicate with the board using a serial console I Con
  • 256.
    gure, build andinstall U-Boot I Learn U-Boot commands I Set up TFTP communication with the board Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 121/526
  • 257.
    Linux kernel introduction Linux kernel introduction Free Electrons ©Copyright 2004-2014, Free Electrons. Creative Commons BY-SA 3.0 license. Corrections, suggestions, contributions and translations are welcome! Embedded Linux Developers Free Electrons Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 122/526
  • 258.
    Linux kernel introduction Linux features Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 123/526
  • 259.
    History I TheLinux kernel is one component of a system, which also requires libraries and applications to provide features to end users. I The Linux kernel was created as a hobby in 1991 by a Finnish student, Linus Torvalds. I Linux quickly started to be used as the kernel for free software operating systems I Linus Torvalds has been able to create a large and dynamic developer and user community around Linux. I Nowadays, more than one thousand people contribute to each kernel release, individuals or companies big and small. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 124/526
  • 260.
    Linux kernel keyfeatures I Portability and hardware support. Runs on most architectures. I Scalability. Can run on super computers as well as on tiny devices (4 MB of RAM is enough). I Compliance to standards and interoperability. I Exhaustive networking support. I Security. It can't hide its aws. Its code is reviewed by many experts. I Stability and reliability. I Modularity. Can include only what a system needs even at run time. I Easy to program. You can learn from existing code. Many useful resources on the net. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 125/526
  • 261.
    Linux kernel inthe system Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 126/526
  • 262.
    Linux kernel mainroles I Manage all the hardware resources: CPU, memory, I/O. I Provide a set of portable, architecture and hardware independent APIs to allow user space applications and libraries to use the hardware resources. I Handle concurrent accesses and usage of hardware resources from dierent applications. I Example: a single network interface is used by multiple user space applications through various network connections. The kernel is responsible to multiplex the hardware resource. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 127/526
  • 263.
    System calls IThe main interface between the kernel and user space is the set of system calls I About 300 system calls that provide the main kernel services I File and device operations, networking operations, inter-process communication, process management, memory mapping, timers, threads, synchronization primitives, etc. I This interface is stable over time: only new system calls can be added by the kernel developers I This system call interface is wrapped by the C library, and user space applications usually never make a system call directly but rather use the corresponding C library function Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 128/526
  • 264.
  • 265.
    lesystems I Linuxmakes system and kernel information available in user space through pseudo
  • 266.
  • 267.
  • 268.
    lesystems allow applicationsto see directories and
  • 269.
    les that donot exist on any real storage: they are created and updated on the y by the kernel I The two most important pseudo
  • 270.
    lesystems are Iproc, usually mounted on /proc: Operating system related information (processes, memory management parameters...) I sysfs, usually mounted on /sys: Representation of the system as a set of devices and buses. Information about these devices. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 129/526
  • 271.
    Inside the Linuxkernel Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 130/526
  • 272.
    Linux license IThe whole Linux sources are Free Software released under the GNU General Public License version 2 (GPL v2). I For the Linux kernel, this basically implies that: I 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. I When you produce Linux based devices, you must release the sources to the recipient, with the same rights, with no restriction. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 131/526
  • 273.
    Supported hardware architectures I See the arch/ directory in the kernel sources I Minimum: 32 bit processors, with or without MMU, and gcc support I 32 bit architectures (arch/ subdirectories) Examples: arm, avr32, blackfin, c6x, m68k, microblaze, mips, score, sparc, um I 64 bit architectures: Examples: alpha, arm64, ia64, tile I 32/64 bit architectures Examples: powerpc, x86, sh, sparc I Find details in kernel sources: arch/arch/Kconfig, arch/arch/README, or Documentation/arch/ Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 132/526
  • 274.
    Linux kernel introduction Linux versioning scheme and development process Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 133/526
  • 275.
    Until 2.6 (1) I One stable major branch every 2 or 3 years I Identi
  • 276.
    ed by aneven middle number I Examples: 1.0.x, 2.0.x, 2.2.x, 2.4.x I One development branch to integrate new functionalities and major changes I Identi
  • 277.
    ed by anodd middle number I Examples: 2.1.x, 2.3.x, 2.5.x I After some time, a development version becomes the new base version for the stable branch I Minor releases once in while: 2.2.23, 2.5.12, etc. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 134/526
  • 278.
    Until 2.6 (2) Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 135/526
  • 279.
    Changes since Linux2.6 I Since 2.6.0, kernel developers have been able to introduce lots of new features one by one on a steady pace, without having to make disruptive changes to existing subsystems. I Since then, there has been no need to create a new development branch massively breaking compatibility with the stable branch. I Thanks to this, more features are released to users at a faster pace. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 136/526
  • 280.
    3.x stable branch I From 2003 to 2011, the ocial kernel versions were named 2.6.x. I Linux 3.0 was released in July 2011 I This is only a change to the numbering scheme I Ocial kernel versions are now named 3.x (3.0, 3.1, 3.2, etc.) I Stabilized versions are named 3.x.y (3.0.2, 3.4.3, etc.) I It eectively only removes a digit compared to the previous numbering scheme Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 137/526
  • 281.
    New development model Using merge and bug
  • 282.
    xing windows FreeElectrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 138/526
  • 283.
    New development model- Details I After the release of a 3.x version (for example), a two-weeks merge window opens, during which major additions are merged. I The merge window is closed by the release of test version 3.(x+1)-rc1 I The bug
  • 284.
    xing period opens,for 6 to 10 weeks. I At regular intervals during the bug
  • 285.
    xing period, 3.(x+1)-rcYtest versions are released. I When considered suciently stable, kernel 3.(x+1) is released, and the process starts again. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 139/526
  • 286.
    More stability forthe kernel source tree I Issue: bug and security
  • 287.
    xes only released for most recent stable kernel versions. I Some people need to have a recent kernel, but with long term support for security updates. I You could get long term support from a commercial embedded Linux provider. I You could reuse sources for the kernel used in Ubuntu Long Term Support releases (5 years of free security updates). I The http://kernel.org front page shows which versions will be supported for some time (up to 2 or 3 years), and which ones won't be supported any more (EOL: End Of Life) Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 140/526
  • 288.
    What's new ineach Linux release? I The ocial list of changes for each Linux release is just a huge list of individual patches! commit aa6e52a35d388e730f4df0ec2ec48294590cc459 Author: Thomas Petazzoni thomas.petazzoni@free-electrons.com Date: Wed Jul 13 11:29:17 2011 +0200 at91: at91-ohci: support overcurrent notification Several USB power switches (AIC1526 or MIC2026) have a digital output that is used to notify that an overcurrent situation is taking place. This digital outputs are typically connected to GPIO inputs of the processor and can be used to be notified of these overcurrent situations. Therefore, we add a new overcurrent_pin[] array in the at91_usbh_data structure so that boards can tell the AT91 OHCI driver which pins are used for the overcurrent notification, and an overcurrent_supported boolean to tell the driver whether overcurrent is supported or not. The code has been largely borrowed from ohci-da8xx.c and ohci-s3c2410.c. Signed-off-by: Thomas Petazzoni thomas.petazzoni@free-electrons.com Signed-off-by: Nicolas Ferre nicolas.ferre@atmel.com I Very dicult to
  • 289.
    nd out thekey changes and to get the global picture out of individual changes. I Fortunately, there are some useful resources available I http://wiki.kernelnewbies.org/LinuxChanges I http://lwn.net I http://linuxfr.org, for French readers Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 141/526
  • 290.
    Linux kernel introduction Linux kernel sources Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 142/526
  • 291.
    Location of kernelsources I The ocial versions of the Linux kernel, as released by Linus Torvalds, are available at http://www.kernel.org I These versions follow the development model of the kernel I However, they may not contain the latest development from a speci
  • 292.
    c area yet.Some features in development might not be ready for mainline inclusion yet. I Many chip vendors supply their own kernel sources I Focusing on hardware support
  • 293.
    rst I Canhave a very important delta with mainline Linux I Useful only when mainline hasn't caught up yet. I Many kernel sub-communities maintain their own kernel, with usually newer but less stable features I Architecture communities (ARM, MIPS, PowerPC, etc.), device drivers communities (I2C, SPI, USB, PCI, network, etc.), other communities (real-time, etc.) I No ocial releases, only development trees are available. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 143/526
  • 294.
    Getting Linux sources I The kernel sources are available from http://kernel.org/pub/linux/kernel as full tarballs (complete kernel sources) and patches (dierences between two kernel versions). I However, more and more people use the git version control system. Absolutely needed for kernel development! I Fetch the entire kernel sources and history git clone git://git.kernel.org/pub/scm/linux/ kernel/git/torvalds/linux.git I Create a branch that starts at a speci
  • 295.
    c stable version git checkout -b name-of-branch v3.11 I Web interface available at http://git.kernel.org/cgit/ linux/kernel/git/torvalds/linux.git/tree/. I Read more about Git at http://git-scm.com/ Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 144/526
  • 296.
    Linux kernel size(1) I Linux 3.10 sources: Raw size: 573 MB (43,000
  • 297.
    les, approx 15,800,000lines) gzip compressed tar archive: 105 MB bzip2 compressed tar archive: 83 MB (better) xz compressed tar archive: 69 MB (best) I Minimum Linux 3.17 compiled kernel size, booting on the ARM Versatile board (hard drive on PCI, ext2
  • 298.
    lesystem, ELF executablesupport, framebuer console and input devices): 876 KB (compressed), 2.3 MB (raw) I Why are these sources so big? Because they include thousands of device drivers, many network protocols, support many architectures and
  • 299.
    lesystems... I TheLinux core (scheduler, memory management...) is pretty small! Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 145/526
  • 300.
    Linux kernel size(2) As of kernel version 3.10. I drivers/: 49.4% I arch/: 21.9% I fs/: 6.0% I include/: 4.7% I sound/: 4.4% I Documentation/: 4.0% I net/: 3.9% I firmware/: 1.0% I kernel/: 1.0% I tools/: 0.9% I scripts/: 0.5% I mm/: 0.5% I crypto/: 0.4% I security/: 0.4% I lib/: 0.4% I block/: 0.2% I ... Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 146/526
  • 301.
    Getting Linux sources I Full tarballs I Contain the complete kernel sources: long to download and uncompress, but must be done at least once I Example: http://www.kernel.org/pub/linux/kernel/v3.x/linux- 3.10.9.tar.xz I Extract command: tar xf linux-3.10.9.tar.xz I Incremental patches between versions I It assumes you already have a base version and you apply the correct patches in the right order. Quick to download and apply I Examples: http://www.kernel.org/pub/linux/kernel/v3.x/patch-3.10.xz (3.9 to 3.10) http://www.kernel.org/pub/linux/kernel/v3.x/patch-3.10.9.xz (3.10 to 3.10.9) I All previous kernel versions are available in http://kernel.org/pub/linux/kernel/ Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 147/526
  • 302.
    Patch I Apatch is the dierence between two source trees I Computed with the diff tool, or with more elaborate version control systems I They are very common in the open-source community I 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* Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 148/526
  • 303.
    Contents of apatch I One section per modi
  • 304.
  • 305.
    le, starting witha 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 I One sub-section per modi
  • 306.
  • 307.
    le, starting with header with the aected line numbers @@ -1,7 +1,7 @@ I Three lines of context before the change VERSION = 2 PATCHLEVEL = 6 SUBLEVEL = 11 I The change itself -EXTRAVERSION = +EXTRAVERSION = .1 I Three lines of context after the change NAME=Woozy Numbat # *DOCUMENTATION* Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 149/526
  • 308.
    Using the patchcommand The patch command: I Takes the patch contents on its standard input I Applies the modi
  • 309.
    cations described bythe patch into the current directory patch usage examples: I patch -pn diff_file I cat diff_file | patch -pn I xzcat diff_file.xz | patch -pn I bzcat diff_file.bz2 | patch -pn I zcat diff_file.gz | patch -pn I Notes: I n: number of directory levels to skip in the
  • 310.
    le paths IYou can reverse apply a patch with the -R option I You can test a patch with --dry-run option Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 150/526
  • 311.
    Applying a Linuxpatch I Two types of Linux patches: I Either to be applied to the previous stable version (from 3.x-1 to 3.x) I Or implementing
  • 312.
    xes to thecurrent stable version (from 3.x to 3.x.y) I Can be downloaded in gzip, bzip2 or xz (much smaller) compressed
  • 313.
    les. I Alwaysproduced for n=1 (that's what everybody does... do it too!) I Need to run the patch command inside the kernel source directory I Linux patch command line example: cd linux-3.9 xzcat ../patch-3.10.xz | patch -p1 xzcat ../patch-3.10.9.xz | patch -p1 cd ..; mv linux-3.9 linux-3.10.9 Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 151/526
  • 314.
    Practical lab -Kernel sources Time to start the practical lab! I Get the Linux kernel sources I Apply patches Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 152/526
  • 315.
  • 316.
    guration Free Electrons- Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 153/526
  • 317.
  • 318.
    guration and buildsystem I The kernel con
  • 319.
    guration and buildsystem is based on multiple Make
  • 320.
    les I Oneonly interacts with the main Makefile, present at the top directory of the kernel source tree I Interaction takes place I using the make tool, which parses the Make
  • 321.
    le I throughvarious targets, de
  • 322.
    ning which actionshould be done (con
  • 323.
    guration, compilation, installation,etc.). Run make help to see all available targets. I Example I cd linux-3.6.x/ I make target Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 154/526
  • 324.
  • 325.
    guration (1) IThe kernel contains thousands of device drivers,
  • 326.
    lesystem drivers, networkprotocols and other con
  • 327.
    gurable items IThousands of options are available, that are used to selectively compile parts of the kernel source code I The kernel con
  • 328.
    guration is theprocess of de
  • 329.
    ning the setof options with which you want your kernel to be compiled I The set of options depends I On your hardware (for device drivers, etc.) I On the capabilities you would like to give to your kernel (network capabilities,
  • 330.
    lesystems, real-time, etc.) Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 155/526
  • 331.
  • 332.
  • 333.
    guration is storedin the .config
  • 334.
    le at theroot of kernel sources I Simple text
  • 335.
    le, key=value style I As options have dependencies, typically never edited by hand, but through graphical or text interfaces: I make xconfig, make gconfig (graphical) I make menuconfig, make nconfig (text) I You can switch from one to another, they all load/save the same .config
  • 336.
    le, and showthe same set of options I To modify a kernel in a GNU/Linux distribution: the con
  • 337.
  • 338.
    les are usuallyreleased in /boot/, together with kernel images: /boot/config-3.2.0-31-generic Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 156/526
  • 339.
    Kernel or module? I The kernel image is a single
  • 340.
    le, resulting fromthe linking of all object
  • 341.
    les that correspondto features enabled in the con
  • 342.
  • 343.
    le that getsloaded in memory by the bootloader I All included features are therefore available as soon as the kernel starts, at a time where no
  • 344.
    lesystem exists ISome features (device drivers,
  • 345.
    lesystems, etc.) canhowever be compiled as modules I These are plugins that can be loaded/unloaded dynamically to add/remove features to the kernel I Each module is stored as a separate
  • 346.
  • 347.
  • 348.
    lesystem is mandatoryto use modules I This is not possible in the early boot procedure of the kernel, because no
  • 349.
    lesystem is available Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 157/526
  • 350.
    Kernel option types I There are dierent types of options I bool options, they are either I true (to include the feature in the kernel) or I false (to exclude the feature from the kernel) I tristate options, they are either I true (to include the feature in the kernel image) or I module (to include the feature as a kernel module) or I false (to exclude the feature) I int options, to specify integer values I hex options, to specify hexadecimal values I string options, to specify string values Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 158/526
  • 351.
    Kernel option dependencies I There are dependencies between kernel options I For example, enabling a network driver requires the network stack to be enabled I Two types of dependencies I depends on dependencies. In this case, option A that depends on option B is not visible until option B is enabled I select dependencies. In this case, with option A depending on option B, when option A is enabled, option B is automatically enabled I make xconfig allows to see all options, even the ones that cannot be selected because of missing dependencies. In this case, they are displayed in gray Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 159/526
  • 352.
  • 353.
    g make xconfig I The most common graphical interface to con
  • 354.
    gure the kernel. I Make sure you read help - introduction: useful options! I File browser: easier to load con
  • 355.
  • 356.
    les I Searchinterface to look for parameters I Required Debian / Ubuntu packages: libqt4-dev g++ (libqt3-mt-dev for older kernel releases) Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 160/526
  • 357.
  • 358.
    g screenshot FreeElectrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 161/526
  • 359.
  • 360.
    g search interface Looks for a keyword in the parameter name. Allows to select or unselect found parameters. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 162/526
  • 361.
  • 362.
    guration options FreeElectrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 163/526
  • 363.
  • 364.
  • 365.
    le excerpt Optionsare grouped by sections and are pre
  • 366.
    xed 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 Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 164/526
  • 367.
  • 368.
    g make gconfig I GTK based graphical con
  • 369.
    guration interface. Functionalitysimilar to that of make xconfig. I Just lacking a search functionality. I Required Debian packages: libglade2-dev Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 165/526
  • 370.
  • 371.
    g make menuconfig I Useful when no graphics are available. Pretty convenient too! I Same interface found in other tools: BusyBox, Buildroot... I Required Debian packages: libncurses-dev Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 166/526
  • 372.
  • 373.
    g make nconfig I A newer, similar text interface I More user friendly (for example, easier to access help information). I Required Debian packages: libncurses-dev Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 167/526
  • 374.
  • 375.
    g make oldconfig I Needed very often! I Useful to upgrade a .config
  • 376.
    le from anearlier kernel release I Issues warnings for con
  • 377.
    guration parameters thatno longer exist in the new kernel. I Asks for values for new parameters If you edit a .config
  • 378.
    le by hand,it's strongly recommended to run make oldconfig afterwards! Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 168/526
  • 379.
  • 380.
    guration changes Afrequent problem: I After changing several kernel con
  • 381.
    guration settings, your kernel no longer works. I If you don't remember all the changes you made, you can get back to your previous con
  • 382.
    guration: $ cp.config.old .config I All the con
  • 383.
    guration interfaces ofthe kernel (xconfig, menuconfig, oldconfig...) keep this .config.old backup copy. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 169/526
  • 384.
  • 385.
    guration per architecture I The set of con
  • 386.
    guration options isarchitecture dependent I Some con
  • 387.
    guration options arevery architecture-speci
  • 388.
    c I Mostof the con
  • 389.
    guration options (globalkernel options, network subsystem,
  • 390.
    lesystems, most ofthe device drivers) are visible in all architectures. I By default, the kernel build system assumes that the kernel is being built for the host architecture, i.e. native compilation I The architecture is not de
  • 391.
  • 392.
    guration, but at a higher level I We will see later how to override this behaviour, to allow the con
  • 393.
    guration of kernelsfor a dierent architecture Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 170/526
  • 394.
    Linux kernel introduction Compiling and installing the kernel for the host system Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 171/526
  • 395.
    Kernel compilation Imake I in the main kernel source directory I Remember to run multiple jobs in parallel if you have multiple CPU cores. Example: make -j 4 I No need to run as root! I Generates I vmlinux, the raw uncompressed kernel image, at the ELF format, useful for debugging purposes, but cannot be booted I arch/arch/boot/*Image, the
  • 396.
    nal, usually compressed, kernel image that can be booted I bzImage for x86, zImage for ARM, vmImage.gz for Black
  • 397.
    n, etc. Iarch/arch/boot/dts/*.dtb, compiled Device Tree
  • 398.
    les (on somearchitectures) I All kernel modules, spread over the kernel source tree, as .ko
  • 399.
    les. Free Electrons- Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 172/526
  • 400.
    Kernel installation Imake install I Does the installation for the host system by default, so needs to be run as root. Generally not used when compiling for an embedded system, and it installs
  • 401.
    les on thedevelopment workstation. I Installs I /boot/vmlinuz-version Compressed kernel image. Same as the one in arch/arch/boot I /boot/System.map-version Stores kernel symbol addresses I /boot/config-version Kernel con
  • 402.
    guration for thisversion I Typically re-runs the bootloader con
  • 403.
    guration utility totake the new kernel into account. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 173/526
  • 404.
    Module installation Imake modules_install I Does the installation for the host system by default, so needs to be run as root I Installs all modules in /lib/modules/version/ I kernel/ Module .ko (Kernel Object)
  • 405.
    les, in thesame directory structure as in the sources. I modules.alias Module aliases for module loading utilities. Example line: alias sound-service-?-0 snd_mixer_oss I modules.dep Module dependencies I modules.symbols Tells which module a given symbol belongs to. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 174/526
  • 406.
    Kernel cleanup targets I Clean-up generated
  • 407.
    les (to force re-compilation): make clean I Remove all generated
  • 408.
    les. Needed when switching from one architecture to another. Caution: it also removes your .config
  • 409.
    le! make mrproper I Also remove editor backup and patch reject
  • 410.
    les (mainly togenerate patches): make distclean Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 175/526
  • 411.
    Linux kernel introduction Cross-compiling the kernel Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 176/526
  • 412.
    Cross-compiling the kernel When you compile a Linux kernel for another CPU architecture I Much faster than compiling natively, when the target system is much slower than your GNU/Linux workstation. I Much easier as development tools for your GNU/Linux workstation are much easier to
  • 413.
    nd. I Tomake the dierence with a native compiler, cross-compiler executables are pre
  • 414.
    xed by thename of the target system, architecture and sometimes library. Examples: mips-linux-gcc, the pre
  • 415.
    x is mips-linux-arm- linux-gnueabi-gcc, the pre
  • 416.
    x is arm-linux-gnueabi- Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 177/526
  • 417.
    Specifying cross-compilation (1) The CPU architecture and cross-compiler pre
  • 418.
  • 419.
    ned through theARCH and CROSS_COMPILE variables in the toplevel Make
  • 420.
    le. I ARCHis the name of the architecture. It is de
  • 421.
    ned by the name of the subdirectory in arch/ in the kernel sources I Example: arm if you want to compile a kernel for the arm architecture. I CROSS_COMPILE is the pre
  • 422.
    x of thecross compilation tools I Example: arm-linux- if your compiler is arm-linux-gcc Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 178/526
  • 423.
  • 424.
    ne ARCH andCROSS_COMPILE: I Pass ARCH and CROSS_COMPILE on the make command line: make ARCH=arm CROSS_COMPILE=arm-linux- ... Drawback: it is easy to forget to pass these variables when you run any make command, causing your build and con
  • 425.
    guration to bescrewed up. I De
  • 426.
    ne ARCH andCROSS_COMPILE as environment variables: export ARCH=arm export CROSS_COMPILE=arm-linux- Drawback: it only works inside the current shell or terminal. You could put these settings in a
  • 427.
    le that yousource every time you start working on the project. If you only work on a single architecture with always the same toolchain, you could even put these settings in your ~/.bashrc
  • 428.
    le to makethem permanent and visible from any terminal. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 179/526
  • 429.
  • 430.
  • 431.
  • 432.
  • 433.
  • 434.
    les available, perboard or per-CPU family I They are stored in arch/arch/configs/, and are just minimal .config
  • 435.
    les I Thisis the most common way of con
  • 436.
    guring a kernelfor embedded platforms I Run make help to
  • 437.
    nd if oneis available for your platform I To load a default con
  • 438.
  • 439.
    le, just run make acme_defconfig I This will overwrite your existing .config
  • 440.
    le! I Tocreate your own default con
  • 441.
  • 442.
    le I makesavedefconfig, to create a minimal con
  • 443.
  • 444.
    le I mvdefconfig arch/arch/configs/myown_defconfig Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 180/526
  • 445.
  • 446.
    guring the kernel I After loading a default con
  • 447.
  • 448.
    le, you canadjust the con
  • 449.
    guration to yourneeds with the normal xconfig, gconfig or menuconfig interfaces I You can also start the con
  • 450.
    guration from scratchwithout loading a default con
  • 451.
  • 452.
    le I Asthe architecture is dierent from your host architecture I Some options will be dierent from the native con
  • 453.
    guration (processor andarchitecture speci
  • 454.
  • 455.
    c drivers, etc.) I Many options will be identical (
  • 456.
    lesystems, network protocols, architecture-independent drivers, etc.) I Make sure you have the support for the right CPU, the right board and the right device drivers. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 181/526
  • 457.
    Device Tree IMany embedded architectures have a lot of non-discoverable hardware. I Depending on the architecture, such hardware is either described using C code directly within the kernel, or using a special hardware description language in a Device Tree. I ARM, PowerPC, OpenRISC, ARC, Microblaze are examples of architectures using the Device Tree. I A Device Tree Source, written by kernel developers, is compiled into a binary Device Tree Blob, passed at boot time to the kernel. I There is one dierent Device Tree for each board/platform supported by the kernel, available in arch/arm/boot/dts/board.dtb. I The bootloader must load both the kernel image and the Device Tree Blob in memory before starting the kernel. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 182/526
  • 458.
    Building and installingthe kernel I Run make I Copy the
  • 459.
    nal kernel imageto the target storage I can be uImage, zImage, vmlinux, bzImage in arch/arch/boot I copying the Device Tree Blob might be necessary as well, they are available in arch/arch/boot/dts I make install is rarely used in embedded development, as the kernel image is a single
  • 460.
    le, easy tohandle I It is however possible to customize the make install behaviour in arch/arch/boot/install.sh I make modules_install is used even in embedded development, as it installs many modules and description
  • 461.
    les I makeINSTALL_MOD_PATH=dir/ modules_install I The INSTALL_MOD_PATH variable is needed to install the modules in the target root
  • 462.
    lesystem instead ofyour host root
  • 463.
    lesystem. Free Electrons- Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 183/526
  • 464.
    Booting with U-Boot I Recent versions of U-Boot can boot the zImage binary. I Older versions require a special kernel image format: uImage I uImage is generated from zImage using the mkimage tool. It is done automatically by the kernel make uImage target. I On some ARM platforms, make uImage requires passing a LOADADDR environment variable, which indicates at which physical memory address the kernel will be executed. I In addition to the kernel image, U-Boot can also pass a Device Tree Blob to the kernel. I The typical boot process is therefore: 1. Load zImage or uImage at address X in memory 2. Load board.dtb at address Y in memory 3. Start the kernel with bootz X - Y or bootm X - Y The - in the middle indicates no initramfs Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 184/526
  • 465.
    Kernel command line I In addition to the compile time con
  • 466.
    guration, the kernel behaviour can be adjusted with no recompilation using the kernel command line I The kernel command line is a string that de
  • 467.
    nes various argumentsto the kernel I It is very important for system con
  • 468.
    guration I root=for the root
  • 469.
    lesystem (covered later) I console= for the destination of kernel messages I Many more exist. The most important ones are documented in Documentation/kernel-parameters.txt in kernel sources. I This kernel command line is either I Passed by the bootloader. In U-Boot, the contents of the bootargs environment variable is automatically passed to the kernel I Built into the kernel, using the CONFIG_CMDLINE option. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 185/526
  • 470.
    Practical lab -Kernel cross-compiling I Set up the cross-compiling environment I Con
  • 471.
    gure the kernelMakefile accordingly I Cross-compile the kernel for an arm platform I On this platform, interact with the bootloader and boot your kernel Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 186/526
  • 472.
    Linux kernel introduction Using kernel modules Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 187/526
  • 473.
    Advantages of modules I Modules make it easy to develop drivers without rebooting: load, test, unload, rebuild, load... I Useful to keep the kernel image size to the minimum (essential in GNU/Linux distributions for PCs). I Also useful to reduce boot time: you don't spend time initializing devices and kernel features that you only need later. I 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. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 188/526
  • 474.
    Module dependencies ISome kernel modules can depend on other modules, which need to be loaded
  • 475.
    rst. I Example:the usb-storage module depends on the scsi_mod, libusual and usbcore modules. I Dependencies are described in /lib/modules/kernel-version/modules.dep This
  • 476.
    le is generatedwhen you run make modules_install. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 189/526
  • 477.
    Kernel log Whena new module is loaded, related information is available in the kernel log. I The kernel keeps its messages in a circular buer (so that it doesn't consume more memory with many messages) I Kernel log messages are available through the dmesg command (diagnostic message) I Kernel log messages are also displayed in the system console (console messages can be
  • 478.
    ltered by levelusing the loglevel kernel parameter, or completely disabled with the quiet parameter). I Note that you can write to the kernel log from user space too: echo nDebug info /dev/kmsg Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 190/526
  • 479.
    Module utilities (1) I modinfo module_name modinfo module_path.ko Gets information about a module: parameters, license, description and dependencies. Very useful before deciding to load a module or not. I sudo insmod module_path.ko Tries to load the given module. The full path to the module object
  • 480.
    le must begiven. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 191/526
  • 481.
    Understanding module loadingissues I When loading a module fails, insmod often doesn't give you enough details! I Details are often available in the kernel log. I 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 Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 192/526
  • 482.
    Module utilities (2) I sudo modprobe module_name Most common usage of modprobe: tries to load all the modules the given module depends on, and then this module. Lots of other options are available. modprobe automatically looks in /lib/modules/version/ for the object
  • 483.
    le corresponding tothe given module name. I lsmod Displays the list of loaded modules Compare its output with the contents of /proc/modules! Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 193/526
  • 484.
    Module utilities (3) I sudo rmmod module_name Tries to remove the given module. Will only be allowed if the module is no longer in use (for example, no more processes opening a device
  • 485.
    le) I sudomodprobe -r module_name Tries to remove the given module and all dependent modules (which are no longer needed after removing the module) Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 194/526
  • 486.
    Passing parameters tomodules I Find available parameters: modinfo snd-intel8x0m I Through insmod: sudo insmod ./snd-intel8x0m.ko index=-2 I Through modprobe: Set parameters in /etc/modprobe.conf or in any
  • 487.
    le in /etc/modprobe.d/: options snd-intel8x0m index=-2 I Through the kernel command line, when the driver is built statically into the kernel: snd-intel8x0m.index=-2 I snd-intel8x0m is the driver name I index is the driver parameter name I -2 is the driver parameter value Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 195/526
  • 488.
  • 489.
    nd the currentvalues for the parameters of a loaded module? I Check /sys/module/name/parameters. I There is one
  • 490.
    le per parameter,containing the parameter value. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 196/526
  • 491.
    Useful reading LinuxKernel in a Nutshell, Dec 2006 I By Greg Kroah-Hartman, O'Reilly http://www.kroah.com/lkn/ I A good reference book and guide on con
  • 492.
    guring, compiling andmanaging the Linux kernel sources. I Freely available on-line! Great companion to the printed book for easy electronic searches! Available as single PDF
  • 493.
    le on http://free-electrons.com/ community/kernel/lkn/ I Our rating: 2 stars Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 197/526
  • 494.
    Linux Root Filesystem Linux Root Filesystem Free Electrons ©Copyright 2004-2014, Free Electrons. Creative Commons BY-SA 3.0 license. Corrections, suggestions, contributions and translations are welcome! Embedded Linux Developers Free Electrons Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 198/526
  • 495.
    Linux Root Filesystem Principle and solutions Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 199/526
  • 496.
    Filesystems I Filesystemsare used to organize data in directories and
  • 497.
    les on storagedevices or on the network. The directories and
  • 498.
    les are organizedas a hierarchy I In Unix systems, applications and users see a single global hierarchy of
  • 499.
    les and directories,which can be composed of several
  • 500.
    lesystems. I Filesystemsare mounted in a speci
  • 501.
    c location inthis hierarchy of directories I When a
  • 502.
    lesystem is mountedin a directory (called mount point), the contents of this directory re ects the contents of the storage device I When the
  • 503.
    lesystem is unmounted,the mount point is empty again. I This allows applications to access
  • 504.
    les and directorieseasily, regardless of their exact storage location Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 200/526
  • 505.
    Filesystems (2) ICreate a mount point, which is just a directory $ mkdir /mnt/usbkey I It is empty $ ls /mnt/usbkey $ I Mount a storage device in this mount point $ mount -t vfat /dev/sda1 /mnt/usbkey $ I You can access the contents of the USB key $ ls /mnt/usbkey docs prog.c picture.png movie.avi $ Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 201/526
  • 506.
    mount / umount I mount allows to mount
  • 507.
    lesystems I mount-t type device mountpoint I type is the type of
  • 508.
    lesystem I deviceis the storage device, or network location to mount I mountpoint is the directory where
  • 509.
    les of thestorage device or network location will be accessible I mount with no arguments shows the currently mounted
  • 510.
    lesystems I umountallows to unmount
  • 511.
    lesystems I Thisis needed before rebooting, or before unplugging a USB key, because the Linux kernel caches writes in memory to increase performance. umount makes sure that these writes are committed to the storage. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 202/526
  • 512.
  • 513.
    lesystem I Aparticular
  • 514.
    lesystem is mountedat the root of the hierarchy, identi
  • 515.
    ed by / I This
  • 516.
  • 517.
    lesystem I Asmount and umount are programs, they are
  • 518.
  • 519.
    lesystem. I Theyare not accessible before mounting at least one
  • 520.
  • 521.
  • 522.
  • 523.
    lesystem, it cannotbe mounted with the normal mount command I It is mounted directly by the kernel, according to the root= kernel option I When no root
  • 524.
    lesystem is available,the kernel panics Please append a correct root= boot option Kernel panic - not syncing: VFS: Unable to mount root fs on unknown block(0,0) Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 203/526
  • 525.
  • 526.
    lesystem I Itcan be mounted from dierent locations I From the partition of a hard disk I From the partition of a USB key I From the partition of an SD card I From the partition of a NAND ash chip or similar type of storage device I From the network, using the NFS protocol I From memory, using a pre-loaded
  • 527.
    lesystem (by the bootloader) I etc. I It is up to the system designer to choose the con
  • 528.
    guration for thesystem, and con
  • 529.
    gure the kernelbehaviour with root= Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 204/526
  • 530.
    Mounting rootfs fromstorage devices I Partitions of a hard disk or USB key I root=/dev/sdXY, where X is a letter indicating the device, and Y a number indicating the partition I /dev/sdb2 is the second partition of the second disk drive (either USB key or ATA hard drive) I Partitions of an SD card I root=/dev/mmcblkXpY, where X is a number indicating the device and Y a number indicating the partition I /dev/mmcblk0p2 is the second partition of the
  • 531.
    rst device IPartitions of ash storage I root=/dev/mtdblockX, where X is the partition number I /dev/mtdblock3 is the fourth partition of a NAND ash chip (if only one NAND ash chip is present) Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 205/526
  • 532.
    Mounting rootfs overthe network (1) Once networking works, your root
  • 533.
    lesystem could bea directory on your GNU/Linux development host, exported by NFS (Network File System). This is very convenient for system development: I Makes it very easy to update
  • 534.
  • 535.
    lesystem, without rebooting.Much faster than through the serial port. I Can have a big root
  • 536.
    lesystem even ifyou don't have support for internal or external storage yet. I The root
  • 537.
    lesystem can behuge. You can even build native compiler tools and build all the tools you need on the target itself (better to cross-compile though). Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 206/526
  • 538.
    Mounting rootfs overthe network (2) On the development workstation side, a NFS server is needed I Install an NFS server (example: Debian, Ubuntu) sudo apt-get install nfs-kernel-server I Add the exported directory to your /etc/exports
  • 539.
    le: /home/tux/rootfs 192.168.1.111(rw,no_root_squash, no_subtree_check) I 192.168.1.111 is the client IP address I rw,no_root_squash,no_subtree_check are the NFS server options for this directory export. I Start or restart your NFS server (example: Debian, Ubuntu) sudo /etc/init.d/nfs-kernel-server restart Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 207/526
  • 540.
    Mounting rootfs overthe network (3) I On the target system I The kernel must be compiled with I CONFIG_NFS_FS=y (NFS support) I CONFIG_IP_PNP=y (con
  • 541.
    gure IP atboot time) I CONFIG_ROOT_NFS=y (support for NFS as rootfs) I The kernel must be booted with the following parameters: I root=/dev/nfs (we want rootfs over NFS) I ip=192.168.1.111 (target IP address) I nfsroot=192.168.1.110:/home/tux/rootfs/ (NFS server details) Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 208/526
  • 542.
    Mounting rootfs overthe network (4) Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 209/526
  • 543.
    rootfs in memory:initramfs (1) I It is also possible to have the root
  • 544.
    lesystem integrated into the kernel image I It is therefore loaded into memory together with the kernel I This mechanism is called initramfs I It integrates a compressed archive of the
  • 545.
    lesystem into the kernel image I Variant: the compressed archive can also be loaded separately by the bootloader. I It is useful for two cases I Fast booting of very small root
  • 546.
  • 547.
    lesystem is completelyloaded at boot time, application startup is very fast. I As an intermediate step before switching to a real root
  • 548.
    lesystem, located ondevices for which drivers not part of the kernel image are needed (storage drivers,
  • 549.
    lesystem drivers, networkdrivers). This is always used on the kernel of desktop/server distributions to keep the kernel image size reasonable. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 210/526
  • 550.
    rootfs in memory:initramfs (2) Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 211/526
  • 551.
    rootfs in memory:initramfs (3) I The contents of an initramfs are de
  • 552.
    ned at thekernel con
  • 553.
    guration level, withthe CONFIG_INITRAMFS_SOURCE option I Can be the path to a directory containing the root
  • 554.
    lesystem contents ICan be the path to a cpio archive I Can be a text
  • 555.
    le describing thecontents of the initramfs (see documentation for details) I The kernel build process will automatically take the contents of the CONFIG_INITRAMFS_SOURCE option and integrate the root
  • 556.
    lesystem into thekernel image I Details (in kernel sources): Documentation/filesystems/ramfs-rootfs-initramfs.txt Documentation/early-userspace/README Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 212/526
  • 557.
    Linux Root Filesystem Contents Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 213/526
  • 558.
  • 559.
    lesystem organization IThe organization of a Linux root
  • 560.
    lesystem in termsof directories is well-de
  • 561.
    ned by theFilesystem Hierarchy Standard I http://www.linuxfoundation.org/collaborate/ workgroups/lsb/fhs I Most Linux systems conform to this speci
  • 562.
    cation I Applicationsexpect this organization I It makes it easier for developers and users as the
  • 563.
    lesystem organization issimilar in all systems Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 214/526
  • 564.
    Important directories (1) /bin Basic programs /boot Kernel image (only when the kernel is loaded from a
  • 565.
    lesystem, not commonon non-x86 architectures) /dev Device
  • 566.
    les (covered later) /etc System-wide con
  • 567.
    guration /home Directoryfor the users home directories /lib Basic libraries /media Mount points for removable media /mnt Mount points for static media /proc Mount point for the proc virtual
  • 568.
    lesystem Free Electrons- Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 215/526
  • 569.
    Important directories (2) /root Home directory of the root user /sbin Basic system programs /sys Mount point of the sysfs virtual
  • 570.
  • 571.
    les /usr /usr/binNon-basic programs /usr/lib Non-basic libraries /usr/sbin Non-basic system programs /var Variable data
  • 572.
    les. This includesspool directories and
  • 573.
    les, administrative andlogging data, and transient and temporary
  • 574.
    les Free Electrons- Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 216/526
  • 575.
    Separation of programsand libraries I Basic programs are installed in /bin and /sbin and basic libraries in /lib I All other programs are installed in /usr/bin and /usr/sbin and all other libraries in /usr/lib I In the past, on Unix systems, /usr was very often mounted over the network, through NFS I In order to allow the system to boot when the network was down, some binaries and libraries are stored in /bin, /sbin and /lib I /bin and /sbin contain programs like ls, ifconfig, cp, bash, etc. I /lib contains the C library and sometimes a few other basic libraries I All other programs and libraries are in /usr Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 217/526
  • 576.
    Linux Root Filesystem Device Files Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 218/526
  • 577.
    Devices I Oneof the kernel important role is to allow applications to access hardware devices I In the Linux kernel, most devices are presented to user space applications through two dierent abstractions I Character device I Block device I Internally, the kernel identi
  • 578.
    es each deviceby a triplet of information I Type (character or block) I Major (typically the category of device) I Minor (typically the identi
  • 579.
    er of thedevice) Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 219/526
  • 580.
    Types of devices I Block devices I A device composed of
  • 581.
    xed-sized blocks, thatcan be read and written to store data I Used for hard disks, USB keys, SD cards, etc. I Character devices I Originally, an in
  • 582.
    nite stream ofbytes, with no beginning, no end, no size. The pure example: a serial port. I Used for serial ports, terminals, but also sound cards, video acquisition devices, frame buers I Most of the devices that are not block devices are represented as character devices by the Linux kernel Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 220/526
  • 583.
  • 584.
    le I Avery important Unix design decision was to represent most of the system objects as
  • 585.
    les I Itallows applications to manipulate all system objects with the normal
  • 586.
    le API (open,read, write, close, etc.) I So, devices had to be represented as
  • 587.
    les to theapplications I This is done through a special artifact called a device
  • 588.
    le I Itis a special type of
  • 589.
  • 590.
    le name visibleto user space applications to the triplet (type, major, minor) that the kernel understands I All device
  • 591.
    les are byconvention stored in the /dev directory Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 221/526
  • 592.
  • 593.
  • 594.
    les in aLinux system $ ls -l /dev/ttyS0 /dev/tty1 /dev/sda1 /dev/sda2 /dev/zero brw-rw---- 1 root disk 8, 1 2011-05-27 08:56 /dev/sda1 brw-rw---- 1 root disk 8, 2 2011-05-27 08:56 /dev/sda2 crw------- 1 root root 4, 1 2011-05-27 08:57 /dev/tty1 crw-rw---- 1 root dialout 4, 64 2011-05-27 08:56 /dev/ttyS0 crw-rw-rw- 1 root root 1, 5 2011-05-27 08:56 /dev/zero Example C code that uses the usual
  • 595.
    le API towrite data to a serial port int fd; fd = open(/dev/ttyS0, O_RDWR); write(fd, Hello, 5); close(fd); Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 222/526
  • 596.
  • 597.
    les I Ona basic Linux system, the device
  • 598.
    les have tobe created manually using the mknod command I mknod /dev/device [c|b] major minor I Needs root privileges I Coherency between device
  • 599.
    les and deviceshandled by the kernel is left to the system developer I On more elaborate Linux systems, mechanisms can be added to create/remove them automatically when devices appear and disappear I devtmpfs virtual
  • 600.
    lesystem, since kernel2.6.32 I udev daemon, solution used by desktop and server Linux systems I mdev program, a lighter solution than udev Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 223/526
  • 601.
    Linux Root Filesystem Pseudo Filesystems Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 224/526
  • 602.
  • 603.
    lesystem I Theproc virtual
  • 604.
    lesystem exists sincethe beginning of Linux I It allows I The kernel to expose statistics about running processes in the system I The user to adjust at runtime various system parameters about process management, memory management, etc. I The proc
  • 605.
    lesystem is usedby many standard user space applications, and they expect it to be mounted in /proc I Applications such as ps or top would not work without the proc
  • 606.
    lesystem I Commandto mount /proc: mount -t proc nodev /proc I Documentation/filesystems/proc.txt in the kernel sources I man proc Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 225/526
  • 607.
    proc contents IOne directory for each running process in the system I /proc/pid I cat /proc/3840/cmdline I It contains details about the
  • 608.
    les opened bythe process, the CPU and memory usage, etc. I /proc/interrupts, /proc/devices, /proc/iomem, /proc/ioports contain general device-related information I /proc/cmdline contains the kernel command line I /proc/sys contains many
  • 609.
    les that canbe written to to adjust kernel parameters I They are called sysctl. See Documentation/sysctl/ in kernel sources. I Example echo 3 /proc/sys/vm/drop_caches Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 226/526
  • 610.
  • 611.
  • 612.
    lesystem is afeature integrated in the 2.6 Linux kernel I It allows to represent in user space the vision that the kernel has of the buses, devices and drivers in the system I It is useful for various user space applications that need to list and query the available hardware, for example udev or mdev. I All applications using sysfs expect it to be mounted in the /sys directory I Command to mount /sys: mount -t sysfs nodev /sys I $ ls /sys/ block bus class dev devices firmware fs kernel module power Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 227/526
  • 613.
  • 614.
    lesystem Free Electrons- Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 228/526
  • 615.
    Basic applications IIn order to work, a Linux system needs at least a few applications I An init application, which is the
  • 616.
    rst user spaceapplication started by the kernel after mounting the root
  • 617.
    lesystem I Thekernel tries to run /sbin/init, /bin/init, /etc/init and /bin/sh. I In the case of an initramfs, it will only look for /init. Another path can be supplied by the rdinit kernel argument. I If none of them are found, the kernel panics and the boot process is stopped. I The init application is responsible for starting all other user space applications and services I Usually a shell, to allow a user to interact with the system I Basic Unix applications, to copy
  • 618.
  • 619.
  • 620.
    les (commands likemv, cp, mkdir, cat, etc.) I These basic components have to be integrated into the root
  • 621.
    lesystem to makeit usable Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 229/526
  • 622.
    Overall booting process Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 230/526
  • 623.
    Overall booting processwith initramfs Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 231/526
  • 624.
    Busybox Busybox FreeElectrons ©Copyright 2004-2014, Free Electrons. Creative Commons BY-SA 3.0 license. Corrections, suggestions, contributions and translations are welcome! Embedded Linux Developers Free Electrons Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 232/526
  • 625.
    Why Busybox? IA Linux system needs a basic set of programs to work I An init program I A shell I Various basic utilities for
  • 626.
  • 627.
    guration I Innormal Linux systems, these programs are provided by dierent projects I coreutils, bash, grep, sed, tar, wget, modutils, etc. are all dierent projects I A lot of dierent components to integrate I Components not designed with embedded systems constraints in mind: they are not very con
  • 628.
    gurable and havea wide range of features I Busybox is an alternative solution, extremely common on embedded systems Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 233/526
  • 629.
    General purpose toolbox:BusyBox I Rewrite of many useful Unix command line utilities I Integrated into a single project, which makes it easy to work with I Designed with embedded systems in mind: highly con
  • 630.
    gurable, no unnecessaryfeatures I All the utilities are compiled into a single executable, /bin/busybox I Symbolic links to /bin/busybox are created for each application integrated into Busybox I For a fairly featureful con
  • 631.
    guration, less than500 KB (statically compiled with uClibc) or less than 1 MB (statically compiled with glibc). I http://www.busybox.net/ Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 234/526
  • 632.
    BusyBox commands! Commandsavailable in BusyBox 1.13 [, [[, addgroup, adduser, adjtimex, ar, arp, arping, ash, awk, basename, bbconfig, bbsh, brctl, bunzip2, busybox, bzcat, bzip2, cal, cat, catv, chat, chattr, chcon, chgrp, chmod, chown, chpasswd, chpst, chroot, chrt, chvt, cksum, clear, cmp, comm, cp, cpio, crond, crontab, cryptpw, cttyhack, cut, date, dc, dd, deallocvt, delgroup, deluser, depmod, devfsd, df, dhcprelay, diff, dirname, dmesg, dnsd, dos2unix, dpkg, dpkg_deb, du, dumpkmap, dumpleases, e2fsck, echo, ed, egrep, eject, env, envdir, envuidgid, ether_wake, expand, expr, fakeidentd, false, fbset, fbsplash, fdflush, fdformat, fdisk, fetchmail, fgrep, find, findfs, fold, free, freeramdisk, fsck, fsck_minix, ftpget, ftpput, fuser, getenforce, getopt, getsebool, getty, grep, gunzip, gzip, halt, hd, hdparm, head, hexdump, hostid, hostname, httpd, hush, hwclock, id, ifconfig, ifdown, ifenslave, ifup, inetd, init, inotifyd, insmod, install, ip, ipaddr, ipcalc, ipcrm, ipcs, iplink, iproute, iprule, iptunnel, kbd_mode, kill, killall, killall5, klogd, lash, last, length, less, linux32, linux64, linuxrc, ln, load_policy, loadfont, loadkmap, logger, login, logname, logread, losetup, lpd, lpq, lpr, ls, lsattr, lsmod, lzmacat, makedevs, man, matchpathcon, md5sum, mdev, mesg, microcom, mkdir, mke2fs, mkfifo, mkfs_minix, mknod, mkswap, mktemp, modprobe, more, mount, mountpoint, msh, mt, mv, nameif, nc, netstat, nice, nmeter, nohup, nslookup, od, openvt, parse, passwd, patch, pgrep, pidof, ping, ping6, pipe_progress, pivot_root, pkill, poweroff, printenv, printf, ps, pscan, pwd, raidautorun, rdate, rdev, readahead, readlink, readprofile, realpath, reboot, renice, reset, resize, restorecon, rm, rmdir, rmmod, route, rpm, rpm2cpio, rtcwake, run_parts, runcon, runlevel, runsv, runsvdir, rx, script, sed, selinuxenabled, sendmail, seq, sestatus, setarch, setconsole, setenforce, setfiles, setfont, setkeycodes, setlogcons, setsebool, setsid, setuidgid, sh, sha1sum, showkey, slattach, sleep, softlimit, sort, split, start_stop_daemon, stat, strings, stty, su, sulogin, sum, sv, svlogd, swapoff, swapon, switch_root, sync, sysctl, syslogd, tac, tail, tar, taskset, tcpsvd, tee, telnet, telnetd, test, tftp, tftpd, time, top, touch, tr, traceroute, true, tty, ttysize, tune2fs, udhcpc, udhcpd, udpsvd, umount, uname, uncompress, unexpand, uniq, unix2dos, unlzma, unzip, uptime, usleep, uudecode, uuencode, vconfig, vi, vlock, watch, watchdog, wc, wget, which, who, whoami, xargs, yes, zcat, zcip Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 235/526
  • 633.
    Applet highlight: Busyboxinit I Busybox provides an implementation of an init program I Simpler than the init implementation found on desktop/server systems: no runlevels are implemented I A single con
  • 634.
  • 635.
    le: /etc/inittab IEach line has the form id::action:process I Allows to run services at startup, and to make sure that certain services are always running on the system I See examples/inittab in Busybox for details on the con
  • 636.
    guration Free Electrons- Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 236/526
  • 637.
    Applet highlight -BusyBox vi I If you are using BusyBox, adding vi support only adds 20K. (built with shared libraries, using uClibc). I You can select which exact features to compile in. I Users hardly realize that they are using a lightweight vi version! I Tip: you can learn vi on the desktop, by running the vimtutor command. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 237/526
  • 638.
  • 639.
    guring BusyBox IGet the latest stable sources from http://busybox.net I Con
  • 640.
  • 641.
    le): I makedefconfig Good to begin with BusyBox. Con
  • 642.
    gures BusyBox withall options for regular users. I make allnoconfig Unselects all options. Good to con
  • 643.
    gure only whatyou need. I make xconfig (graphical, needs the libqt3-mt-dev package) or make menuconfig (text) Same con
  • 644.
    guration interfaces asthe ones used by the Linux kernel (though older versions are used). Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 238/526
  • 645.
  • 646.
    g You canchoose: I the commands to compile, I and even the command options and features that you need! Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 239/526
  • 647.
    Compiling BusyBox ISet the cross-compiler pre
  • 648.
  • 649.
    guration interface: BusyBoxSettings - Build Options - Cross Compiler prefix Example: arm-linux- I Set the installation directory in the con
  • 650.
    guration interface: BusyBoxSettings - Installation Options - BusyBox installation prefix I Add the cross-compiler path to the PATH environment variable: export PATH=/usr/xtools/arm-unknown-linux-uclibcgnueabi/ bin:$PATH I Compile BusyBox: make I Install it (this creates a Unix directory structure symbolic links to the busybox executable): make install Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 240/526
  • 651.
    Practical lab -A tiny embedded system I Make Linux boot on a directory on your workstation, shared by NFS I Create and con
  • 652.
    gure a minimalistic Linux embedded system I Install and use BusyBox I System startup with /sbin/init I Setup a simple web interface I Use shared libraries Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 241/526
  • 653.
  • 654.
  • 655.
    lesystems Free Electrons ©Copyright 2004-2014, Free Electrons. Creative Commons BY-SA 3.0 license. Corrections, suggestions, contributions and translations are welcome! Embedded Linux Developers Free Electrons Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 242/526
  • 656.
    Block vs. ash I Storage devices are classi
  • 657.
    ed in twomain types: block devices and ash devices I They are handled by dierent subsystems and dierent
  • 658.
    lesystems I Blockdevices can be read and written to on a per-block basis, without erasing. I Hard disks, oppy disks, RAM disks I USB keys, Compact Flash, SD card: these are based on ash storage, but have an integrated controller that emulates a block device, managing and erasing ash sectors in a transparent way. I Flash devices can be read, but writing requires erasing, and often occurs on a larger size than the block size. I NOR ash, NAND ash Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 243/526
  • 659.
    Block device list I The list of all block devices available in the system can be found in /proc/partitions $ cat /proc/partitions major minor #blocks name 179 0 3866624 mmcblk0 179 1 73712 mmcblk0p1 179 2 3792896 mmcblk0p2 8 0 976762584 sda 8 1 1060258 sda1 8 2 975699742 sda2 I And also in /sys/block/ Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 244/526
  • 660.
  • 661.
  • 662.
    lesystems I Canbe left in a non-coherent state after a system crash or sudden powero, which requires a full
  • 663.
    lesystem check after reboot. I ext2: traditional Linux
  • 664.
    lesystem (repair itwith fsck.ext2) I vfat: traditional Windows
  • 665.
    lesystem (repair itwith fsck.vfat on GNU/Linux or Scandisk on Windows) Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 245/526
  • 666.
  • 667.
    lesystems I Designedto stay in a correct state even after system crashes or a sudden powero I All writes are
  • 668.
    rst described inthe journal before being committed to
  • 669.
    les Free Electrons- Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 246/526
  • 670.
    Filesystem recovery aftercrashes I Thanks to the journal, the
  • 671.
    lesystem is neverleft in a corrupted state I Recently saved data could still be lost Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 247/526
  • 672.
  • 673.
  • 674.
    lesystems I ext3:ext2 with journal extension ext4: the new generation with many improvements. Ready for production. They are the default
  • 675.
    lesystems for all Linux systems in the world. I The Linux kernel supports many other
  • 676.
    lesystems: reiserFS, JFS,XFS, etc. Each of them have their own characteristics, but are more oriented towards server or scienti
  • 677.
    c workloads IBtrfs (Butter FS) The next generation. Great performance. In mainline but still experimental. We recommend ext2 for very small partitions ( 5 MB), because other
  • 678.
    lesystems need toomuch space for metadata (ext3 and ext4 need about 1 MB for a 4 MB partition). Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 248/526
  • 679.
    Creating ext2/ext3/ext4 volumes I To create an empty ext2/ext3/ext4
  • 680.
    lesystem on ablock device or inside an already-existing image
  • 681.
    le I mkfs.ext2/dev/hda3 I mkfs.ext3 /dev/sda2 I mkfs.ext4 /dev/sda3 I mkfs.ext2 disk.img I To create a
  • 682.
    lesystem image froma directory containing all your
  • 683.
    les and directories I Use the genext2fs tool, from the package of the same name I genext2fs -d rootfs/ rootfs.img I Your image is then ready to be transferred to your block device Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 249/526
  • 684.
  • 685.
  • 686.
    lesystem image hasbeen created, one can access and modi
  • 687.
    es its contentsfrom the development workstation, using the loop mechanism I Example: genext2fs -d rootfs/ rootfs.img mkdir /tmp/tst mount -t ext2 -o loop rootfs.img /tmp/tst I In the /tmp/tst directory, one can access and modify the contents of the rootfs.img
  • 688.
    le. I Thisis possible thanks to loop, which is a kernel driver that emulates a block device with the contents of a
  • 689.
    le. I Donot forget to run umount before using the
  • 690.
    lesystem image! FreeElectrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 250/526
  • 691.
    F2FS http://en.wikipedia.org/wiki/F2FS IFilesystem optimized for block devices based on NAND ash I Available in the mainline Linux kernel I Benchmarks: best performer on ash devices most of the time: See http://lwn.net/Articles/520003/ I Technical details: http://lwn.net/Articles/518988/ Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 251/526
  • 692.
  • 693.
    lesystem for blockdevices. Fine for parts of a
  • 694.
    lesystem which canbe read-only (kernel, binaries...) I Great compression rate and read access performance I Used in most live CDs and live USB distributions I Supports LZO compression for better performance on embedded systems with slow CPUs (at the expense of a slightly degraded compression rate) I Now supports XZ algorithm, for a much better compression rate, at the expense of higher CPU usage and time. Benchmarks: (roughly 3 times smaller than ext3, and 2-4 times faster) http://elinux.org/Squash_Fs_Comparisons Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 252/526
  • 695.
    Squashfs - Howto use I Need to install the squashfs-tools package I Creation of the image I On your workstation, create your
  • 696.
    lesystem image: mksquashfsrootfs/ rootfs.sqfs I Caution: if the image already exists remove it
  • 697.
    rst, or usethe -noappend option. I Installation of the image I Let's assume your partition on the target is in /dev/sdc1 I Copy the
  • 698.
    lesystem image onthe device dd if=rootfs.sqfs of=/dev/sdc1 Be careful when using dd to not overwrite the incorrect partition! I Mount your
  • 699.
    lesystem: mount -tsquashfs /dev/sdc1 /mnt/root Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 253/526
  • 700.
  • 701.
    lesystem of course! Perfect to store temporary data in RAM: system log
  • 702.
  • 703.
  • 704.
    guration: File systems- Pseudo filesystems Lives in the Linux
  • 705.
    le cache. Doesn'twaste RAM: unlike ramdisks, no need to copy
  • 706.
  • 707.
    le cache, growsand shrinks to accommodate stored
  • 708.
    les. Saves RAM:can swap out pages to disk when needed. I How to use: choose a name to distinguish the various tmpfs instances you could have. Examples: mount -t tmpfs varrun /var/run mount -t tmpfs udev /dev See Documentation/filesystems/tmpfs.txt in kernel sources. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 254/526
  • 709.
  • 710.
    lesystems Good ideato split your block storage into: I A compressed read-only partition (Squashfs) Typically used for the root
  • 711.
    lesystem (binaries, kernel...). Compression saves space. Read-only access protects your system from mistakes and data corruption. I A read-write partition with a journaled
  • 712.
    lesystem (like ext3) Used to store user or con
  • 713.
  • 714.
    lesystem integrity after power o or crashes. I Ram storage for temporary
  • 715.
    les (tmpfs) FreeElectrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 255/526
  • 716.
  • 717.
    lesystems I Creatingpartitions on your block storage I Booting your system with a mix of
  • 718.
  • 719.
  • 720.
    guration and user data, and tmpfs for temporary system
  • 721.
    les. Free Electrons- Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 256/526
  • 722.
  • 723.
  • 724.
    lesystems Free Electrons ©Copyright 2004-2014, Free Electrons. Creative Commons BY-SA 3.0 license. Corrections, suggestions, contributions and translations are welcome! Embedded Linux Developers Free Electrons Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 257/526
  • 725.
    The MTD subsystem MTD stands for Memory Technology Devices Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 258/526
  • 726.
    MTD devices IMTD devices are visible in /proc/mtd I The mtdchar driver creates a character device for each MTD device of the system I Usually named /dev/mtdX or /dev/mtdXro, major 90. Even minors for read-write access (/dev/mtdX), odd minors for read-only access (/dev/mtdXro) I Provide ioctl() to erase and manage the ash I Used by the mtd-utils utilities I The mtdblock driver creates a block device for each MTD device of the system I Usually named /dev/mtdblockX, major 31. Minor is the number of the MTD device I Allows read/write block-level access. But bad blocks are not handled, and no wear leveling is done for writes. I Primary use: mounting
  • 727.
    lesystems Free Electrons- Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 259/526
  • 728.
    MTD partitioning IMTD devices are usually partitioned I It allows to use dierent areas of the ash for dierent purposes: read-only
  • 729.
  • 730.
    lesystem, backup areas,bootloader area, kernel area, etc. I Unlike block devices, which contains their own partition table, the partitioning of MTD devices is described externally I Speci
  • 731.
    ed in theboard Device Tree I Hard-coded into the kernel code (if no Device Tree) I Speci
  • 732.
    ed through thekernel command line I Each partition becomes a separate MTD device I Dierent from block device labeling (hda3, sda2) I /dev/mtd1 is either the second partition of the
  • 733.
  • 734.
    rst partition ofthe second ash device Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 260/526
  • 735.
  • 736.
    nition of MTDpartitions (1) The Device Tree is the standard place to device MTD partitions for boards with Device Tree support. Example from arch/arm/boot/dts/omap3-igep0020.dts: nand@0,0 { linux,mtd-name= micron,mt29c4g96maz; [...] partition@0 { label = SPL; reg = 0 0x100000; }; partition@0x80000 { label = U-Boot; reg = 0x100000 0x180000; }; [...] partition@0x780000 { label = Filesystem; reg = 0x680000 0x1f980000; }; Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 261/526
  • 737.
  • 738.
    nition of MTDpartitions (2) For boards or platforms that do not use the Device Tree, MTD partitions can be de
  • 739.
    ned in thekernel. Legacy example from arch/arm/mach-omap2/board-igep0020.c (removed in 3.13): static struct mtd_partition igep2_flash_partitions[] = { { .name = X-Loader, .offset = 0, .size = 2 * (64*(2*2048)) }, { .name = U-Boot, .offset = MTDPART_OFS_APPEND, .size = 6 * (64*(2*2048)), }, [...] { .name = File System, .offset = MTDPART_OFS_APPEND, .size = MTDPART_SIZ_FULL, }, }; Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 262/526
  • 740.
    Modifying MTD partitions(1) I MTD partitions can fortunately be de
  • 741.
    ned through thekernel command line. I First need to
  • 742.
    nd the nameof the MTD device. Look at the kernel log at boot time. In the below example, the MTD device name is omap2-nand.0: 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 Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 263/526
  • 743.
    Modifying MTD partitions(2) I You can now use the mtdparts kernel boot parameter I Example: mtdparts=omap2-nand.0:512k(X-Loader)ro,1536k(U-Boot) ro,512k(Environment),4m(Kernel),16m(RootFS) ,-(Data) I We've just de
  • 744.
    ned 6 partitionsin the omap2-nand.0 device: I 1st stage bootloader (512 KiB, read-only) I U-Boot (1536 KiB, read-only) I U-Boot environment (512 KiB) I Kernel (4 MiB) I Root filesystem (16 MiB) I Data filesystem (Remaining space) Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 264/526
  • 745.
    Modifying MTD partitions(3) I Partition sizes must be multiple of the erase block size. You can use sizes in hexadecimal too. Remember the below sizes: 0x20000 = 128k, 0x100000 = 1m, 0x1000000 = 16m I ro lists the partition as read only I - is used to use all the remaining space. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 265/526
  • 746.
    mtd-utils I Aset of utilities to manipulate MTD devices I mtdinfo to get detailed information about an MTD device I flash_eraseall to completely erase a given MTD device I flashcp to write to NOR ash I nandwrite to write to NAND ash I UBI utilities I Flash
  • 747.
    lesystem image creationtools: mkfs.jffs2, mkfs.ubifs I Usually available as the mtd-utils package in your distribution I Most commands now also available in BusyBox I See http://www.linux-mtd.infradead.org/ Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 266/526
  • 748.
    js2 I Today'sstandard
  • 749.
    lesystem for MTD ash I Nice features: on the y compression (saves storage space and reduces I/O), power down reliable, wear-leveling and ECC. I Drawbacks: doesn't scale well I Mount time depending on
  • 750.
    lesystem size: thekernel has to scan the whole
  • 751.
    lesystem at mounttime, to read which block belongs to each
  • 752.
    le. I Needto use the CONFIG_JFFS2_SUMMARY kernel option to store such information in ash. This dramatically reduces mount time (from 16 s to 0.8 s for a 128 MB partition). I http://www.linux-mtd. infradead.org/doc/jffs2.html Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 267/526
  • 753.
    js2 - Howto use On the Linux target I Need either the mtd-utils package from the MTD project, or their embedded variants from Busybox I Erase and format a partition with js2: flash_eraseall -j /dev/mtd2 I Mount the partition: mount -t jffs2 /dev/mtdblock2 /mnt/flash I Fill the contents by writing (copying through the network or from external storage) I Other possibility: use a js2 image (see next page to make it): flash_eraseall /dev/mtd2 nandwrite -p /dev/mtd2 rootfs.jffs2 Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 268/526
  • 754.
    How to createa js2 image I mkfs.jffs2 command available in the mtd-utils package. Caution: unlike some mkfs commands, it doesn't create a
  • 755.
  • 756.
  • 757.
    nd the eraseblock size (on the target running Linux): cat /proc/mtd For example: 00040000 (256 KiB) I Then create the image on your workstation: mkfs.jffs2 --pad --no-cleanmarkers -- eraseblock=256 -d rootfs/ -o rootfs.jffs2 I The --pad option pads the js2 image contents until the end of the
  • 758.
  • 759.
    ne if thejs2 image is smaller than the MTD partition. The js2
  • 760.
    le system willuse the entire partition anyway. I The --no-cleanmarkers option is for NAND ash only. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 269/526
  • 761.
    Mounting a js2image on your host Useful to edit jffs2 images on your development system Mounting an MTD device as a loop device is a bit complex task. Here's an example for jffs2, for your reference: I First
  • 762.
    nd the eraseblock size used to create the js2 image. Let's assume it is 256KiB (262144 bytes). I Create a block device from the image losetup /dev/loop0 root.jffs2 I Emulate an MTD device from a block device, using the block2mtd kernel module modprobe block2mtd block2mtd=/dev/loop0,262144 I Load the mtdblock driver if needed modprobe mtdblock I Finally, mount the
  • 763.
    lesystem (create /mnt/jffs2if needed) mount -t jffs2 /dev/mtdblock0 /mnt/jffs2 Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 270/526
  • 764.
    Initializing js2 partitionsfrom U-boot You may not want to have mtd-utils on your target! I Create a JFFS2 image on your workstation I In the U-Boot prompt: I Download the js2 image to RAM with tftp Or copy this image to RAM from external storage (U-boot understands FAT
  • 765.
    lesystems and supportsUSB storage) I Flash it inside an MTD partition (exact instructions depending on ash type, NOR or NAND, reuse the instructions used to ash your kernel). Make sure to write only the size of the image, not more! I Limitation: need to split the js2 image in several chunks if bigger than the RAM size. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 271/526
  • 766.
    Booting from ajs2 root partition I To boot on a js2 root
  • 767.
    lesystem, add root=/dev/mtdblockxand rootfstype=jffs2 to the Linux command line arguments. I The Linux kernel cannot autodetect the type of a root
  • 768.
    lesystem in MTD ash. That's why the rootfstype kernel parameter is needed. I For root partitions on a block device, using rootfstype (for example: rootfstype=ext3) can reduce boot time a little bit. This way, the kernel doesn't have to try all the block
  • 769.
    lesystems it supports. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 272/526
  • 770.
    yas2 I Mainlysupports NAND ash I No compression I Wear leveling, ECC, power failure resistant I Fast boot time I Code available separately through git (Dual GPL / Proprietary license for non Linux operating systems) I http://www.yaffs.net/ Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 273/526
  • 771.
    yas2 - Howto use I Erase a partition: flash_eraseall /dev/mtd2 I The
  • 772.
    lesystem is automaticallyformatted at the
  • 773.
    rst mount: mount-t yaffs2 /dev/mtdblock2 /mnt/flash I Images can be created with the mkyaffs tool, from yaffs-utils http://code.google.com/p/yaffs2utils/ Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 274/526
  • 774.
    UBI (1) UnsortedBlock Images I http://www.linux-mtd.infradead.org/doc/ubi.html I Volume management system on top of MTD devices. I Allows to create multiple logical volumes and spread writes across all physical blocks. I Takes care of managing the erase blocks and wear leveling. Makes
  • 775.
    lesystems easier toimplement. I Wear leveling can operate on the whole storage, not only on individual partitions (strong advantage). I Volumes can be dynamically resized or, on the opposite, can be read-only (static). Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 275/526
  • 776.
    UBI (2) FreeElectrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 276/526
  • 777.
  • 778.
    lesystem, from thesame linux-mtd developers. I Works on top of UBI volumes I Has a noticeable metadata overhead on very small partitions (4M, 8M) Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 277/526
  • 779.
    UBI layout example Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 278/526
  • 780.
    UBI - Preparation I Have /dev/ mounted as a devtmpfs
  • 781.
    lesystem I Eraseyour ash partition while preserving your erase counters ubiformat /dev/mtd1 See http://www.linux-mtd.infradead.org/faq/ubi.html if you face problems I Attach UBI to one (of several) of the MTD partitions: ubiattach /dev/ubi_ctrl -m 1 I This command creates the ubi0 device, which represent the full UBI space stored on MTD device 1 (new /dev/ubi0 character device) Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 279/526
  • 782.
    UBI - Volumemanagement I Volume creation with ubimkvol I ubimkvol /dev/ubi0 -N test -s 116MiB I ubimkvol /dev/ubi0 -N test -m (max available size) I The volume is then identi
  • 783.
    ed as ubi0:testfor the mount/umount commands I Volume removal with ubirmvol I ubirmvol /dev/ubi0 -N test Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 280/526
  • 784.
    UBIFS - Howto use I When a UBI volume is created, creating an empty UBIFS
  • 785.
    lesystem is justa matter of mounting it I mount -t ubifs ubi0:test /mnt/flash I Images of UBIFS
  • 786.
    lesystems can becreated using the mkfs.ubifs utility I mkfs.ubifs -m 4096 -e 252KiB -c 1000 - r rootfs/ ubifs.img I -m 4096, minimal I/O size (see /sys/class/mtd/mtdx/writesize). I -e 252KiB, logical erase block size (smaller than PEB size, look at dmesg) I -c 1000, maximum number of logical erase blocks. Details: http://linux-mtd.infradead.org/faq/ubifs.html#L_max_leb_cnt I Can be written to a UBI volume using ubiupdatevol: ubiupdatevol /dev/ubi0_0 /path/to/ubifs.img Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 281/526
  • 787.
    Ubinize (1) Aftercreating images of UBIFS
  • 788.
    lesystems, images ofa full UBI space, containing several volumes, can be created using ubinize: I Can be written to a raw MTD partition in U-boot. I Must be written using nand write.trimffs. I Caution: nand erase will also erase the Erase Counters First, create a con
  • 789.
  • 790.
    le describing theUBI volumes (ubi.ini): [RFS-volume] mode=ubi image=rootfs.ubifs vol_id=1 vol_size=30MiB vol_type=dynamic vol_name=rootfs vol_flags=autoresize vol_alignment=1 Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 282/526
  • 791.
    Ubinize (2) Iubinize -o ubi.img -p 256KiB -m 4096 ubi.ini I Creates ubi.img, with 256KiB physical erase blocks, 4096 minimum I/O size (-m). I See http://free-electrons.com/blog/creating-flashing- ubi-ubifs-images/ for details about creating UBI and UBIFS images. I See http://www.linux-mtd. infradead.org/doc/ubi.html#L_flasher_algo for what is required when ashing UBI images containing UBIFS volumes. I Build systems like Buildroot can run ubinize automatically! Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 283/526
  • 792.
    UBIFS - Howto prepare a root fs I Create the UBIFS image for the root
  • 793.
    lesystem I Createthe UBI device image (with ubinize) I Flash it from the bootloader I Pass UBI layout information to the kernel: I rootfstype=ubifs ubi.mtd=1 root=ubi0:rootfs Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 284/526
  • 794.
  • 795.
    lesystem benchmarks http://elinux.org/Flash_Filesystem_Benchmarks I js2 I Worst performance I Requires CONFIG_SUMMARY to have acceptable boot time I yas2 I Good performance, but not in mainline Linux I ubifs I Best solution and performance for medium and big partitions I Too much metadata overhead small partitions (only case when yaffs2 and jffs2 are still useful) Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 285/526
  • 796.
    Issues with ash-basedblock storage I Flash storage made available only through a block interface. I Hence, no way to access a low level ash interface and use the Linux
  • 797.
    lesystems doing wearleveling. I No details about the layer (Flash Translation Layer) they use. Details are kept as trade secrets, and may hide poor implementations. I Can use ashbench (https://github.com/bradfa/flashbench) to
  • 798.
    nd out theerase block size and optimize
  • 799.
    lesystem formating. INot knowing about the wear leveling algorithm, it is highly recommended to limit the number of writes to these devices. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 286/526
  • 800.
    Reducing the numberof writes I Of course, do not use your ash storage as swap area (rare in embedded systems anyway) I Mount your
  • 801.
    lesystems as read-only,or use read-only
  • 802.
    lesystems (SquashFS), wheneverpossible. I Keep volatile
  • 803.
    les in RAM(tmpfs) I Don't use the sync mount option (commits writes immediately). Use the fsync() system call for per-
  • 804.
    le synchronization. IYou may decide to do without journaled
  • 805.
    lesystems. They causemore writes, but are also much more power down resistant (trade-o). Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 287/526
  • 806.
    Useful reading IArnd Bergmann: Optimizing Linux with cheap ash drives In depth coverage of ash storage with a block interface. http://lwn.net/Articles/428584/ I Managing ash storage with Linux: http://free-electrons.com/blog/managing-flash-storage- with-linux/ I Documentation on the linux-mtd website: http://www.linux-mtd.infradead.org/ Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 288/526
  • 807.
    Practical lab -Flash Filesystems I Creating partitions in your internal ash storage. I Use a read-only JFFS2 partition for the system I Use a read-write JFFS2 partition for data Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 289/526
  • 808.
    Embedded Linux systemdevelopment Embedded Linux system development Free Electrons ©Copyright 2004-2014, Free Electrons. Creative Commons BY-SA 3.0 license. Corrections, suggestions, contributions and translations are welcome! Embedded Linux Developers Free Electrons Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 290/526
  • 809.
    Contents I Usingopen-source components I Tools for the target device I Networking I System utilities I Language interpreters I Audio, video and multimedia I Graphical toolkits I Databases I Web browsers I System building I Commercial tool sets and distributions Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 291/526
  • 810.
    Embedded Linux systemdevelopment Leveraging open-source components in an Embedded Linux system Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 292/526
  • 811.
    Third party librariesand applications I One of the advantages of embedded Linux is the wide range of third-party libraries and applications that one can leverage in its product I They are freely available, freely distributable, and thanks to their open-source nature, they can be analyzed and modi
  • 812.
    ed according tothe needs of the project I However, eciently re-using these components is not always easy. One must: I Find these components I Choose the most appropriate ones I Cross-compile them I Integrate them in the embedded system and with the other applications Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 293/526
  • 813.
    Find existing components I Free Software Directory http://directory.fsf.org I Look at other embedded Linux products, and see what their components are I Look at the list of software packaged by embedded Linux build systems I These are typically chosen for their suitability to embedded systems I Ask the community or Google I This presentation will also feature a list of components for common needs Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 294/526
  • 814.
    Choosing components INot all free software components are necessarily good to re-use. One must pay attention to: I 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 trac and the version control system activity. I 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. I License. The license of the component must match your licensing constraints. For example, GPL libraries cannot be used in proprietary applications. I 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! Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 295/526
  • 815.
    Licenses (1) IAll software that are under a free software license give four freedoms to all users I Freedom to use I Freedom to study I Freedom to copy I Freedom to modify and distribute modi
  • 816.
    ed copies ISee http://www.gnu.org/philosophy/free-sw.html for a de
  • 817.
    nition of FreeSoftware I Open Source software, as per the de
  • 818.
    nition of theOpen Source Initiative, are technically similar to Free Software in terms of freedoms I See http://www.opensource.org/docs/osd for the de
  • 819.
    nition of OpenSource Software Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 296/526
  • 820.
    Licenses (2) IFree Software licenses fall in two main categories I The copyleft licenses I The non-copyleft licenses I The concept of copyleft is to ask for reciprocity in the freedoms given to a user. I The result is that when you receive a software under a copyleft free software license and distribute modi
  • 821.
    ed versions ofit, you must do so under the same license I Same freedoms to the new users I It's an incentive to contribute back your changes instead of keeping them secret I Non-copyleft licenses have no such requirements, and modi
  • 822.
    ed versions canbe kept proprietary, but they still require attribution Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 297/526
  • 823.
    GPL I GNUGeneral Public License I Covers around 55% of the free software projects I Including the Linux kernel, Busybox and many applications I Is a copyleft license I Requires derivative works to be released under the same license I Programs linked with a library released under the GPL must also be released under the GPL I Some programs covered by version 2 (Linux kernel, Busybox and others) I More and more programs covered by version 3, released in 2007 I Major change for the embedded market: the requirement that the user must be able to run the modi
  • 824.
    ed versions onthe device, if the device is a consumer device Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 298/526
  • 825.
    GPL: redistribution INo obligation when the software is not distributed I You can keep your modi
  • 826.
    cations secret untilthe product delivery I It is then authorized to distribute binary versions, if one of the following conditions is met: I Convey the binary with a copy of the source on a physical medium I Convey the binary with a written oer valid for 3 years that indicates how to fetch the source code I Convey the binary with the network address of a location where the source code can be found I See section 6. of the GPL license I In all cases, the attribution and the license must be preserved I See section 4. and 5. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 299/526
  • 827.
    LGPL I GNULesser General Public License I Covers around 10% of the free software projects I A copyleft license I Modi
  • 828.
    ed versions mustbe released under the same license I But, programs linked against a library under the LGPL do not need to be released under the LGPL and can be kept proprietary. I However, the user must keep the ability to update the library independently from the program. Dynamic linking is the easiest solution. Statically linked executables are only possible if the developer provides a way to relink with an update (with source code or linkable object
  • 829.
    les). I Usedinstead of the GPL for most of the libraries, including the C libraries I Some exceptions: MySQL, or Qt = 4.4 I Also available in two versions, v2 and v3 Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 300/526
  • 830.
    Licensing: examples IYou make modi
  • 831.
    cations to theLinux kernel (to add drivers or adapt to your board), to Busybox, U-Boot or other GPL software I You must release the modi
  • 832.
    ed versions underthe same license, and be ready to distribute the source code to your customers I You make modi
  • 833.
    cations to theC library or any other LGPL library I You must release the modi
  • 834.
    ed versions underthe same license I You create an application that relies on LGPL libraries I You can keep your application proprietary, but you must link dynamically with the LGPL libraries I You make modi
  • 835.
    cations to anon-copyleft licensed software I You can keep your modi
  • 836.
    cations proprietary, butyou must still credit the authors Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 301/526
  • 837.
    Non-copyleft licenses IA large family of non-copyleft licenses that are relatively similar in their requirements I A few examples I Apache license (around 4%) I BSD license (around 6%) I MIT license (around 4%) I X11 license I Artistic license (around 9 %) Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 302/526
  • 838.
    BSD license Copyright(c) year, copyright holder All rights reserved. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * Neither the name of the organization nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission. [...] Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 303/526
  • 839.
    Is this freesoftware? I Most of the free software projects are covered by 10 well-known licenses, so it is fairly easy for the majority of project to get a good understanding of the license I Otherwise, read the license text I Check Free Software Foundation's opinion http://www.fsf.org/licensing/licenses/ I Check Open Source Initiative's opinion http://www.opensource.org/licenses Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 304/526
  • 840.
    Respect free softwarelicenses I Free Software is not public domain software, the distributors have obligations due to the licenses I Before using a free software component, make sure the license matches your project constraints I Make sure to keep a complete list of the free software packages you use, the original version you used and to keep your modi
  • 841.
    cations and adaptationswell-separated from the original version I Conform to the license requirements before shipping the product to the customers I Free Software licenses have been enforced successfully in courts. Organizations which can help: I GPL-violations.org, http://www.gpl-violations.org I Software Freedom Law Center, http://www.softwarefreedom.org/ I Software Freedom Conservancy, http://sfconservancy.org/ I Ask your legal department! Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 305/526
  • 842.
    Keeping changes separate(1) I When integrating existing open-source components in your project, it is sometimes needed to make modi
  • 843.
    cations to them I Better integration, reduced footprint, bug
  • 844.
    xes, new features, etc. I Instead of mixing these changes, it is much better to keep them separate from the original component version I If the component needs to be upgraded, easier to know what modi
  • 845.
    cations were madeto the component I If support from the community is requested, important to know how dierent the component we're using is from the upstream version I Makes contributing the changes back to the community possible I It is even better to keep the various changes made on a given component separate I Easier to review and to update to newer versions Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 306/526
  • 846.
    Keeping changes separate(2) I The simplest solution is to use Quilt I Quilt is a tool that allows to maintain a stack of patches over source code I Makes it easy to add, remove modi
  • 847.
    cations from apatch, to add and remove patches from stack and to update them I The stack of patches can be integrated into your version control system I https://savannah.nongnu.org/projects/quilt/ I Another solution is to use a version control system I Import the original component version into your version control system I Maintain your changes in a separate branch Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 307/526
  • 848.
    Embedded Linux systemdevelopment Tools for the target device: Networking Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 308/526
  • 849.
    ssh server andclient: Dropbear http://matt.ucc.asn.au/dropbear/dropbear.html I Very small memory footprint ssh server for embedded systems I Satis
  • 850.
    es most needs.Both client and server! I Size: 110 KB, statically compiled with uClibc on i386. (OpenSSH client and server: approx 1200 KB, dynamically compiled with glibc on i386) I Useful to: I Get a remote console on the target device I Copy
  • 851.
    les to andfrom the target device (scp or rsync -e ssh). I An alternative to OpenSSH, used on desktop and server systems. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 309/526
  • 852.
  • 853.
    ts of aweb server interface Many network enabled devices can just have a network interface I Examples: modems / routers, IP cameras, printers... I No need to develop drivers and applications for computers connected to the device. No need to support multiple operating systems! I Just need to develop static or dynamic HTML pages (possibly with powerful client-side JavaScript). Easy way of providing access to device information and parameters. I Reduced hardware costs (no LCD, very little storage space needed) Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 310/526
  • 854.
    Web servers IBusyBox http server: http://busybox.net I Tiny: only adds 9 K to BusyBox (dynamically linked with glibc on i386, with all features enabled.) I Sucient features for many devices with a web interface, including CGI, http authentication and script support (like PHP, with a separate interpreter). I License: GPL I lighttpd: http://lighttpd.net Low footprint server good at managing high loads. May be useful in embedded systems too I Other possibilities: Boa, thttpd, nginx, etc Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 311/526
  • 855.
    Network utilities (1) I avahi is an implementation of Multicast DNS Service Discovery, that allows programs to publish and discover services on a local network I bind, a DNS server I iptables, the user space tools associated to the Linux
  • 856.
  • 857.
    lter I iwand wireless tools, the user space tools associated to Wireless devices I netsnmp, implementation of the SNMP protocol I openntpd, implementation of the Network Time Protocol, for clock synchronization I openssl, a toolkit for SSL and TLS connections Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 312/526
  • 858.
    Network utilities (2) I pppd, implementation of the Point to Point Protocol, used for dial-up connections I samba, implements the SMB and CIFS protocols, used by Windows to share
  • 859.
    les and printers I coherence, a UPnP/DLNA implementation I vsftpd, proftpd, FTP servers Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 313/526
  • 860.
    Embedded Linux systemdevelopment Tools for the target device: System utilities Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 314/526
  • 861.
    System utilities Idbus, an inter-application object-oriented communication bus I gpsd, a daemon to interpret and share GPS data I libraw1394, raw access to Firewire devices I libusb, a user space library for accessing USB devices without writing an in-kernel driver I Utilities for kernel subsystems: i2c-tools for I2C, input-tools for input, mtd-utils for MTD devices, usbutils for USB devices Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 315/526
  • 862.
    Embedded Linux systemdevelopment Tools for the target device: Language interpreters Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 316/526
  • 863.
    Language interpreters IInterpreters for the most common scripting languages are available. Useful for I Application development I Web services development I Scripting I Languages supported I Lua I Python I Perl I Ruby I TCL I PHP Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 317/526
  • 864.
    Embedded Linux systemdevelopment Tools for the target device: Audio, video and multimedia Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 318/526
  • 865.
    Audio, video andmultimedia I GStreamer, a multimedia framework I Allows to decode/encode a wide variety of codecs. I Supports hardware encoders and decoders through plugins, proprietary/speci
  • 866.
    c plugins areoften provided by SoC vendors. I alsa-lib, the user space tools associated to the ALSA sound kernel subsystem I Directly using encoding and decoding libraries, if you decide not to use GStreamer: libavcodec, libogg, libtheora, libvpx, ac, libvorbis, libmad, libsnd
  • 867.
    le, speex, etc. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 319/526
  • 868.
    Embedded Linux systemdevelopment Tools for the target device: Graphical toolkits Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 320/526
  • 869.
    Embedded Linux systemdevelopment Graphical toolkits: Low-level solutions and layers Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 321/526
  • 870.
    DirectFB (1) ILow-level graphical library I Lines, rectangles, triangles drawing and
  • 871.
    lling I Blitting, ipping I Text drawing I Windows and transparency I Image loading and video display I But also handles input event handling: mice, keyboards, joysticks, touchscreens, etc. I Provides accelerated graphic operations on a few hardware platforms I Single-application by default, but multiple applications can share the framebuer thanks to fusion I License: LGPL 2.1 I http://www.directfb.org Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 322/526
  • 872.
    DirectFB: architecture FreeElectrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 323/526
  • 873.
    DirectFB: usage (1) I Multimedia applications I For example the Disko framework, for set-top box related applications I Simple graphical applications I Industrial control I Device control with limited number of widgets I Visualization applications I As a lower layer for higher-level graphical libraries Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 324/526
  • 874.
    DirectFB: architecture FreeElectrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 325/526
  • 875.
    X.org - KDrive I Stand-alone simpli
  • 876.
    ed version ofthe X server, for embedded systems I Formerly know as Tiny-X I Kdrive is integrated in the ocial X.org server I Works on top of the Linux frame buer, thanks to the Xfbdev variant of the server I Real X server I Fully supports the X11 protocol: drawing, input event handling, etc. I Allows to use any existing X11 application or library I Actively developed and maintained. I X11 license I http://www.x.org Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 326/526
  • 877.
    Kdrive: architecture FreeElectrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 327/526
  • 878.
    Kdrive: usage ICan be directly programmed using Xlib / XCB I Low-level graphic library I Probably doesn't make sense since DirectFB is a more lightweight solution for an API of roughly the same level (no widgets) I Or, usually used with a toolkit on top of it I Gtk I Qt I Enlightment Foundation Libraries I Others: Fltk, WxEmbedded, etc Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 328/526
  • 879.
    Wayland I Intendedto be a simpler replacement for X I Wayland is a protocol for a compositor to talk to its clients as well as a C library implementation of that protocol. I Weston: a minimal and fast reference implementation of a Wayland compositor, and is suitable for many embedded and mobile use cases. I Still experimental. Porting Gtk and Qt to Wayland is not complete yet, for example. I http://wayland.freedesktop.org/ Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 329/526
  • 880.
    Wayland: architecture FreeElectrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 330/526
  • 881.
    Embedded Linux systemdevelopment Graphical toolkits: High-level solutions Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 331/526
  • 882.
    Gtk I Thefamous toolkit, providing widget-based high-level APIs to develop graphical applications I Standard API in C, but bindings exist for various languages: C++, Python, etc. I Works on top of X.org. I No windowing system, a lightweight window manager needed to run several applications. Possible solution: Matchbox. I License: LGPL I http://www.gtk.org Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 332/526
  • 883.
    Gtk stack components I Glib, core infrastructure I Object-oriented infrastructure GObject I Event loop, threads, asynchronous queues, plug-ins, memory allocation, I/O channels, string utilities, timers, date and time, internationalization, simple XML parser, regular expressions I Data types: memory slices and chunks, linked lists, arrays, trees, hash tables, etc. I Pango, internationalization of text handling I ATK, accessibility toolkit I Cairo, vector graphics library I Gtk+, the widget library itself I The Gtk stack is a complete framework to develop applications Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 333/526
  • 884.
    Gtk examples (1) Openmoko phone interface Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 334/526
  • 885.
    Gtk examples (2) Maemo tablet / phone interface GTK is losing traction, however: Mer, the descendent of Maemo, is now implemented in EFL (see next slides). Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 335/526
  • 886.
    Qt (1) IThe other famous toolkit, providing widget-based high-level APIs to develop graphical applications I Implemented in C++ I the C++ library is required on the target system I standard API in C++, but with bindings for other languages I Works either on top of I Framebuer I X11 I DirectFB back-end integrated in version 4.4, which allows to take advantage of the acceleration provided by DirectFB drivers Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 336/526
  • 887.
    Qt (2) IQt is more than just a graphical toolkit, it also oers a complete development framework: data structures, threads, network, databases, XML, etc. I See our presentation Qt for non graphical applications presentation at ELCE 2011 (Thomas Petazzoni): http://j.mp/W4PK85 I Qt Embedded has an integrated windowing system, allowing several applications to share the same screen I Very well documented I Since version 4.5, available under the LGPL, allowing proprietary applications Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 337/526
  • 888.
    Qt's usage Qton the Dash Express navigation system Qt on the Net ix player by Roku Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 338/526
  • 889.
    Other less frequentsolutions I Enlightenment Foundation Libraries (EFL) I Very powerful. Supported by Samsung, Intel and Free.fr. I Work on top of X or Wayland. I http://www.enlightenment.org/p.php?p=about/efl Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 339/526
  • 890.
    Embedded Linux systemdevelopment Tools for the target device: Databases Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 340/526
  • 891.
    Lightweight database -SQLite http://www.sqlite.org I SQLite is a small C library that implements a self-contained, embeddable, lightweight, zero-con
  • 892.
    guration SQL database engine I The database engine of choice for embedded Linux systems I Can be used as a normal library I Can be directly embedded into a application, even a proprietary one since SQLite is released in the public domain Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 341/526
  • 893.
    Embedded Linux systemdevelopment Tools for the target device: Web browsers Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 342/526
  • 894.
    WebKit http://webkit.org/ IWeb browser engine. Application framework that can be used to develop web browsers. I License: portions in LGPL and others in BSD. Proprietary applications allowed. I Used by many web browsers: Safari, iPhone et Android default browsers ... Google Chrome now uses a fork of its WebCore component). Used by e-mail clients too to render HTML: http://trac.webkit.org/wiki/ Applications%20using%20WebKit I Multiple graphical back-ends: Qt4, GTK, EFL... I You could use it to create your custom browser. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 343/526
  • 895.
    Embedded Linux systemdevelopment Example of components used in real devices Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 344/526
  • 896.
    Industrial applications IIn many industrial applications, the system is only responsible for monitoring and control a device I Such a system is usually relatively simple in terms of components I Kernel I BusyBox I C library I Applications relying directly on the C library, sometimes using the real-time capabilities of the Linux kernel I Sometimes a Web server for remote control, or another server implementing a custom protocol Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 345/526
  • 897.
    Digital Photo Frame:requirements I Example taken from a conference of Matt Porter, Embedded Alley at ELC 2008 I Hardware: ARM SoC with DSP, audio, 800x600 LCD, MMC/SD, NAND, buttons, speakers I The photo frame must be able to I Display to the LCD I Detect SD card insertion, notify applications of the insertion so that applications can build a catalog of the pictures on the SD card I Modern 3D GUI with nice transitions I Navigation through buttons I Support audio playback (MP3, playlists, ID3 tag) I JPEG resizing and rotation Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 346/526
  • 898.
    Digital Photo Frame:components (1) I Base system I Components present in virtually all embedded Linux systems I The U-Boot bootloader I Linux Kernel I Drivers for SD/MMC, framebuer, sound, input devices I Busybox I Build system, in this case was OpenEmbedded I Components: u-boot, Linux, busybox Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 347/526
  • 899.
    Digital Photo Frame:components (2) I Event handling to detect SD card insertion I udev, that receives events from the kernel, creates device nodes, and sends events to hal I hal, which maintains a database of available devices and provides a dbus API I dbus to connect hal with the application. The application subscribes to hal event through dbus and gets noti
  • 900.
    ed when theyare triggered I Components: udev, hal, dbus Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 348/526
  • 901.
    Digital Photo Frame:components (3) I JPEG display I libjpeg to decode the pictures I jpegtran to resize and rotate them I FIM (Fbi Improved) for dithering I MP3 support I libmad for playing I libid3 for ID3 tags reading I libm3u to support playlists I Used vendor-provided components to leverage the DSP to play MP3 Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 349/526
  • 902.
    Digital Photo Frame:components (4) I 3D interface I Vincent, an open-source implementation of OpenGL ES I Clutter, higher-level API to develop 3D applications I Application itself I Manages media events I Uses the JPEG libraries to decode and render pictures I Receives Linux input events from buttons and draws OpenGL-based UI developed with Clutter I Manage a user-de
  • 903.
  • 904.
    guration I Playthe music with the MP3-related libraries I Display photo slideshow Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 350/526
  • 905.
    Embedded Linux systemdevelopment System building Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 351/526
  • 906.
    System building: goaland solutions I Goal I Integrate all the software components, both third-party and in-house, into a working root
  • 907.
    lesystem I Itinvolves the download, extraction, con
  • 908.
    guration, compilation andinstallation of all components, and possibly
  • 909.
    xing issues andadapting con
  • 910.
  • 911.
    les I Severalsolutions I Manually I System building tools I Distributions or ready-made
  • 912.
    lesystems Free Electrons- Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 352/526
  • 913.
    System building: manually I Manually building a target system involves downloading, con
  • 914.
    guring, compiling andinstalling all the components of the system. I All the libraries and dependencies must be con
  • 915.
    gured, compiled andinstalled in the right order. I Sometimes, the build system used by libraries or applications is not very cross-compile friendly, so some adaptations are necessary. I There is no infrastructure to reproduce the build from scratch, which might cause problems if one component needs to be changed, if somebody else takes over the project, etc. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 353/526
  • 916.
    System building: manually(2) I Manual system building is not recommended for production projects I However, using automated tools often requires the developer to dig into speci
  • 917.
    c issues IHaving a basic understanding of how a system can be built manually is therefore very useful to
  • 918.
    x issues encounteredwith automated tools I We will
  • 919.
    rst study manualsystem building, and during a practical lab, create a system using this method I Then, we will study the automated tools available, and use one of them during a lab Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 354/526
  • 920.
  • 921.
    le system needsat least I A traditional directory hierarchy, with /bin, /etc, /lib, /root, /usr/bin, /usr/lib, /usr/share, /usr/sbin, /var, /sbin I A set of basic utilities, providing at least the init program, a shell and other traditional Unix command line tools. This is usually provided by Busybox I The C library and the related libraries (thread, math, etc.) installed in /lib I A few con
  • 922.
  • 923.
    les, such as/etc/inittab, and initialization scripts in /etc/init.d I On top of this foundation common to most embedded Linux system, we can add third-party or in-house components Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 355/526
  • 924.
    Target and buildspaces I The system foundation, Busybox and C library, are the core of the target root
  • 925.
    lesystem I However,when building other components, one must distinguish two directories I The target space, which contains the target root
  • 926.
    lesystem, everything thatis needed for execution of the application I The build space, which will contain a lot more
  • 927.
    les 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 con
  • 928.
  • 929.
    les Free Electrons- Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 356/526
  • 930.
    Build systems IEach open-source component comes with a mechanism to con
  • 931.
    gure, compile andinstall it I A basic Makefile I Need to read the Makefile to understand how it works and how to tweak it for cross-compilation I A build system based on the Autotools I As this is the most common build system, we will study it in details I CMake, http://www.cmake.org/ I Newer and simpler than the autotools. Used by large projects such as KDE or Second Life I Scons, http://www.scons.org/ I Waf, http://code.google.com/p/waf/ I Other manual build systems Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 357/526
  • 932.
    Autotools and friends I A family of tools, which associated together form a complete and extensible build system I autoconf is used to handle the con
  • 933.
    guration of thesoftware package I automake is used to generate the Make
  • 934.
    les needed tobuild the software package I pkgcon
  • 935.
    g is usedto ease compilation against already installed shared libraries I libtool is used to handle the generation of shared libraries in a system-independent way I 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. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 358/526
  • 936.
    automake / autoconf/ autoheader Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 359/526
  • 937.
    automake / autoconf I Files written by the developer I configure.in describes the con
  • 938.
    guration options andthe checks done at con
  • 939.
    gure time IMakefile.am describes how the software should be built I The configure script and the Makefile.in
  • 940.
    les are generatedby autoconf and automake respectively. I They should never be modi
  • 941.
    ed directly IThey are usually shipped pre-generated in the software package, because there are several versions of autoconf and automake, and they are not completely compatible I The Makefile
  • 942.
  • 943.
    gure time, before compiling I They are never shipped in the software package. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 360/526
  • 944.
  • 945.
    guring and compiling:native case I The traditional steps to con
  • 946.
    gure and compilean autotools based package are I Con
  • 947.
    guration of thepackage ./configure I Compilation of the package make I Installation of the package make install I Additional arguments can be passed to the ./configure script to adjust the component con
  • 948.
    guration. I Onlythe make install needs to be done as root if the installation should take place system-wide Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 361/526
  • 949.
  • 950.
    guring and compiling:cross case (1) I For cross-compilation, things are a little bit more complicated. I At least some of the environment variables AR, AS, LD, NM, CC, GCC, CPP, CXX, STRIP, OBJCOPY must be de
  • 951.
    ned to pointto the proper cross-compilation tools. The host tuple is also by default used as pre
  • 952.
    x. I configurescript arguments: I --host: mandatory but a bit confusing. Corresponds to the target platform the code will run on. Example: --host=arm-linux I --build: build system. Automatically detected. I --target is only for tools generating code. I It is recommended to pass the --prefix argument. It de
  • 953.
    nes from whichlocation the software will run in the target environment. Usually, /usr is
  • 954.
    ne. Free Electrons- Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 362/526
  • 955.
  • 956.
    guring and compiling:cross case (2) I 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. I Making the distinction between the pre
  • 957.
    x (as passedwith --prefix at con
  • 958.
    gure time) andthe destination directory (as passed with DESTDIR at installation time) is very important. I 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 Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 363/526
  • 959.
    Installation (1) IThe autotools based software packages provide both a install and install-strip make targets, used to install the software, either stripped or unstripped. I For applications, the software is usually installed in prefix/bin, with con
  • 960.
  • 961.
    les in prefix/etc and data in prefix/share/application/ I The case of libraries is a little more complicated: I In prefix/lib, the library itself (a .so.version), a few symbolic links, and the libtool description
  • 962.
  • 963.
    le) I Thepkgcon
  • 964.
  • 965.
  • 966.
    les in prefix/include/ I Sometimes a libname-config program in prefix/bin I Documentation in prefix/share/man or prefix/share/doc/ Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 364/526
  • 967.
    Installation (2) Contentsof usr/lib after installation of libpng and zlib I libpng libtool description
  • 968.
    les ./lib/libpng12.la ./lib/libpng.la- libpng12.la I libpng static version ./lib/libpng12.a ./lib/libpng.a - libpng12.a I 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 I libpng pkg-con
  • 969.
  • 970.
    les ./lib/pkgconfig/libpng12.pc ./lib/pkgconfig/libpng.pc- libpng12.pc I 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 Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 365/526
  • 971.
    Installation in thebuild and target spaces I From all these
  • 972.
    les, everything exceptdocumentation is necessary to build an application that relies on libpng. I These
  • 973.
    les will gointo the build space I However, only the library .so binaries in prefix/lib and some symbolic links are needed to execute the application on the target. I Only these
  • 974.
    les will goin the target space I The build space must be kept in order to build other applications or recompile existing applications. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 366/526
  • 975.
  • 976.
    g I pkg-configis a tool that allows to query a small database to get information on how to compile programs that depend on libraries I The database is made of .pc
  • 977.
    les, installed bydefault in prefix/lib/pkgconfig/. I pkg-config is used by the configure script to get the library con
  • 978.
    gurations I Itcan also be used manually to compile an application: arm-linux-gcc -o test test.c $(pkg-config -- libs --cflags thelib) I By default, pkg-config looks in /usr/lib/pkgconfig for the *.pc
  • 979.
    les, and assumesthat the paths in these
  • 980.
    les are correct. I PKG_CONFIG_PATH allows to set another location for the *.pc
  • 981.
  • 982.
    x to thepaths mentioned in the .pc
  • 983.
    les. Free Electrons- Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 367/526
  • 984.
  • 985.
    nd the libraries I 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. I The
  • 986.
    rst thing todo 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 I The libtool
  • 987.
  • 988.
  • 989.
    ed because they include the absolute paths of the libraries: - libdir='/usr/lib' + libdir='/my/build/space/usr/lib' I The PKG_CONFIG_PATH environment variable must be set to the location of the .pc
  • 990.
    les and the PKG_CONFIG_SYSROOT_DIR variable must be set to the build space directory. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 368/526
  • 991.
    Practical lab -Manual cross-compiling I Manually cross-compiling applications and libraries I Learning about common techniques and issues. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 369/526
  • 992.
    System building tools:principle I Dierent tools are available to automate the process of building a target system, including the kernel, and sometimes the toolchain. I They automatically download, con
  • 993.
    gure, compile andinstall all the components in the right order, sometimes after applying patches to
  • 994.
    x cross-compiling issues. I They already contain a large number of packages, that should
  • 995.
    t your mainrequirements, and are easily extensible. I The build becomes reproducible, which allows to easily change the con
  • 996.
    guration of somecomponents, upgrade them,
  • 997.
    x bugs, etc. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 370/526
  • 998.
    Available system buildingtools Large choice of tools I Buildroot, developed by the community http://www.buildroot.net I PTXdist, developed by Pengutronix http://pengutronix.de/software/ptxdist/ I OpenWRT, originally a fork of Buildroot for wireless routers, now a more generic project http://www.openwrt.org I LTIB. Good support for Freescale boards, but small community http://ltib.org/ I OpenEmbedded, more exible but also far more complicated http://www.openembedded.org, its industrialized version Yocto and vendor-speci
  • 999.
    c derivatives suchas Arago I Vendor speci
  • 1000.
    c tools (siliconvendor or embedded Linux vendor) Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 371/526
  • 1001.
    Buildroot (1) IAllows to build a toolchain, a root
  • 1002.
    lesystem image withmany applications and libraries, a bootloader and a kernel image I Or any combination of the previous items I Supports building uClibc, glibc, eglibc and musl toolchains, either built by Buildroot, or external I Over 1200+ applications or libraries integrated, from basic utilities to more elaborate software stacks: X.org, GStreamer, Qt, Gtk, WebKit, Python, PHP, etc. I Good for small to medium embedded systems, with a
  • 1003.
    xed set offeatures I No support for generating packages (.deb or .ipk) I Needs complete rebuild for most con
  • 1004.
    guration changes. IActive community, releases published every 3 months. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 372/526
  • 1005.
  • 1006.
    guration takes placethrough a *config interface similar to the kernel make menuconfig I Allows to de
  • 1007.
  • 1008.
    c CPU IToolchain con
  • 1009.
    guration I Setof applications and libraries to integrate I Filesystem images to generate I Kernel and bootloader con
  • 1010.
    guration I Buildby just running make Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 373/526
  • 1011.
    Buildroot: adding anew package (1) I A package allows to integrate a user application or library to Buildroot I Each package has its own directory (such as package/gqview). This directory contains: I A Config.in
  • 1012.
  • 1013.
    guration options forthe package. At least one is needed to enable the package. This
  • 1014.
    le must besourced from package/Config.in I A gqview.mk
  • 1015.
    le (mandatory), describinghow the package is built. I Patches (optional). Each
  • 1016.
    le of theform gqview-*.patch will be applied as a patch. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 374/526
  • 1017.
    Buildroot: adding anew package (2) I For a simple package with a single con
  • 1018.
    guration option to enable/disable it, the Config.in
  • 1019.
    le 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 I It must be sourced from package/Config.in: source package/gqview/Config.in Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 375/526
  • 1020.
    Buildroot: adding newpackage (3) I Create the gqview.mk
  • 1021.
    le to describethe build steps 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 $(eval $(autotools-package)) I The package directory and the pre
  • 1022.
    x of allvariables must be identical to the sux of the main con
  • 1023.
    guration option BR2_PACKAGE_GQVIEW I 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. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 376/526
  • 1024.
    OpenEmbedded / Yocto I The most versatile and powerful embedded Linux build system I A collection of recipes (.bb
  • 1025.
    les) I Atool that processes the recipes: bitbake I Integrates 2000+ application and libraries, is highly con
  • 1026.
    gurable, can generatebinary packages to make the system customizable, supports multiple versions/variants of the same package, no need for full rebuild when the con
  • 1027.
  • 1028.
    guration takes placeby editing various con
  • 1029.
  • 1030.
    les I Goodfor larger embedded Linux systems, or people looking for more con
  • 1031.
    gurability and extensibility I Drawbacks: very steep learning curve, very long
  • 1032.
    rst build. FreeElectrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 377/526
  • 1033.
    Distributions - Debian Debian GNU/Linux, http://www.debian.org I Provides the easiest environment for quickly building prototypes and developing applications. Countless runtime and development packages available. I But brobably too costly to maintain and unnecessarily big for production systems. I Available on ARM, MIPS and PowerPC architectures I Software is compiled natively by default. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 378/526
  • 1034.
    Distributions - Others Fedora I http://fedoraproject.org/wiki/ Architectures/ARM I Supported on various recent ARM boards (such as Beaglebone Black). Pidora supports Raspberry Pi too. I Supports QEMU emulated ARM boards too (Versatile Express board) I Shipping the same version as for desktops! Ubuntu I Had some releases for ARM mobile multimedia devices, but stopped at version 12.04. Now focusing on ARM servers only. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 379/526
  • 1035.
  • 1036.
    c types ofdevices I Android: http://www.android.com/ Google's distribution for phones and tablet PCs. Except the Linux kernel, very dierent user space than other Linux distributions. Very successful, lots of applications available (many proprietary). I Angstrom: http://www.angstrom-distribution.org/ Targets PDAs and webpads (Siemens Simpad...) Binaries available for arm little endian. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 380/526
  • 1037.
    Application frameworks Notreal distributions you can download. Instead, they implement middleware running on top of the Linux kernel and allowing to develop applications. I Mer: http://merproject.org/ Fork from the Meego project. Targeting mobile devices. Supports x86, ARM and MIPS. See http://en.wikipedia.org/wiki/Mer_ (software_distribution) I Tizen: https://www.tizen.org/ Targeting smartphones, tablets, netbooks, smart TVs and In Vehicle Infotainment devices. Supported by big phone manufacturers and operators HTML5 base application framework. Likely to compete against Android! See http://en.wikipedia.org/wiki/Tizen Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 381/526
  • 1038.
    System emulator: QEMU http://qemu.org Fast processor emulator I Useful to develop root
  • 1039.
    lesystems and applicationswhen the hardware is not available yet (binary compatible development boards are also possible). I Emulates the processor and various peripherals (System emulation): x86, ppc, arm, sparc, mips, m68k... I Many dierent ARM boards supported: qemu-system-arm -M ? I QEMU also allows to just run binaries for other architectures (User emulation). Used inside some building tools. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 382/526
  • 1040.
    Practical lab -Buildroot I Rebuild the same system, this time with Buildroot. I See how easier it gets! Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 383/526
  • 1041.
    Embedded Linux systemdevelopment Commercial embedded Linux solutions Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 384/526
  • 1042.
    Commercial embedded Linuxsolutions Caution: commercial doesn't mean proprietary! I Vendors play fair with the GPL and do make their source code available to their users, and most of the time, eventually to the community. I As long as they distribute the sources to their users, the GPL doesn't require vendors to share their sources with any third party. I Graphical toolkits developed by the vendors are usually proprietary, trying to make it easier to create and embedded Linux systems. I Major players: Wind River, Montavista, TimeSys Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 385/526
  • 1043.
    Commercial solution strengths I Technical advantages I Well tested and supported kernel and tool versions I Often supporting patches not supported by the mainline kernel yet (example: real-time patches) I Complete development tool sets: kernels, toolchains, utilities, binaries for impressive lists of target platforms I Integrated utilities for automatic kernel image, initramfs and
  • 1044.
    lesystem generation. IGraphical developments tools I Development tools available on multiple platforms: GNU / Linux, Solaris, Windows... I Support services I Useful if you don't have your own support resources I Long term support commitment, even for versions considered as obsolete by the community, but not by your users! Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 386/526
  • 1045.
    Commercial or communitysolutions? Commercial distributions and tool sets I Best if you don't have your own support resources and have a sucient budget I Help focusing on your primary job: making an embedded device. I You can even subcontract driver development to the vendor Community distributions and tools I Best if you are on a tight budget I Best if you are willing to build your own embedded Linux expertise, investigate issues by yourselves, and train your own support resources. In any case, your products are based on Free Software! Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 387/526
  • 1046.
    Embedded Linux applicationdevelopment Embedded Linux application development Free Electrons ©Copyright 2004-2014, Free Electrons. Creative Commons BY-SA 3.0 license. Corrections, suggestions, contributions and translations are welcome! Embedded Linux Developers Free Electrons Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 388/526
  • 1047.
    Contents I Applicationdevelopment I Developing applications on embedded Linux I Building your applications I Source management I Integrated development environments (IDEs) I Version control systems I Debugging and analysis tools I Debuggers I Memory checkers I System analysis I Development environments I Developing on Windows Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 389/526
  • 1048.
    Embedded Linux applicationdevelopment Developing applications on embedded Linux Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 390/526
  • 1049.
    Application development IAn embedded Linux system is just a normal Linux system, with usually a smaller selection of components I In terms of application development, developing on embedded Linux is exactly the same as developing on a desktop Linux system I All existing skills can be re-used, without any particular adaptation I All existing libraries, either third-party or in-house, can be integrated into the embedded Linux system I Taking into account, of course, the limitation of the embedded systems in terms of performance, storage and memory Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 391/526
  • 1050.
    Programming language IThe default programming language for system-level application in Linux is usually C I The C library is already present on your system, nothing to add I C++ can be used for larger applications I The C++ library must be added to the system I Some libraries, including Qt, are developed in C++ so they need the C++ library on the system anyway I Scripting languages can also be useful for quick application development, web applications or scripts I But they require an interpreter on the embedded system and have usually higher memory consumption and slightly lower performance I Languages: Python, Perl, Lua, Ada, Fortran, etc. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 392/526
  • 1051.
    C library orhigher-level libraries? I For many applications, the C library already provides a relatively large set of features I
  • 1052.
    le and deviceI/O, networking, threads and synchronization, inter-process communication I Thoroughly described in the glibc manual, or in any Linux system programming book I However, the API carries a lot of history and is not necessarily easy to grasp for new comers I Therefore, using a higher level framework, such as Qt or the Gtk stack, might be a good idea I These frameworks are not only graphical libraries, their core is separate from the graphical part I But of course, these libraries have some memory and storage footprint, in the order of a few megabytes Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 393/526
  • 1053.
    Building your applications I For simple applications that do not need to be really portable or provide compile-time con
  • 1054.
  • 1055.
    le will besucient I For more complicated applications, or if you want to be able to run your application on a desktop Linux PC and on the target device, using a build system is recommended I autotools is ancient, complicated but very widely used. I We recommend to invest in CMake instead: modern, simpler, smaller but growing user base. I The QT library is a special case, since it comes with its own build system for applications, called qmake. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 394/526
  • 1056.
  • 1057.
    le (1) ICase of an application that only uses the C library, contains two source
  • 1058.
    les and generatesa single binary CROSS_COMPILE?=arm-linux- CC=$(CROSS_COMPILE)gcc OBJS=foo.o bar.o all: foobar foobar: $(OBJS) $(CC) -o $@ $^ clean: $(RM) -f foobar $(OBJS) Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 395/526
  • 1059.
  • 1060.
    le (2) ICase 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) $(CC) -o $@ $^ $(LDFLAGS) clean: $(RM) -f foobar $(OBJS) Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 396/526
  • 1061.
    Embedded Linux applicationdevelopment Integrated Development Environments (IDE) Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 397/526
  • 1062.
    KDevelop http://kdevelop.org IA full featured IDE! I License: GPL I Supports many languages: Ada, C, C++, Database, Java, Perl, PHP, Python, Ruby, Shell I Supports many kinds of projects: KDE, but also GTK, Gnome, kernel drivers, embedded (Opie)... I Many features: editor, syntax highlighting, code completion, compiler interface, debugger interface,
  • 1063.
    le manager, classbrowser... Nice overview: http://en.wikipedia.org/wiki/Kdevelop Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 398/526
  • 1064.
    KDevelop screenshot Rubydebugger Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 399/526
  • 1065.
    Eclipse (1) http://www.eclipse.org/ I An extensible, plug-in based software development kit, typically used for creating IDEs. I Supported by the Eclipse foundation, a non-pro
  • 1066.
    t consortium ofmajor software industry vendors (IBM, Intel, Borland, Nokia, Wind River, Zend, Computer Associates...). I Free Software license (Eclipse Public License). Incompatible with the GPL. I Supported platforms: GNU/Linux, Unix, Windows Extremely popular: created a lot of attraction. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 400/526
  • 1067.
    Eclipse (2) IEclipse is actually a platform composed of many projects: http://www.eclipse.org/projects/ I Some projects are dedicated to integrating into Eclipse features useful for embedded developers (cross-compilation, remote development, remote debugging, etc.) I The platform is used by major embedded Linux software vendors for their (proprietary) system development kits: MontaVista DevRocket, TimeSys TimeStorm, Wind River Workbench, Sysgo ELinOS. I Used by free software build systems and development environments too, such as Yocto and Buildroot. Eclipse is a huge project. It would require an entire training session! Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 401/526
  • 1068.
    Other popular solutions I Many embedded Linux developers simply use Vim or Emacs. They can integrate with debuggers, source code browsers such as cscope, oer syntax highlighting and more. I Geany is an easy-to-use graphical code editor. I CodeBlocks is also quite popular, since it's also available on the Windows platform. All these editors are available in most Linux distributions, simply install them and try them out! Vim Emacs Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 402/526
  • 1069.
    Embedded Linux applicationdevelopment Version control systems Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 403/526
  • 1070.
    Version control systems Real projects can't do without them I Allow multiple developers to contribute on the same project. Each developer can see the latest changes from the others, or choose to stick with older versions of some components. I Allow to keep track of changes, and revert them if needed. I Allow developers to have their own development branch (branching) I Supposed to help developers resolving con icts with dierent branches (merging) Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 404/526
  • 1071.
    Traditional version controlsystems Rely on a central repository. The most popular open-source ones: I CVS - Concurrent Versions System I Still quite popular in enterprise contexts. Almost no longer exists in the open-source community. I Should no longer be used for new projects I http: //en.wikipedia.org/wiki/Concurrent_Versions_System I Subversion I Created as a replacement of CVS, removing many of its limitations. I Commits on several
  • 1072.
    les, proper renamingsupport, better performance, etc. I The user interface is very similar to CVS I http://en.wikipedia.org/wiki/Subversion_(software) Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 405/526
  • 1073.
    Distributed source controlsystems (1) No longer have a central repository I More adapted to the way the Free Software community develops software and organizes I Allows each developer to have a full local history of the project, to create local branches. Makes each developer's work easier. I People get working copies from other people's working copies, and exchange changes between themselves. Branching and merging is made easier. I Make it easier for new developers to join, making their own experiments without having to apply for repository access. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 406/526
  • 1074.
    Distributed source controlsystems (2) I Git I Initially designed and developed by Linus Torvalds for Linux kernel development I Extremely popular in the community, and used by more and more projects (kernel, U-Boot, Barebox, uClibc, GNOME, X.org, etc.) I Outstanding performance, in particular in big projects I http://en.wikipedia.org/wiki/Git_(software) I Mercurial I Another system, created with the same goals as Git. I Used by some big projects too I http://en.wikipedia.org/wiki/Mercurial http://en.wikipedia.org/wiki/Version_control_systems# Distributed_revision_control Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 407/526
  • 1075.
    Embedded Linux applicationdevelopment Debuggers Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 408/526
  • 1076.
    GDB The GNUProject Debugger http://www.gnu.org/software/gdb/ I The debugger on GNU/Linux, available for most embedded architectures. I Supported languages: C, C++, Pascal, Objective-C, Fortran, Ada... I Console interface (useful for remote debugging). I Graphical front-ends available. I Can be used to control the execution of a program, set breakpoints or change internal variables. You can also use it to see what a program was doing when it crashed (by loading its memory image, dumped into a core
  • 1077.
    le). See alsohttp://en.wikipedia.org/wiki/Gdb Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 409/526
  • 1078.
    GDB crash course I A few useful GDB commands I break foobar puts a breakpoint at the entry of function foobar() I break foobar.c:42 puts a breakpoint in foobar.c, line 42 I 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 I continue continue the execution I next continue to the next line, stepping over function calls I step continue to the next line, entering into subfunctions Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 410/526
  • 1079.
    GDB graphical front-ends I DDD - Data Display Debugger http://www.gnu.org/software/ddd/ A popular graphical front-end, with advanced data plotting capabilities. I GDB/Insight http://sourceware.org/insight/ From the GDB maintainers. I KDbg http://www.kdbg.org/ Another front-end, for the K Display Environment. I Integration with other IDEs: Eclipse, Emacs, KDevelop, etc. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 411/526
  • 1080.
    Embedded Linux applicationdevelopment Remote debugging Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 412/526
  • 1081.
    Remote debugging IIn a non-embedded environment, debugging takes place using gdb or one of its front-ends. I gdb has direct access to the binary and libraries compiled with debugging symbols. I However, in an embedded context, the target platform environment is often too limited to allow direct debugging with gdb (2.4 MB on x86). I Remote debugging is preferred I gdb is used on the development workstation, oering all its features. I gdbserver is used on the target system (only 100 KB on arm). Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 413/526
  • 1082.
    Remote debugging: architecture Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 414/526
  • 1083.
    Remote debugging: usage I On the target, run a program through gdbserver. Program execution will not start immediately. gdbserver localhost:port executable args gdbserver /dev/ttyS0 executable args I Otherwise, attach gdbserver to an already running program: gdbserver --attach localhost:port pid I Then, on the host, run the ARCH-linux-gdb program, and use the following gdb commands: I To connect to the target: gdb target remote ip-addr:port (networking) gdb target remote /dev/ttyS0 (serial link) I To tell gdb where shared libraries are: gdb set sysroot library-path (without lib/) Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 415/526
  • 1084.
    Post mortem analysis I 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 I Fortunately, Linux can generate a core
  • 1085.
    le that containsthe image of the application memory at the moment of the crash, and gdb can use this core
  • 1086.
    le to letus analyze the state of the crashed application I On the target I Use ulimit -c unlimited to enable the generation of a core
  • 1087.
    le when acrash occurs I On the host I After the crash, transfer the core
  • 1088.
    le from thetarget to the host, and run ARCH-linux-gdb -c core-file application-binary Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 416/526
  • 1089.
    Embedded Linux applicationdevelopment Memory checkers Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 417/526
  • 1090.
    DUMA Detect UnintendedMemory Access http://duma.sourceforge.net/ I Fork and replacement for Electric Fence I Stops your program on the exact instruction that overruns or underruns a malloc() memory buer. I GDB will then display the source-code line that causes the bug. I Works by using the virtual-memory hardware to create a red-zone at the border of each buer - touch that, and your program stops. I Works on any platform supported by Linux, whatever the CPU (provided virtual memory support is available). Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 418/526
  • 1091.
    Valgrind (1) http://valgrind.org/ I GNU GPL Software suite for debugging and pro
  • 1092.
    ling programs. ISupported platforms: Linux on x86, x86 64, ppc32, ppc64 and arm (armv7 only: Cortex A8, A9 and A5) I Can detect many memory management and threading bugs. I Pro
  • 1093.
    ler: provides informationhelpful to speed up your program and reduce its memory usage. I The most popular tool for this usage. Even used by projects with hundreds of programmers. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 419/526
  • 1094.
    Valgrind (2) ICan be used to run any program, without the need to recompile it. I Example usage Valgrind --leak-check=yes ls -la I Works by adding its own instrumentation to your code and then running in on its own virtual cpu core. Signi
  • 1095.
    cantly slows downexecution, but still
  • 1096.
    ne for testing! I More details on http://valgrind.org/info/ and http://valgrind.org/docs/manual/ coregrind_core.html#howworks Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 420/526
  • 1097.
    Embedded Linux applicationdevelopment System analysis Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 421/526
  • 1098.
    strace System calltracer http://sourceforge.net/projects/strace/ I Available on all GNU/Linux systems Can be built by your cross-compiling toolchain generator. I Even easier: drop a ready-made static binary for your architecture, just when you need it. See http://git.free-electrons.com/users/michael-opdenacker/ static-binaries/tree/strace I Allows to see what any of your processes is doing: accessing
  • 1099.
    les, allocating memory... Often sucient to
  • 1100.
    nd simple bugs. I Usage: strace command (starting a new process) strace -p pid (tracing an existing process) See man strace for details. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 422/526
  • 1101.
    strace example output strace cat Makefile execve(/bin/cat, [cat, Makefile], [/* 38 vars */]) = 0 brk(0) = 0x98b4000 access(/etc/ld.so.nohwcap, F_OK) = -1 ENOENT (No such file or directory) mmap2(NULL, 8192, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_ANONYMOUS, -1, 0) = 0xb7f85000 access(/etc/ld.so.preload, R_OK) = -1 ENOENT (No such file or directory) open(/etc/ld.so.cache, O_RDONLY) = 3 fstat64(3, {st_mode=S_IFREG|0644, st_size=111585, ...}) = 0 mmap2(NULL, 111585, PROT_READ, MAP_PRIVATE, 3, 0) = 0xb7f69000 close(3) = 0 access(/etc/ld.so.nohwcap, F_OK) = -1 ENOENT (No such file or directory) open(/lib/tls/i686/cmov/libc.so.6, O_RDONLY) = 3 read(3, 177ELF11100000000030301000320h10004000344..., 512) = 512 fstat64(3, {st_mode=S_IFREG|0755, st_size=1442180, ...}) = 0 mmap2(NULL, 1451632, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0xb7e06000 mprotect(0xb7f62000, 4096, PROT_NONE) = 0 mmap2(0xb7f63000, 12288, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x15c) = 0xb7f63000 mmap2(0xb7f66000, 9840, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0xb7f66000 close(3) = 0 Hint: follow the open
  • 1102.
    le descriptors returnedby open(). This tells you what
  • 1103.
    les system callsare run on. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 423/526
  • 1104.
    ltrace A toolto trace library calls used by a program and all the signals it receives I Very useful complement to strace, which shows only system calls. I Of course, works even if you don't have the sources I Allows to
  • 1105.
    lter library callswith regular expressions, or just by a list of function names. I Manual page: http://linux.die.net/man/1/ltrace See http://en.wikipedia.org/wiki/Ltrace for details Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 424/526
  • 1106.
    ltrace example output ltrace nedit index.html sscanf(0x8274af1, 0x8132618, 0x8248640, 0xbfaadfe8, 0) = 1 sprintf(const 0, const %d, 0) = 7 strcmp(startScan, const 0) = 1 strcmp(ScanDistance, const 0) = -1 strcmp(const 200, const 0) = 1 strcmp($list_dialog_button, const 0) = -1 strcmp($shell_cmd_status, const 0) = -1 strcmp($read_status, const 0) = -1 strcmp($search_end, const 0) = -1 strcmp($string_dialog_button, const 0) = -1 strcmp($rangeset_list, const 0) = -1 strcmp($calltip_ID, const 0) = -1 Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 425/526
  • 1107.
    ltrace summary Examplesummary 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 Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 426/526
  • 1108.
  • 1109.
  • 1110.
    ling tool ICan collect statistics like the top users of the CPU. I Works without having the sources. I Requires a kernel patch to access all features, but is already available in a standard kernel. I Requires more investigation to see how it works. I Ubuntu/Debian packages: oprofile, oprofile-gui Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 427/526
  • 1111.
    Callgrind / KCachegrind I Cachegrind / Callgrind: part of the Valgrind tool suite Collects function call statistics and call graphs. Useful to know in which functions most time is spent. I KCachegrind: http://kcachegrind.sourceforge.net/ An amazing visualizer for Cachegrind / Callgrind data. I KCachegrind can also import data from other pro
  • 1112.
  • 1113.
  • 1114.
    ling output fromPython, Perl and PHP. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 428/526
  • 1115.
    KCachegrind screenshot FreeElectrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 429/526
  • 1116.
    Practical lab -App. development and debugging Application development I Compile your own application with the ncurses library Remote debugging I Set up remote debugging tools on the target: strace, ltrace and gdbserver. I Debug a simple application running on the target using remote debugging Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 430/526
  • 1117.
    Embedded Linux applicationdevelopment Developing on Windows Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 431/526
  • 1118.
    Developing on Windows!? Using a GNU/Linux workstation is the easiest way to create software for GNU/Linux or embedded Linux I You use the same tools and environment as all community developers do. Much fewer issues you are the only one to face. I You get familiar with the system. Essential for understanding issues. However, some developers have no choice: Windows is the only desktop OS allowed in their company. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 432/526
  • 1119.
    Cygwin http://cygwin.com/ Linux(POSIX)-like environment for Windows I 2 components: Linux API emulation layer: cygwin1.dll A collection of tools originally found in GNU/Linux I Allows to compile and run many GNU/Linux programs on Windows: shells, compiler, http servers, X Window, GTK... I Very easy to install. Can choose which tools to download and install. I For embedded Linux system developers: makes it possible to use GNU toolchains (compiled for Windows) required to build Linux binaries (kernel, libraries or applications). Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 433/526
  • 1120.
    Cygwin limitations Cygwinis not a complete substitute for a real GNU/Linux system. I Almost all developers work on GNU/Linux or on another Unix platform (typically BSD). Don't expect them to test that their tools build on Windows with Cygwin. I The number of Cygwin users is quite small. You may be the
  • 1121.
    rst to faceor report building issues on this platform for a given compiler or tool version. I Cygwin is very slow. So, the best solution is to run Linux inside Windows! Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 434/526
  • 1122.
    VMware http://en.wikipedia.org/wiki/VMware ILicense: proprietary I Can run a GNU/Linux PC from Windows, almost at the host speed. I VMware Player is now available free of charge. Many Free Software system images available for download. The most popular solution in the corporate world. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 435/526
  • 1123.
    VirtualBox http://virtualbox.org fromOracle I PC emulation solution available on both Windows and GNU/Linux I 2 licenses: I Proprietary: free of cost for personal use and evaluation. Binaries available for Windows. Full features. I Open Source Edition (OSE): GPL license. Most features (except in particular USB support). No binaries released for Windows so far (but possible). I Based on QEMU's core engine. Performance similar to that of VMware. See http://en.wikipedia.org/wiki/VirtualBox Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 436/526
  • 1124.
    Real-time in embeddedLinux systems Real-time in embedded Linux systems Free Electrons ©Copyright 2004-2014, Free Electrons. Creative Commons BY-SA 3.0 license. Corrections, suggestions, contributions and translations are welcome! Embedded Linux Developers Free Electrons Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 437/526
  • 1125.
    Real-time in embeddedLinux systems Introduction Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 438/526
  • 1126.
    Embedded Linux andreal time I Due to its advantages, Linux and open-source software are more and more commonly used in embedded applications I However, some applications also have real-time constraints I They, at the same time, want to I Get all the nice advantages of Linux: hardware support, components re-use, low cost, etc. I Get their real-time constraints met Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 439/526
  • 1127.
    Embedded Linux andreal time I Linux is an operating system part of the large Unix family I It was originally designed as a time-sharing system I The main goal was to get the best throughput from the available hardware, by making the best possible usage of resources (CPU, memory, I/O) I Time determinism was not taken into account I On the opposite, real-time constraints imply time determinism, even at the expense of lower global throughput I Best throughput and time determinism are contradictory requirements Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 440/526
  • 1128.
    Linux and real-timeapproaches (1) I Over time, two major approaches have been taken to bring real-time requirements into Linux I Approach 1 I Improve the Linux kernel itself so that it matches real-time requirements, by providing bounded latencies, real-time APIs, etc. I Approach taken by the mainline Linux kernel and the PREEMPT RT project. I Approach 2 I Add a layer below the Linux kernel that will handle all the real-time requirements, so that the behaviour of Linux doesn't aect real-time tasks. I Approach taken by RTLinux, RTAI and Xenomai Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 441/526
  • 1129.
    Linux and real-timeapproaches (2) An alternative approach is to use speci
  • 1130.
    c hardware torun real-time work on: I Dedicating a CPU core to a real-time OS or to a real-time application, using some kind of hypervizor. I Running real-time work on an FPGA I Running real-time work on a dedicated microcontroller. For example, the TI AM335x CPU (used in the Beaglebone Black) has a Programmable Real-Time Unit and Industrial Communication Subsystem (PRU-ICSS), which can be used for real-time processing. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 442/526
  • 1131.
    Real-time in embeddedLinux systems Improving the main Linux kernel with PREEMPT RT Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 443/526
  • 1132.
    Understanding latency IWhen developing real-time applications with a system such as Linux, the typical scenario is the following I An event from the physical world happens and gets noti
  • 1133.
    ed to theCPU by means of an interrupt I The interrupt handler recognizes and handles the event, and then wake-up the user space task that will react to this event I Some time later, the user space task will run and be able to react to the physical world event I Real-time is about providing guaranteed worst case latencies for this reaction time, called latency Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 444/526
  • 1134.
    Linux kernel latencycomponents kernel latency = interrupt latency + handler duration + scheduler latency + scheduler duration Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 445/526
  • 1135.
    Interrupt latency FreeElectrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 446/526
  • 1136.
    Sources of interruptlatency I One of the concurrency prevention mechanism used in the kernel is the spinlock I 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 I 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 I The duration of these critical sections is unbounded I Other possible source: shared interrupts Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 447/526
  • 1137.
    Interrupt handler duration Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 448/526
  • 1138.
    Interrupt handler implementation I In Linux, many interrupt handlers are split in two parts I A top-half, started by the CPU as soon as interrupts are enabled. It runs with the interrupt line disabled and is supposed to complete as quickly as possible. I A bottom-half, scheduled by the top-half, which starts after all pending top-halves have completed their execution. I Therefore, for real-time critical interrupts, bottom-halves shouldn't be used: their execution is delayed by all other interrupts in the system. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 449/526
  • 1139.
    Scheduler latency FreeElectrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 450/526
  • 1140.
    Understanding preemption (1) I The Linux kernel is a preemptive operating system I When a task runs in user space mode and gets interrupted by an interruption, if the interrupt handler wakes up another task, this task can be scheduled as soon as we return from the interrupt handler. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 451/526
  • 1141.
    Understanding preemption (2) I However, when the interrupt comes while the task is executing a system call, this system call has to
  • 1142.
    nish before anothertask can be scheduled. I By default, the Linux kernel does not do kernel preemption. I This means that the time before which the scheduler will be called to schedule another task is unbounded. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 452/526
  • 1143.
    Scheduler duration FreeElectrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 453/526
  • 1144.
    Other non-deterministic mechanisms I Outside of the critical path detailed previously, other non-deterministic mechanisms of Linux can aect the execution time of real-time tasks I Linux is highly based on virtual memory, as provided by an MMU, so that memory is allocated on demand. Whenever an application accesses code or data for the
  • 1145.
    rst time, itis loaded on demand, which can creates huge delays. I Many C library services or kernel services are not designed with real-time constraints in mind. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 454/526
  • 1146.
    Priority inversion Aprocess with a low priority might hold a lock needed by a higher priority process, eectively reducing the priority of this process. Things can be even worse if a middle priority process uses the CPU. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 455/526
  • 1147.
    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. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 456/526
  • 1148.
    The PREEMPT RTproject I Long-term project lead by Linux kernel developers Ingo Molnar, Thomas Gleixner and Steven Rostedt I https://rt.wiki.kernel.org I The goal is to gradually improve the Linux kernel regarding real-time requirements and to get these improvements merged into the mainline kernel I PREEMPT RT development works very closely with the mainline development I Many of the improvements designed, developed and debugged inside PREEMPT RT over the years are now part of the mainline Linux kernel I The project is a long-term branch of the Linux kernel that ultimately should disappear as everything will have been merged Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 457/526
  • 1149.
    Improvements in themainline kernel I Coming from the PREEMPT RT project I Since the beginning of 2.6 I O(1) scheduler I Kernel preemption I Better POSIX real-time API support I Since 2.6.18 I Priority inheritance support for mutexes I Since 2.6.21 I High-resolution timers I Since 2.6.30 I Threaded interrupts I Since 2.6.33 I Spinlock annotations Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 458/526
  • 1150.
    New preemption optionsin Linux 2.6 2 new preemption models oered by standard Linux 2.6: Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 459/526
  • 1151.
    1st option: noforced preemption CONFIG_PREEMPT_NONE Kernel code (interrupts, exceptions, system calls) never preempted. Default behavior in standard kernels. I Best for systems making intense computations, on which overall throughput is key. I Best to reduce task switching to maximize CPU and cache usage (by reducing context switching). I Still bene
  • 1152.
    ts from someLinux 2.6 improvements: O(1) scheduler, increased multiprocessor safety (work on RT preemption was useful to identify hard to
  • 1153.
    nd SMP bugs). I Can also bene
  • 1154.
    t from alower timer frequency (100 Hz instead of 250 or 1000). Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 460/526
  • 1155.
    2nd option: voluntarykernel preemption CONFIG_PREEMPT_VOLUNTARY Kernel code can preempt itself I Typically for desktop systems, for quicker application reaction to user input. I Adds explicit rescheduling points throughout kernel code. I Minor impact on throughput. I Used in: Ubuntu Desktop 13.04, Ubuntu Server 12.04 Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 461/526
  • 1156.
    3rd option: preemptiblekernel 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. I 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. I Typically for desktop or embedded systems with latency requirements in the milliseconds range. I Still a relatively minor impact on throughput. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 462/526
  • 1157.
    Priority inheritance IOne classical solution to the priority inversion problem is called priority inheritance I The idea is that when a task of a low priority holds a lock requested by a higher priority task, the priority of the
  • 1158.
    rst task getstemporarily raised to the priority of the second task: it has inherited its priority. I In Linux, since 2.6.18, mutexes support priority inheritance I In user space, priority inheritance must be explicitly enabled on a per-mutex basis. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 463/526
  • 1159.
    High resolution timers I The resolution of the timers used to be bound to the resolution of the regular system tick I Usually 100 Hz or 250 Hz, depending on the architecture and the con
  • 1160.
    guration I Aresolution of only 10 ms or 4 ms. I Increasing the regular system tick frequency is not an option as it would consume too many resources I The high-resolution timers infrastructure, merged in 2.6.21, allows to use the available hardware timers to program interrupts at the right moment. I Hardware timers are multiplexed, so that a single hardware timer is sucient to handle a large number of software-programmed timers. I Usable directly from user space using the usual timer APIs Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 464/526
  • 1161.
    Threaded interrupts ITo solve the interrupt inversion problem, PREEMPT RT has introduced the concept of threaded interrupts I The interrupt handlers run in normal kernel threads, so that the priorities of the dierent interrupt handlers can be con
  • 1162.
    gured I Thereal interrupt handler, as executed by the CPU, is only in charge of masking the interrupt and waking-up the corresponding thread I The idea of threaded interrupts also allows to use sleeping spinlocks (see later) I Merged since 2.6.30, the conversion of interrupt handlers to threaded interrupts is not automatic: drivers must be modi
  • 1163.
    ed I InPREEMPT RT, all interrupt handlers are switched to threaded interrupts Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 465/526
  • 1164.
    The future ofthe PREEMPT RT patchset Conclusion of the Realtime Linux minisummit (Nov. 2013): I The PREEMPT RT project will be done in 2014, one way or another. The
  • 1165.
    rst way wouldbe to get most of the rest of the code upstream, but that will require more of an eort, from a wider group than is currently involved. The alternative is to decide that the 95% of the realtime work already upstream is good enough and to drop further eorts. I Main issue: lack of companies devoting enough engineering resources to complete the job. I See http://lwn.net/Articles/572740/ Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 466/526
  • 1166.
    Real-time in embeddedLinux systems PREEMPT RT speci
  • 1167.
    cs Free Electrons- Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 467/526
  • 1168.
    CONFIG PREEMPT RT(1) I The PREEMPT RT patch adds a new level of preemption, called CONFIG_PREEMPT_RT I This level of preemption replaces all kernel spinlocks by mutexes (or so-called sleeping spinlocks) I 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. I Works well with threaded interrupts, since threads can block, while usual interrupt handlers could not. I Some core, carefully controlled, kernel spinlocks remain as normal spinlocks. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 468/526
  • 1169.
    CONFIG PREEMPT RT(2) I With CONFIG_PREEMPT_RT, virtually all kernel code becomes preemptible I An interrupt can occur at any time, when returning from the interrupt handler, the woken up process can start immediately. I This is the last big part of PREEMPT RT that isn't fully in the mainline kernel yet I Part of it has been merged in 2.6.33: the spinlock annotations. The spinlocks that must remain as spinning spinlocks are now dierentiated from spinlocks that can be converted to sleeping spinlocks. This has reduced a lot the PREEMPT RT patch size! Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 469/526
  • 1170.
    Threaded interrupts IThe mechanism of threaded interrupts in PREEMPT RT is still dierent from the one merged in mainline I In PREEMPT RT, all interrupt handlers are unconditionally converted to threaded interrupts. I This is a temporary solution, until interesting drivers in mainline get gradually converted to the new threaded interrupt API that has been merged in 2.6.30. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 470/526
  • 1171.
    Real-time in embeddedLinux systems Setting up PREEMPT RT Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 471/526
  • 1172.
    PREEMPT RT setup(1) I PREEMPT RT is delivered as a patch against the mainline kernel I Best to have a board supported by the mainline kernel, otherwise the PREEMPT RT patch may not apply and may require some adaptations I At the moment, only even releases of the Linux kernel are supported: 3.0, 3.2, 3.4, 3.6, 3.8, 3.10, 3.12, 3.14. I Quick set up I Download the latest PREEMPT RT patch from http://www.kernel.org/pub/linux/kernel/projects/rt/ I Download and extract the corresponding mainline kernel version I Apply the patch to the mainline kernel tree Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 472/526
  • 1173.
    PREEMPT RT setup(2) I In the kernel con
  • 1174.
    guration, be sureto enable I CONFIG_PREEMPT_RT I High-resolution timers I Compile your kernel, and boot I You are now running the real-time Linux kernel I Of course, some system con
  • 1175.
    guration remains tobe done, in particular setting appropriate priorities to the interrupt threads, which depend on your application. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 473/526
  • 1176.
    Real-time in embeddedLinux systems Real-time application development with PREEMPT RT Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 474/526
  • 1177.
    Development and compilation I No special library is needed, the POSIX real-time API is part of the standard C library I The glibc or eglibc C libraries are recommended, as support for some real-time features is not available in uClibc yet I Priority inheritance mutexes or NPTL on some architectures, for example I Compile a program I ARCH-linux-gcc -o myprog myprog.c -lrt I To get the documentation of the POSIX API I Install the manpages-posix-dev package I Run man function-name Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 475/526
  • 1178.
    Process, thread? IConfusion about the terms process, thread and task I In Unix, a process is created using fork() and is composed of I An address space, which contains the program code, data, stack, shared libraries, etc. I One thread, that starts executing the main() function. I Upon creation, a process contains one thread I Additional threads can be created inside an existing process, using pthread_create() I They run in the same address space as the initial thread of the process I They start executing a function passed as argument to pthread_create() Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 476/526
  • 1179.
    Process, thread: kernelpoint of view I The kernel represents each thread running in the system by a structure of type task_struct I From a scheduling point of view, it makes no dierence between the initial thread of a process and all additional threads created dynamically using pthread_create() Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 477/526
  • 1180.
    Creating threads ILinux support the POSIX thread API I To create a new thread pthread_create(pthread_t *thread, pthread_attr_t *attr, void *(*routine)(void*), void *arg); I The new thread will run in the same address space, but will be scheduled independently I Exiting from a thread pthread_exit(void *value_ptr); I Waiting for the termination of a thread pthread_join(pthread_t *thread, void **value_ptr); Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 478/526
  • 1181.
    Scheduling classes (1) I The Linux kernel scheduler support dierent scheduling classes I The default class, in which processes are started by default is a time-sharing class I All processes, regardless of their priority, get some CPU time I The proportion of CPU time they get is dynamic and aected by the nice value, which ranges from -20 (highest) to 19 (lowest). Can be set using the nice or renice commands I The real-time classes SCHED_FIFO and SCHED_RR I The highest priority process gets all the CPU time, until it blocks. I In SCHED_RR, round-robin scheduling between the processes of the same priority. All must block before lower priority processes get CPU time. I Priorities ranging from 0 (lowest) to 99 (highest) Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 479/526
  • 1182.
    Scheduling classes (2) I An existing program can be started in a speci
  • 1183.
    c scheduling classwith a speci
  • 1184.
    c priority usingthe chrt command line tool I Example: chrt -f 99 ./myprog I The sched_setscheduler() API can be used to change the scheduling class and priority of a process int sched_setscheduler(pid_t pid, int policy, const struct sched_param *param); I policy can be SCHED_OTHER, SCHED_FIFO, SCHED_RR, etc. I param is a structure containing the priority Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 480/526
  • 1185.
    Scheduling classes (3) I The priority can be set on a per-thread basis when a thread is created struct sched_param parm; pthread_attr_t attr; 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); I Then the thread can be created using pthread_create(), passing the attr structure. I Several other attributes can be de
  • 1186.
    ned this way:stack size, etc. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 481/526
  • 1187.
    Memory locking IIn order to solve the non-determinism introduced by virtual memory, memory can be locked I Guarantee that the system will keep it allocated I Guarantee that the system has pre-loaded everything into memory I mlockall(MCL_CURRENT | MCL_FUTURE); I Locks all the memory of the current address space, for currently mapped pages and pages mapped in the future I Other, less useful parts of the API: munlockall, mlock, munlock. I Watch out for non-currently mapped pages I Stack pages I Dynamically-allocated memory Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 482/526
  • 1188.
    Mutexes I Allowsmutual exclusion between two threads in the same address space I Initialization/destruction pthread_mutex_init(pthread_mutex_t *mutex, const pthread_mutexattr_t *mutexattr); pthread_mutex_destroy(pthread_mutex_t *mutex); I Lock/unlock pthread_mutex_lock(pthread_mutex_t *mutex); pthread_mutex_unlock(pthread_mutex_t *mutex); I Priority inheritance must be activated explicitly pthread_mutexattr_t attr; pthread_mutexattr_init (attr); pthread_mutexattr_setprotocol(attr, PTHREAD_PRIO_INHERIT); Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 483/526
  • 1189.
    Timers I Timercreation timer_create(clockid_t clockid, struct sigevent *evp, timer_t *timerid); I clockid is usually CLOCK_MONOTONIC. sigevent de
  • 1190.
    nes what happensupon timer expiration: send a signal or start a function in a new thread. timerid is the returned timer identi
  • 1191.
  • 1192.
    gure the timerfor expiration at a given time timer_settime(timer_t timerid, int flags, struct itimerspec *newvalue, struct itimerspec *oldvalue); Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 484/526
  • 1193.
    Timers (2) IDelete a timer timer_delete(timer_t timerid) I Get the resolution of a clock, clock_getres I Other functions: timer_getoverrun(), timer_gettime() Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 485/526
  • 1194.
    Signals I Signalsare asynchronous noti
  • 1195.
  • 1196.
    cation occurs either I By the call of a signal handler. Be careful with the limitations of signal handlers! I By being unblocked from the sigwait(), sigtimedwait() or sigwaitinfo() functions. Usually better. I Signal behaviour can be con
  • 1197.
    gured using sigaction() I The mask of blocked signals can be changed with pthread_sigmask() I Delivery of a signal using pthread_kill() or tgkill() I All signals between SIGRTMIN and SIGRTMAX, 32 signals under Linux. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 486/526
  • 1198.
    Inter-process communication ISemaphores I Usable between dierent processes using named semaphores I sem_open(), sem_close(), sem_unlink(), sem_init(), sem_destroy(), sem_wait(), sem_post(), etc. I Message queues I Allows processes to exchange data in the form of messages. I mq_open(), mq_close(), mq_unlink(), mq_send(), mq_receive(), etc. I Shared memory I Allows processes to communicate by sharing a segment of memory I shm_open(), ftruncate(), mmap(), munmap(), close(), shm_unlink() Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 487/526
  • 1199.
    Real-time in embeddedLinux systems Debugging latencies in PREEMPT RT Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 488/526
  • 1200.
    ftrace - Kernelfunction tracer Infrastructure that can be used for debugging or analyzing latencies and performance issues in the kernel. I Very well documented in Documentation/trace/ftrace.txt I Negligible overhead when tracing is not enabled at run-time. I Can be used to trace any kernel function! Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 489/526
  • 1201.
    Using ftrace ITracing information available through the debugfs virtual fs (CONFIG_DEBUG_FS in the Kernel Hacking section) I Mount this
  • 1202.
    lesystem as follows: mount -t debugfs nodev /sys/kernel/debug I When tracing is enabled (see the next slides), tracing information is available in /sys/kernel/debug/tracing. I Check available tracers in /sys/kernel/debug/tracing/available_tracers Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 490/526
  • 1203.
    Scheduling latency tracer CONFIG_SCHED_TRACER (Kernel Hacking section) I Maximum recorded time between waking up a top priority task and its scheduling on a CPU, expressed in us. I Check that wakeup is listed in /sys/kernel/debug/tracing/available_tracers I To select, reset and enable this tracer: echo wakeup /sys/kernel/debug/tracing/current_tracer echo 0 /sys/kernel/debug/tracing/tracing_max_latency echo 1 /sys/kernel/debug/tracing/tracing_enabled I Let your system run, in particular real-time tasks. Dummy example: chrt -f 5 sleep 1 I Disable tracing: echo 0 /sys/kernel/debug/tracing/tracing_enabled I Read the maximum recorded latency and the corresponding trace: cat /sys/kernel/debug/tracing/tracing_max_latency Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 491/526
  • 1204.
    Exemple of worst-caselatencies Tests done with cyclictest I x86 Geode 500 Mhz: 96 us I x86 Pentium Dual-core T4500 2.3 Ghz: 36 us I ARM Freescale i.MX35 533 Mhz: 120 us I ARM Marvell 88FR131 1200 Mhz: 54 us See more results at https://www.osadl.org/QA-Farm- Realtime.qa-farm-about.0.html Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 492/526
  • 1205.
    Useful reading Aboutreal-time support in the standard Linux kernel I Inside the RT patch, Steven Rostedt, Red Hat, ELC 2013 http://elinux.org/images/b/ba/Elc2013_Rostedt.pdf Video: http://j.mp/1apUtu6 I The Real-Time Linux Wiki: http://rt.wiki.kernel.org The Wiki Web for the CONFIG PREEMPT RT community, and real-time Linux in general. Contains nice and useful documents! I See also our books page. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 493/526
  • 1206.
    Real-time in embeddedLinux systems Real-time extensions to the Linux kernel Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 494/526
  • 1207.
    Linux real-time extensions Three generations I RTLinux I RTAI I Xenomai A common principle I Add an extra layer between the hardware and the Linux kernel, to manage real-time tasks separately. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 495/526
  • 1208.
    RTLinux First real-timeextension for Linux, created by Victor Yodaiken. I Nice, but the author
  • 1209.
    led a softwarepatent covering the addition of real-time support to general operating systems as implemented in RTLinux! I Its Open Patent License drew many developers away and frightened users. Community projects like RTAI and Xenomai now attract most developers and users. I February, 2007: RTLinux rights sold to Wind River. Today, no longer advertised by Wind River. I Project completely dead. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 496/526
  • 1210.
    RTAI http://www.rtai.org/ -Real-Time Application Interface for Linux I Created in 1999, by Prof. Paolo Mantegazza (long time contributor to RTLinux), Dipartimento di Ingegneria Aerospaziale Politecnico di Milano (DIAPM). I Community project. Signi
  • 1211.
    cant user base.Attracted contributors frustrated by the RTLinux legal issues. I Only really actively maintained on x86 I May oer slightly better latencies than Xenomai, at the expense of a less maintainable and less portable code base I Since RTAI is not really maintained on ARM and other embedded architectures, our presentation is focused on Xenomai. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 497/526
  • 1212.
    Xenomai project http://www.xenomai.org/ I Started in 2001 as a project aiming at emulating traditional RTOS. I Initial goals: facilitate the porting of programs to GNU / Linux. I Initially related to the RTAI project (as the RTAI / fusion branch), now independent. I Skins mimicking the APIs of traditional RTOS such as VxWorks, pSOS+, and VRTXsa as well as the POSIX API, and a native API. I Aims at working both as a co-kernel and on top of PREEMPT RT in future upstream Linux versions. I Will never be merged in the mainline kernel. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 498/526
  • 1213.
    Xenomai architecture FreeElectrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 499/526
  • 1214.
    The Adeos interruptpipeline abstraction I From the Adeos point of view, guest OSes are prioritized domains. I For each event (interrupts, exceptions, syscalls, etc...), the various domains may handle the event or pass it down the pipeline. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 500/526
  • 1215.
    Xenomai features IFactored real-time core with skins implementing various real-time APIs I Seamless support for hard real-time in user space I No second-class citizen, all ports are equivalent feature-wise I Xenomai support is as much as possible independent from the Linux kernel version (backward and forward compatible when reasonable) I Each Xenomai branch has a stable user/kernel ABI I Timer system based on hardware high-resolution timers I Per-skin time base which may be periodic I RTDM skin allowing to write real-time drivers Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 501/526
  • 1216.
    Xenomai user spacereal-time support I Xenomai supports real-time in user space on 5 architectures, including 32 and 64 bits variants. I Two modes are de
  • 1217.
    ned for athread I the primary mode, where the thread is handled by the Xenomai scheduler I the secondary mode, when it is handled by the Linux scheduler. I Thanks to the services of the Adeos I-pipe service, Xenomai system calls are de
  • 1218.
    ned. I Athread migrates from secondary mode to primary mode when such a system call is issued I 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. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 502/526
  • 1219.
    Life of aXenomai application I Xenomai applications are started like normal Linux processes, they are initially handled by the Linux scheduler and have access to all Linux services I After their initialization, they declare themselves as real-time applications, which migrates them to primary mode. In this mode: I They are scheduled directly by the Xenomai scheduler, so they have the real-time properties oered by Xenomai I They don't have access to any Linux service, otherwise they get migrated back to secondary mode and loose all real-time properties I They can only use device drivers that are implemented in Xenomai, not the ones of the Linux kernel I Need to implement device drivers in Xenomai, and to split real-time and non real-time parts of your applications. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 503/526
  • 1220.
    Real Time DriverModel (RTDM) I An approach to unify the interfaces for developing device drivers and associated applications under real-time Linux I An API very similar to the native Linux kernel driver API I Allows to develop in kernel space: I Character-style device drivers I Network-style device drivers I See the whitepaper on http://www.xenomai.org/documentation/xenomai- 2.6/pdf/RTDM-and-Applications.pdf I Current notable RTDM based drivers: I Serial port controllers; I RTnet UDP/IP stack; I RT socket CAN, drivers for CAN controllers; I Analogy, fork of the Comedi project, drivers for acquisition cards. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 504/526
  • 1221.
    Real-time in embeddedLinux systems Setting up Xenomai Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 505/526
  • 1222.
    How to buildXenomai I Download Xenomai sources at http://download.gna.org/xenomai/stable/ I Download one of the Linux versions supported by this release (see ksrc/arch/arch/patches/) I Since version 2.0, split kernel/user building model. I Kernel uses a script called script/prepare-kernel.sh which integrates Xenomai kernel-space support in the Linux sources. I Run the kernel con
  • 1223.
    guration menu. FreeElectrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 506/526
  • 1224.
    Linux options forXenomai con
  • 1225.
    guration Free Electrons- Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 507/526
  • 1226.
    Xenomai user spacesupport I User space libraries are compiled using the traditional autotools I ./configure --host=arm-linux make make DESTDIR=/your/rootfs/ install I Xenomai installs pkg-con
  • 1227.
  • 1228.
    les which helpsyou to compile your own programs against the Xenomai libraries. I See Xenomai's examples directory. I Installation details may be found in the README.INSTALL guide. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 508/526
  • 1229.
    Real-time in embeddedLinux systems Developing applications on Xenomai Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 509/526
  • 1230.
    The POSIX skin I The POSIX skin allows to recompile without changes a traditional POSIX application so that instead of using Linux real-time services, it uses Xenomai services I http://www.xenomai.org/index.php/Porting_POSIX_ applications_to_Xenomai I Clocks and timers, condition variables, message queues, mutexes, semaphores, shared memory, signals, thread management I Good for existing code or programmers familiar with the POSIX API I Of course, if the application uses any Linux service that isn't available in Xenomai, it will switch back to secondary mode I To link an application against the POSIX skin CFL=pkg-config --cflags libxenomai_posix LDF=pkg-config --libs libxenomai_posix ARCH-gcc $CFL -o rttest rttest.c $LDF Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 510/526
  • 1231.
    Communication with anormal task I If a Xenomai real-time application using the POSIX skin wishes to communicate with a separate non-real-time application, it must use the rtipc mechanism I In the Xenomai application, create an IPCPROTO_XDDP socket socket(AF_RTIPC, SOCK_DGRAM, IPCPROTO_XDDP); setsockopt(s, SOL_RTIPC, XDDP_SETLOCALPOOL, poolsz, sizeof(poolsz)); memset(saddr, 0, sizeof(saddr)); saddr.sipc_family = AF_RTIPC; saddr.sipc_port = PORTX; ret = bind(s, (struct sockaddr *)saddr, sizeof(saddr)); I And then the normal socket API sendto() / recvfrom() I In the Linux application I Open /dev/rtpPORTX, where PORTX is the XDDP port I Use read() and write() Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 511/526
  • 1232.
    The native API(1) I A Xenomai speci
  • 1233.
    c API fordeveloping real-time tasks I http://xenomai.org/documentation/branches/v2.4.x/ pdf/Native-API-Tour-rev-C.pdf I Usable both in user space and kernel space. Development of tasks in user space is the preferred way. I More coherent and more exible API than the POSIX API. Easier to learn and understand. Certainly the way to go for new applications. I Applications should include native/service.h, where service can be alarm, buffer, cond, event, heap, intr, misc, mutex, pipe, queue, sem, task, timer I To compile applications: CFL=pkg-config --cflags libxenomai_native LDF=pkg-config --libs libxenomai_native ARCH-gcc $CFL -o rttest rttest.c $LDF Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 512/526
  • 1234.
    The native API(2) I Task management services I rt_task_create(), rt_task_start(), rt_task_suspend(), rt_task_resume(), rt_task_delete(), rt_task_join(), etc. I Counting semaphore services I rt_sem_create(), rt_sem_delete(), rt_sem_p(), rt_sem_v(), etc. I Message queue services I rt_queue_create(), rt_queue_delete(), rt_queue_alloc(), rt_queue_free(), rt_queue_send(), rt_queue_receive(), etc. I Mutex services I rt_mutex_create(), rt_mutex_delete(), rt_mutex_acquire(), rt_mutex_release(), etc. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 513/526
  • 1235.
    The native API(3) I Alarm services I rt_alarm_create(), rt_alarm_delete(), rt_alarm_start(), rt_alarm_stop(), rt_alarm_wait(), etc. I Memory heap services I Allows to share memory between processes and/or to pre-allocate a pool of memory I rt_heap_create(), rt_heap_delete(), rt_heap_alloc(), rt_heap_bind() I Condition variable services I rt_cond_create(), rt_cond_delete(), rt_cond_signal(), rt_cond_broadcast(), rt_cond_wait(), etc. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 514/526
  • 1236.
    Xenomai and normaltask communication I Using rt pipes I In the native Xenomai application, use the Pipe API I rt_pipe_create(), rt_pipe_delete(), rt_pipe_receive(), rt_pipe_send(), rt_pipe_alloc(), rt_pipe_free() I In the normal Linux application I Open the corresponding /dev/rtpX
  • 1237.
    le, the minoris speci
  • 1238.
    ed at rt_pipe_create()time I Then, just read() and write() to the opened
  • 1239.
    le Free Electrons- Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 515/526
  • 1240.
    Xenomai worst-case latenciesresults I ARM OMAP5432 1.1 GHz: 24 us I ARM OMAP4430 1 GHz: 29 us I ARM OMAP3530 720 MHz: 41 us I ARM AT91SAM9263 180 MHz: 108 us I ARM AT91RM9200 180 MHz: 181 us I x86 Atom 1.6 GHz: 31 us I Geode LX800 processor at 500 MHz: 49 us See results at http://xenomai.org/~gch/core-3.10-latencies/ Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 516/526
  • 1241.
    Books Building EmbeddedLinux Systems, O'Reilly By Karim Yaghmour, Jon Masters, Gilad Ben-Yossef, Philippe Gerum and others (including Michael Opdenacker), August 2008 A nice coverage of Xenomai (Philippe Gerum) and the RT patch (Steven Rostedt) http://oreilly.com/catalog/9780596529680/ Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 517/526
  • 1242.
    Organizations I http://www.realtimelinuxfoundation.org/ Community portal for real-time Linux. Organizes a yearly workshop. I http://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. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 518/526
  • 1243.
    Practical lab -Real-time - Scheduling latency I Check clock accuracy. I Start processes with real-time priority. I Build a real-time application against the standard POSIX real-time API, and against Xenomai's POSIX skin I Compare scheduling latency on your system, between a standard kernel and a kernel with Xenomai. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 519/526
  • 1244.
    References References FreeElectrons ©Copyright 2004-2014, Free Electrons. Creative Commons BY-SA 3.0 license. Corrections, suggestions, contributions and translations are welcome! Embedded Linux Developers Free Electrons Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 520/526
  • 1245.
    Books I EmbeddedLinux Primer, Second Edition, Prentice Hall By Christopher Hallinan, October 2010 Covers a very wide range of interesting topics. http://j.mp/17NYxBP I Building Embedded Linux Systems, O'Reilly By Karim Yaghmour, Jon Masters, Gilad Ben-Yossef, Philippe Gerum and others (including Michael Opdenacker), August 2008 http://oreilly.com/catalog/9780596529680/ I Embedded Linux System Design and Development P. Raghavan, A. Lad, S. Neelakandan, Auerbach, Dec. 2005. Very good coverage of the POSIX programming API (still up to date). http://j.mp/19X8iu2 Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 521/526
  • 1246.
    Web sites IELinux.org, http://elinux.org, a Wiki entirely dedicated to embedded Linux. Lots of topics covered: real-time,
  • 1247.
    lesystem, multimedia, tools,hardware platforms, etc. Interesting to explore to discover new things. I LWN, http://lwn.net, very interesting news site about Linux in general, and speci
  • 1248.
    cally about thekernel. Weekly newsletter, available for free after one week for non-paying visitors. I Linux Gizmos, http://linuxgizmos.com, a news site about embedded Linux, mainly oriented on hardware platforms related news. Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 522/526
  • 1249.
    International conferences Usefulconferences featuring embedded Linux and kernel topics I Embedded Linux Conference: http://embeddedlinuxconference.com/ Organized by the Linux Foundation: California (San Francisco, Spring), in Europe (Fall). Very interesting kernel and user space topics for embedded systems developers. Presentation slides freely available I Linux Plumbers, http://linuxplumbersconf.org Conference on the low-level plumbing of Linux: kernel, audio, power management, device management, multimedia, etc. I FOSDEM: http://fosdem.org (Brussels, February) For developers. Presentations about system development. I Don't miss our free conference videos on http://free-electrons. com/community/videos/conferences/! Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 523/526
  • 1250.
    Last slides Lastslides Free Electrons ©Copyright 2004-2014, Free Electrons. Creative Commons BY-SA 3.0 license. Corrections, suggestions, contributions and translations are welcome! Embedded Linux Developers Free Electrons Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 524/526
  • 1251.
    Evaluation form Pleasetake a few minutes to rate this training session, by answering our on-line survey: http://free-electrons.com/doc/training/embedded-linux/survey.html Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 525/526
  • 1252.
    Last slide Thankyou! And may the Source be with you Free Electrons - Embedded Linux, kernel, drivers and Android - Development, consulting, training and support. http://free-electrons.com 526/526