Embedded Linux System Development Slides
Embedded Linux System Development Slides
Savoir-faire Linux
Your fair use and other rights are in no way affected by the above.
▶ blog:
https://blog.savoirfairelinux.com/en-ca/
▶ News and discussions (Youtube):
https://www.youtube.com/user/savoirfairelinux
▶ News and discussions (Google +):
https://plus.google.com/u/0/+Savoirfairelinuxandmore
▶ News and discussions (LinkedIn):
https://www.linkedin.com/company/savoir-faire-linux
▶ Quick news (Twitter):
https://twitter.com/sflinux
information
Savoir-faire Linux
▶ Beaglebone Black
▶ A TTL to USB RS232 adapter
▶ A microSD card with at least 2 GB of capacity
▶ A microSD card reader for your PC
▶ An Ethernet cable
During practical labs, write down all your commands in a text file.
▶ You can save a lot of time re-using
commands in later labs.
▶ This helps to replay your work if Lab commands
Embedded Linux
Savoir-faire Linux
▶ Cross-compilation toolchain
▶ Compiler that runs on the development machine, but generates
code for the target
▶ Bootloader
▶ Started by the hardware, responsible for basic initialization,
loading and executing the kernel
▶ Linux Kernel
▶ Contains the process and memory management, network stack,
device drivers and provides services to user space applications
▶ C library
▶ The interface between the kernel and the user space
applications
▶ Libraries and applications
▶ Third-party or in-house
Embedded Linux
development Embedded Linux
Experts
environment
Savoir-faire Linux
toolchains
Savoir-faire Linux
C Libraries
▶ License: LGPL
▶ C library from the GNU project
▶ Designed for performance, standards
compliance and portability
▶ Found on all GNU / Linux host systems
▶ Of course, actively maintained
▶ By default, quite big for small embedded
systems: approx 2.5 MB on ARM (version
2.9 - libc: 1.5 MB, libm: 750 KB)
▶ But some features not needed in
embedded systems can be configured out
(merged from the old eglibc project).
▶ http://www.gnu.org/software/libc/
▶ http://uclibc-ng.org/
▶ A continuation of the old uClibc project
▶ License: LGPL
▶ Lightweight C library for small embedded systems
▶ High configurability: many features can be enabled or disabled
through a menuconfig interface
▶ Supports most embedded architectures
▶ Supports no-MMU architectures (ARM Cortex-M, Blackfin,
etc.)
▶ No guaranteed binary compatibility. May need to recompile
applications when the library configuration changes.
▶ Focus on size rather than performance
▶ Small compile time
http://www.musl-libc.org/
▶ A lightweight, fast and simple library for embedded systems
▶ Created while uClibc’s development was stalled
▶ In particular, great at making small static executables
▶ Permissive license (MIT)
▶ Compare features with other C libraries:
http://www.etalabs.net/compare_libcs.html
▶ Supported by build systems such as Buildroot
Toolchain Options
Obtaining a Toolchain
▶ Crosstool-ng
▶ Rewrite of the older Crosstool, with a menuconfig-like
configuration system
▶ Feature-full: supports uClibc, glibc, musl, hard and soft float,
many architectures
▶ Actively maintained
▶ http://crosstool-ng.org/
interactions
Savoir-faire Linux
GPIOs
▶ Export a GPIO
▶ Configure it
▶ Read and write its status
▶ USB
▶ Serial (RS-232, RS-485)
▶ SPI
▶ I2C
▶ CAN
▶ PCI/PCI Express
▶ many others...
▶ Speed in Bauds
▶ Parity: odd or even (used to check a word)
▶ Stop bits: Number of stop bits at the end of a word
▶ The number of data bits
The speed in bit/s is inferior to the speed in Bauds because of
start and stop bits.
The most common configuration is 115200 bauds. 8N1: 8 data
bits, No parity bit and 1 stop bit.
Embedded Linux
Experts
Bootloaders
Savoir-faire Linux
Boot Sequence
CHIP_defconfig
CONFIG_ARM=y
CONFIG_ARCH_SUNXI=y
CONFIG_MACH_SUN5I=y
CONFIG_DRAM_TIMINGS_DDR3_800E_1066G_1333J=y
# CONFIG_MMC is not set
CONFIG_USB0_VBUS_PIN="PB10"
CONFIG_VIDEO_COMPOSITE=y
CONFIG_DEFAULT_DEVICE_TREE="sun5i-r8-chip"
CONFIG_SPL=y
CONFIG_SYS_EXTRA_OPTIONS="CONS_INDEX=2"
# CONFIG_CMD_IMLS is not set
CONFIG_CMD_DFU=y
CONFIG_CMD_USB_MASS_STORAGE=y
CONFIG_AXP_ALDO3_VOLT=3300
CONFIG_AXP_ALDO4_VOLT=3300
CONFIG_USB_MUSB_GADGET=y
CONFIG_USB_GADGET=y
CONFIG_USB_GADGET_DOWNLOAD=y
CONFIG_G_DNL_MANUFACTURER="Allwinner Technology"
CONFIG_G_DNL_VENDOR_NUM=0x1f3a
CONFIG_G_DNL_PRODUCT_NUM=0x1010
CONFIG_USB_EHCI_HCD=y
CPU: SAMA5D36
Crystal frequency: 12 MHz
CPU clock : 528 MHz
Master clock : 132 MHz
DRAM: 256 MiB
NAND: 256 MiB
MMC: mci: 0
In: serial
Out: serial
Err: serial
Net: gmac0
Version details
U-Boot> version
U-Boot 2016.05 (May 17 2016 - 12:41:15 -0400)
▶ loadb, loads, loady, load a file from the serial line to RAM
▶ usb, to initialize and control the USB subsystem, mainly used
for USB storage devices such as USB keys
▶ mmc, to initialize and control the MMC subsystem, used for
SD and microSD cards
▶ nand, to erase, read and write contents to NAND flash
▶ erase, protect, cp, to erase, modify protection and write to
NOR flash
▶ md, displays memory contents. Can be useful to check the
contents loaded in memory, or to look at hardware registers.
▶ mm, modifies memory contents. Can be useful to modify
directly hardware registers, for testing purposes.
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
introduction
Savoir-faire Linux
Linux features
▶ The main interface between the kernel and user space is the
set of system calls
▶ About 300 system calls that provide the main kernel services
▶ File and device operations, networking operations,
inter-process communication, process management, memory
mapping, timers, threads, synchronization primitives, etc.
▶ This interface is stable over time: only new system calls can
be added by the kernel developers
▶ 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
▶ The whole Linux sources are Free Software released under the
GNU General Public License version 2 (GPL v2).
▶ For the Linux kernel, this basically implies that:
▶ When you receive or buy a device with Linux on it, you should
receive the Linux sources, with the right to study, modify and
redistribute them.
▶ When you produce Linux based devices, you must release the
sources to the recipient, with the same rights, with no
restriction.
The official 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
Very difficult to find out the key changes and to get the global
picture out of individual changes.
# *DOCUMENTATION*
# *DOCUMENTATION*
Kernel configuration
make xconfig
▶ The most common graphical interface to configure the kernel.
▶ Make sure you read
help -> introduction: useful options!
▶ File browser: easier to load configuration files
▶ Search interface to look for parameters
▶ Required Debian / Ubuntu packages: qt5-default g++
pkg-config
#
# 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
Savoir-Faire Linux savoirfairelinux.com 187/460
make gconfig
make gconfig
▶ GTK based graphical
configuration interface.
Functionality similar to that
of make xconfig.
▶ Just lacking a search
functionality.
▶ Required Debian packages:
libglade2-dev
make menuconfig
▶ Useful when no graphics are
available. Pretty convenient
too!
▶ Same interface found in
other tools: BusyBox,
Buildroot...
▶ Required Debian packages:
libncurses-dev
make nconfig
▶ A newer, similar text
interface
▶ More user friendly (for
example, easier to access
help information).
▶ Required Debian packages:
libncurses-dev
make oldconfig
▶ Needed very often!
▶ Useful to upgrade a .config file from an earlier kernel release
▶ Issues warnings for configuration parameters that no longer
exist in the new kernel.
▶ Asks for values for new parameters (while xconfig and
menuconfig silently set default values for new parameters).
If you edit a .config file by hand, it’s strongly recommended to
run make oldconfig afterwards!
A frequent problem:
▶ After changing several kernel configuration settings, your
kernel no longer works.
▶ If you don’t remember all the changes you made, you can get
back to your previous configuration:
$ cp .config.old .config
▶ All the configuration interfaces of the kernel (xconfig,
menuconfig, oldconfig...) keep this .config.old backup
copy.
▶ make
▶ in the main kernel source directory
▶ Remember to run multiple jobs in parallel if you have multiple
CPU cores. Example: make -j 4
▶ No need to run as root!
▶ Generates
▶ vmlinux, the raw uncompressed kernel image, in the ELF
format, useful for debugging purposes, but cannot be booted
▶ arch/<arch>/boot/*Image, the final, usually compressed,
kernel image that can be booted
▶ bzImage for x86, zImage for ARM, vmImage.gz for Blackfin,
etc.
▶ arch/<arch>/boot/dts/*.dtb, compiled Device Tree files (on
some architectures)
▶ All kernel modules, spread over the kernel source tree, as .ko
(Kernel Object) files.
▶ make install
▶ 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, as it installs files on the development
workstation.
▶ Installs
▶ /boot/vmlinuz-<version>
Compressed kernel image. Same as the one in
arch/<arch>/boot
▶ /boot/System.map-<version>
Stores kernel symbol addresses
▶ /boot/config-<version>
Kernel configuration for this version
▶ Typically re-runs the bootloader configuration utility to take
the new kernel into account.
▶ make modules_install
▶ Does the installation for the host system by default, so needs
to be run as root
▶ Installs all modules in /lib/modules/<version>/
▶ kernel/
Module .ko (Kernel Object) files, in the same directory
structure as in the sources.
▶ modules.alias
Module aliases for module loading utilities. Example line:
alias sound-service-?-0 snd_mixer_oss
▶ modules.dep, modules.dep.bin (binary hashed)
Module dependencies
▶ modules.symbols, modules.symbols.bin (binary hashed)
Tells which module a given symbol belongs to.
▶ Run make
▶ Copy the final kernel image to the target storage
▶ can be zImage, vmlinux, bzImage in arch/<arch>/boot
▶ copying the Device Tree Blob might be necessary as well, they
are available in arch/<arch>/boot/dts
▶ make install is rarely used in embedded development, as the
kernel image is a single file, easy to handle
▶ It is however possible to customize the make install
behaviour in arch/<arch>/boot/install.sh
▶ make modules_install is used even in embedded
development, as it installs many modules and description files
▶ make INSTALL_MOD_PATH=<dir>/ modules_install
▶ The INSTALL_MOD_PATH variable is needed to install the
modules in the target root filesystem instead of your host root
filesystem.
Filesystem
Savoir-faire Linux
Contents
Device Files
▶ Block devices
▶ A device composed of fixed-sized blocks, that can be read and
written to store data
▶ Used for hard disks, USB keys, SD cards, etc.
▶ Character devices
▶ Originally, an infinite stream of bytes, with no beginning, no
end, no size. The pure example: a serial port.
▶ Used for serial ports, terminals, but also sound cards, video
acquisition devices, frame buffers
▶ Most of the devices that are not block devices are represented
as character devices by the Linux kernel
Example C code that uses the usual file API to write data to a
serial port
int fd;
fd = open("/dev/ttyS0", O_RDWR);
write(fd, "Hello", 5);
close(fd);
Pseudo Filesystems
Minimal filesystem
and /bin/sh.
▶ In the case of an initramfs, it will only look for /init. Another
process is stopped.
▶ The init application is responsible for starting all other user
Embedded Linux
system Embedded Linux
Experts
development
Savoir-faire Linux
▶ All software that are under a free software license give four
freedoms to all users
▶ Freedom to use
▶ Freedom to study
▶ Freedom to copy
▶ Freedom to modify and distribute modified copies
▶ See http://www.gnu.org/philosophy/free-sw.html for a
definition of Free Software
▶ Open Source software, as per the definition of the Open
Source Initiative, are technically similar to Free Software in
terms of freedoms
▶ See http://www.opensource.org/docs/osd for the definition
of Open Source Software
[...]
http://matt.ucc.asn.au/dropbear/dropbear.html
▶ Very small memory footprint ssh server for embedded systems
▶ Satisfies most needs. Both client and server!
▶ Size: 110 KB, statically compiled with uClibc on i386.
(OpenSSH client and server: approx 1200 KB, dynamically
compiled with glibc on i386)
▶ Useful to:
▶ Get a remote console on the target device
▶ Copy files to and from the target device (scp or
rsync -e ssh).
▶ An alternative to OpenSSH, used on desktop and server
systems.
http://www.sqlite.org
▶ SQLite is a small C library that implements a self-contained,
embeddable, lightweight, zero-configuration SQL database
engine
▶ The database engine of choice for embedded Linux systems
▶ Can be used as a normal library
▶ Can be directly embedded into a application, even a
proprietary one since SQLite is released in the public domain
http://webkit.org/
▶ Web browser engine. Application framework that
can be used to develop web browsers.
▶ License: portions in LGPL and others in BSD.
Proprietary applications allowed.
▶ Used by many web browsers: Safari, iPhone and
Android default browsers ... Google Chrome now
uses a fork of its WebCore component). Used by
e-mail clients too to render HTML:
http://trac.webkit.org/wiki/
Applications%20using%20WebKit
▶ Multiple graphical back-ends: Qt4, GTK, EFL...
▶ You could use it to create your custom browser.
System building
▶ Goal
▶ Integrate all the software
components, both third-party
and in-house, into a working root
filesystem
▶ It involves the download,
extraction, configuration,
compilation and installation of all
components, and possibly fixing
issues and adapting configuration
files
▶ Several solutions
▶ Manually
▶ System building tools Penguin picture: http://bit.ly/1PwDklz
▶ Distributions or ready-made
filesystems
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
config BR2_PACKAGE_GQVIEW
bool "gqview"
depends on BR2_PACKAGE_LIBGTK2
help
GQview is an image viewer for Unix operating systems
http://prdownloads.sourceforge.net/gqview
▶ It must be sourced from package/Config.in:
source "package/gqview/Config.in"
$(eval $(autotools-package))
Fedora
▶ http://fedoraproject.org/wiki/
Architectures/ARM
▶ Supported on various recent ARM boards
(such as Beaglebone Black). Pidora
supports Raspberry Pi too.
▶ Supports QEMU emulated ARM boards
too (Versatile Express board)
▶ Shipping the same version as for desktops!
Ubuntu
▶ Had some releases for ARM mobile
multimedia devices, but stopped at
version 12.04. Now focusing on ARM
servers only.
Embedded Linux
application Embedded Linux
Experts
development
Savoir-faire Linux
▶ Application development
▶ Developing applications on embedded Linux
▶ Building your applications
▶ Source management
▶ Integrated development environments (IDEs)
▶ Version control systems
▶ Debugging and analysis tools
▶ Debuggers
▶ Memory checkers
▶ System analysis
CROSS_COMPILE?=arm-linux-
CC=$(CROSS_COMPILE)gcc
OBJS=foo.o bar.o
all: foobar
foobar: $(OBJS)
$(CC) -o $@ $^
clean:
$(RM) -f foobar $(OBJS)
▶ Case of an application that uses the Glib and the GPS libraries
CROSS_COMPILE?=arm-linux-
LIBS=libgps glib-2.0
OBJS=foo.o bar.o
CC=$(CROSS_COMPILE)gcc
CFLAGS=$(shell pkg-config --cflags $(LIBS))
LDFLAGS=$(shell pkg-config --libs $(LIBS))
all: foobar
foobar: $(OBJS)
$(CC) -o $@ $^ $(LDFLAGS)
clean:
$(RM) -f foobar $(OBJS)
Integrated Development
Environments (IDE)
http://kdevelop.org
▶ A full featured IDE!
▶ License: GPL
▶ Supports many languages: Ada, C, C++,
Database, Java, Perl, PHP, Python, Ruby, Shell
▶ Supports many kinds of projects: KDE, but also
GTK, Gnome, kernel drivers, embedded (Opie)...
▶ Many features: editor, syntax highlighting, code
completion, compiler interface, debugger
interface, file manager, class browser...
Nice overview:
http://en.wikipedia.org/wiki/Kdevelop
Ruby debugger
http://www.eclipse.org/
▶ An extensible, plug-in based software
development kit, typically used for creating IDEs.
▶ Supported by the Eclipse foundation, a
non-profit consortium of major software industry
vendors (IBM, Intel, Borland, Nokia, Wind
River, Zend, Computer Associates...).
▶ Free Software license (Eclipse Public License).
Incompatible with the GPL.
▶ Supported platforms: GNU/Linux, Unix,
Windows
Extremely popular: created a lot of attraction.
Vim
▶ Many embedded Linux developers
simply use Vim or Emacs. They
can integrate with debuggers,
source code browsers such as
cscope, offer syntax highlighting
and more.
▶ Geany is an easy-to-use graphical
code editor.
Emacs
▶ 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!
▶ Git
▶ Initially designed and developed by Linus Torvalds for Linux
kernel development
▶ Extremely popular in the community, and used by more and
more projects (kernel, U-Boot, Barebox, uClibc, GNOME,
X.org, etc.)
▶ Outstanding performance, in particular in big projects
▶ http://en.wikipedia.org/wiki/Git_(software)
▶ Mercurial
▶ Another system, created with the same goals as Git.
▶ Used by some big projects too
▶ http://en.wikipedia.org/wiki/Mercurial
http://en.wikipedia.org/wiki/Version_control_systems#
Distributed_revision_control
Debuggers
Remote debugging
Memory checkers
http://valgrind.org/
▶ GNU GPL Software suite for debugging and
profiling programs.
▶ Supported platforms: Linux on x86, x86_64,
ppc32, ppc64 and arm (armv7 only: Cortex A8,
A9 and A5)
▶ Can detect many memory management and
threading bugs.
▶ Profiler: provides information helpful to speed
up your program and reduce its memory usage.
▶ The most popular tool for this usage. Even used
by projects with hundreds of programmers.
System analysis
A tool to trace library calls used by a program and all the signals it
receives
▶ Very useful complement to strace, which shows only system
calls.
▶ Of course, works even if you don’t have the sources
▶ Allows to filter library calls with regular expressions, or just by
a list of function names.
▶ Manual page: http://linux.die.net/man/1/ltrace
See http://en.wikipedia.org/wiki/Ltrace for details
http://oprofile.sourceforge.net
▶ A system-wide profiling tool
▶ Can collect statistics like the top users of the CPU.
▶ Works without having the sources.
▶ Requires a kernel patch to access all features, but is already
available in a standard kernel.
▶ Requires more investigation to see how it works.
▶ Ubuntu/Debian packages: oprofile, oprofile-gui
Application development
▶ Cross compile a simple Qt program
with Qt Creator
▶ Run and debug it on the target
with Qt Creator
Embedded Linux
Experts
Busybox
Savoir-faire Linux
Embedded Linux
Experts
Block filesystems
Savoir-faire Linux
Block devices
Available filesystems
http://en.wikipedia.org/wiki/F2FS
▶ Filesystem that takes into account the characteristics of
flash-based storage: eMMC, SD cards, SSD, etc.
▶ Developed and contributed by Samsung
▶ Available in the mainline Linux kernel
▶ For optimal results, need a number of details about the
storage internal behavior which may not easy to get
▶ Benchmarks: best performer on flash devices most of the time:
See http://lwn.net/Articles/520003/
▶ Technical details: http://lwn.net/Articles/518988/
▶ Not as widely used as ext3,4, even on flash-based storage.
▶ Once a filesystem image has been created, one can access and
modifies its contents from the development workstation, using
the loop mechanism
▶ Example:
genext2fs -d rootfs/ rootfs.img
mkdir /tmp/tst
mount -t ext2 -o loop rootfs.img /tmp/tst
▶ In the /tmp/tst directory, one can access and modify the
contents of the rootfs.img file.
▶ This is possible thanks to loop, which is a kernel driver that
emulates a block device with the contents of a file.
▶ Do not forget to run umount before using the filesystem image!
Embedded Linux
Experts
Flash filesystems
Savoir-faire Linux
▶ Block devices:
▶ Allow for random data access using fixed size blocks
▶ Do not require special care when writing on the media
▶ Block size is relatively small (minimum 512 bytes, can be
increased for performance reasons)
▶ Considered as reliable (if the storage media is not, some
hardware or software parts are supposed to make it reliable)
▶ Flash devices:
▶ Allow for random data access too
▶ Require special care before writing on the media (erasing the
region you are about to write on)
▶ Erase, write and read operation might not use the same block
size
▶ Reliability depends on the flash technology
The Device Tree is the standard place to define MTD partitions for
platforms with Device Tree support.
Example from arch/arm/boot/dts/omap3-igep.dtsi:
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>;
};
▶ Example:
setenv mtdids nand0=omap2-nand.0
setenv mtdparts mtdparts=omap2-nand.0:512k(X-Loader)ro,1536k(U-Boot)ro,512k(Env),4m(Kernel),-(RootFS)
Flash users should also take the limited lifetime of flash devices
into account by taking additional precautions
▶ Do not use your flash storage as swap area (rare in embedded
systems anyway)
▶ Mount your filesystems as read-only, or use read-only
filesystems (SquashFS), whenever possible.
▶ Keep volatile files in RAM (tmpfs)
▶ Don’t use the sync mount option (commits writes
immediately). Use the fsync() system call for per-file
synchronization.
[data-volume]
[kernel-volume] [rootfs-volume] mode=ubi
mode=ubi mode=ubi image=data.ubifs
image=zImage image=rootfs.squashfs vol_id=3
vol_id=1 vol_id=2 vol_size=30MiB
vol_type=static vol_type=static vol_type=dynamic
vol_name=kernel vol_name=rootfs vol_name=data
vol_flags=autoresize
Embedded Linux
Experts
References
Savoir-faire Linux