526
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 1, 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

Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

  • Upload
    buithu

  • View
    303

  • Download
    1

Embed Size (px)

Citation preview

Page 1: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Embedded Linux system development

Embedded Linux systemdevelopment

Free Electrons

© Copyright 2004-2014, Free Electrons.Creative Commons BY-SA 3.0 license.Latest update: October 1, 2014.Document updates and sources:http://free-electrons.com/doc/training/embedded-linux

Corrections, suggestions, contributions and translations are welcome!

Embedded LinuxDevelopers

Free Electrons

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 1/526

Page 2: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Rights to copy

© Copyright 2004-2014, Free ElectronsLicense: Creative Commons Attribution - Share Alike 3.0http://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 distributethe 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 ofthis work.

I Any of these conditions can be waived if you get permission from the copyrightholder.

Your fair use and other rights are in no way affected by the above.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 2/526

Page 3: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Electronic copies of these documents

I Electronic copies of your particular version of the materials areavailable on:http://free-electrons.com/doc/training/embedded-linux

I Open the corresponding documents and use them throughoutthe course to look for explanations given earlier by theinstructor.

I You will need these electronic versions because we neitherprint any index nor any table of contents (quite highenvironmental cost for little added value)

I For future reference, see the first slide to see where documentupdates will be available.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 3/526

Page 4: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

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 files and directories:drivers/input

include/linux/fb.h

I Links to the declarations, definitions and instances of kernelsymbols (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

Page 5: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Free Electrons at a glance

I Engineering company created in 2004(not a training company!)

I Locations: Orange, Toulouse, Saint Etienne / Lyon (France)

I Serving customers all around the worldSee http://free-electrons.com/company/customers/

I Head count: 9Only 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, technicalsupport.

I Added value: get the best of the user and developmentcommunity and the resources it offers.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 5/526

Page 6: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

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

Page 7: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Mailing lists for session participants

If you participated to a course taught by Free Electrons, you cansubscribe to our mailing lists dedicated to our training customers:

I To ask questions about the topics covered by the courses, andto get a first level of technical support for issues that your arefacing in real life or with your own hardware.

I To share your experience with other people having taken thesame course and done the same practical labs.

I To share further comments and suggestions about coursecontents.

I To get news about important updates to the course materials.

See http://free-electrons.com/training/mailing-lists

A simple way to register: fill 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

Page 8: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Generic course information

Generic courseinformationFree Electrons

© Copyright 2004-2014, Free Electrons.Creative Commons BY-SA 3.0 license.Corrections, suggestions, contributions and translations are welcome!

Embedded LinuxDevelopers

Free Electrons

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 8/526

Page 9: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

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 NANDflash

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 files, 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

Page 10: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Participate!

During the lectures...

I Don’t hesitate to ask questions. Other people in the audiencemay have similar questions too.

I This helps the trainer to detect any explanation that wasn’tclear or detailed enough.

I Don’t hesitate to share your experience, for example tocompare Linux / Android with other operating systems usedin your company.

I Your point of view is most valuable, because it can be similarto your colleagues’ and different from the trainer’s.

I Your participation can make our session more interactive andmake the topics easier to learn.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 10/526

Page 11: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Practical lab guidelines

During practical labs...

I We cannot support more than 8 workstations at once (eachwith its board and equipment). Having more would make thewhole class progress slower, compromising the coverage of thewhole training agenda (exception for public sessions: up to 10people).

I So, if you are more than 8 participants, please form up to 8working groups.

I Open the electronic copy of your lecture materials, and use itthroughout the practical labs to find 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 asASCII 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

Page 12: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Cooperate!

As in the Free Software and Open Source community, cooperationduring practical labs is valuable in this training session:

I If you complete your labs before other people, don’t hesitateto help other people and investigate the issues they face. Thefaster we progress as a group, the more time we have toexplore extra topics.

I Explain what you understood to other participants whenneeded. 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

Page 13: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Command memento sheet

I This memento sheet givescommand examples for the mosttypical needs (looking for files,extracting a tar archive...)

I It saves us 1 day of UNIX / Linuxcommand line training.

I Our best tip: in the command lineshell, always hit the Tab key tocomplete command names and filepaths. This avoids 95% of typingmistakes.

I Get an electronic copy onhttp://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

Page 14: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

vi basic commands

I The vi editor is very useful tomake quick changes to files in anembedded target.

I Though not very user friendly atfirst, vi is very powerful and itsmain 15 commands are easy tolearn and are sufficient for 99% ofeveryone’s needs!

I Get an electronic copy onhttp://free-electrons.com/

doc/training/embedded-

linux/vi_memento.pdf

I You can also take the quick tutorialby running vimtutor. This is aworthy investment!

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 14/526

Page 15: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Introduction to Embedded Linux

Introduction toEmbedded LinuxFree Electrons

© Copyright 2004-2014, Free Electrons.Creative Commons BY-SA 3.0 license.Corrections, suggestions, contributions and translations are welcome!

Embedded LinuxDevelopers

Free Electrons

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 15/526

Page 16: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Birth of free software

I 1983, Richard Stallman, GNU project and the free softwareconcept. Beginning of the development of gcc, gdb, glibc andother important tools

I 1991, Linus Torvalds, Linux kernel project, a Unix-likeoperating system kernel. Together with GNU software andmany other open-source components: a completely freeoperating system, GNU/Linux

I 1995, Linux is more and more popular on server systems

I 2000, Linux is more and more popular on embeddedsystems

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

Page 17: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Free software?

I A program is considered free when its license offers to all itsusers the following four freedoms

I Freedom to run the software for any purposeI Freedom to study the software and to change itI Freedom to redistribute copiesI Freedom to distribute copies of modified versions

I Those freedoms are granted for both commercial andnon-commercial use

I They imply the availability of source code, software can bemodified 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

Page 18: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

What is embedded Linux?

Embedded Linux is the usage of theLinux kernel and various

open-source components inembedded systems

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 18/526

Page 19: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Introduction to Embedded Linux

Advantages of Linux and open-sourcefor embedded systems

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 19/526

Page 20: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Re-using components

I The key advantage of Linux and open-source in embeddedsystems is the ability to re-use components

I The open-source ecosystem already provides manycomponents for standard features, from hardware support tonetwork protocols, going through multimedia, graphic,cryptographic libraries, etc.

I As soon as a hardware device, or a protocol, or a feature iswide-spread enough, high chance of having open-sourcecomponents 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

Page 21: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Low cost

I Free software can be duplicated on as many devices as youwant, free of charge.

I If your embedded system uses only free software, you canreduce the cost of software licenses to zero. Even thedevelopment tools are free, unless you choose a commercialembedded Linux edition.

I Allows to have a higher budget for the hardware or toincrease 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

Page 22: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Full control

I With open-source, you have the source code for allcomponents in your system

I Allows unlimited modifications, changes, tuning, debugging,optimization, for an unlimited period of time

I Without lock-in or dependency from a third-party vendorI 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 yoursystem

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 22/526

Page 23: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Quality

I Many open-source components are widely used, on millions ofsystems

I Usually higher quality than what an in-house development canproduce, or even proprietary vendors

I Of course, not all open-source components are of goodquality, but most of the widely-used ones are.

I Allows to design your system with high-qualitycomponents at the foundations

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 23/526

Page 24: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Eases testing of new features

I Open-source being freely available, it is easy to get a piece ofsoftware and evaluate it

I Allows to easily study several options while making a choice

I Much easier than purchasing and demonstration proceduresneeded 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

Page 25: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Community support

I Open-source software components are developed bycommunities of developers and users

I This community can provide a high-quality support: you candirectly contact the main developers of the component youare using. The likelyhood of getting an answer doesn’t dependwhat company you work for.

I Often better than traditional support, but one needs tounderstand how the community works to properly use thecommunity support possibilities

I Allows to speed up the resolution of problems whendeveloping your system

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 25/526

Page 26: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Taking part into the community

I Possibility of taking part into the development community ofsome of the components used in the embedded systems: bugreporting, test of new versions or features, patches that fixbugs or add new features, etc.

I Most of the time the open-source components are not thecore value of the product: it’s the interest of everybody tocontribute back.

I For the engineers: a very motivating way of being recognizedoutside the company, communication with others in the samefield, opening of new possibilities, etc.

I For the managers: motivation factor for engineers, allowsthe company to be recognized in the open-source communityand therefore get support more easily and be more attractiveto open-source developers

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 26/526

Page 27: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Introduction to Embedded Linux

A few examples of embedded systemsrunning Linux

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 27/526

Page 28: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Personal routers

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 28/526

Page 29: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Television

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 29/526

Page 30: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Point of sale terminal

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 30/526

Page 31: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Laser cutting machine

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 31/526

Page 32: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Viticulture machine

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 32/526

Page 33: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Introduction to Embedded Linux

Embedded hardware for Linuxsystems

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 33/526

Page 34: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Processor and architecture (1)

I The Linux kernel and most other architecture-dependentcomponent support a wide range of 32 and 64 bitsarchitectures

I x86 and x86-64, as found on PC platforms, but also embeddedsystems (multimedia, industrial)

I ARM, with hundreds of different 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 Blackfin (DSP architecture)I Microblaze (soft-core for Xilinx FPGA)I Coldfire, 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

Page 35: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Processor and architecture (2)

I Both MMU and no-MMU architectures are supported, eventhough 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 othercomponents are generally architecture-independent

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 35/526

Page 36: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

RAM and storage

I RAM: a very basic Linux system can work within 8 MB ofRAM, but a more realistic system will usually require at least32 MB of RAM. Depends on the type and size of applications.

I Storage: a very basic Linux system can work within 4 MB ofstorage, but usually more is needed.

I Flash storage is supported, both NAND and NOR flash, withspecific filesystems

I Block storage including SD/MMC cards and eMMC issupported

I Not necessarily interesting to be too restrictive on the amountof RAM/storage: having flexibility at this level allows tore-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

Page 37: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Communication

I The Linux kernel has support for many commoncommunication buses

I I2CI SPII CANI 1-wireI SDIOI USB

I And also extensive networking supportI Ethernet, Wifi, 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

Page 38: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Types of hardware platforms

I Evaluation platforms from the SoC vendor. Usuallyexpensive, but many peripherals are built-in. Generallyunsuitable for real products.

I Component on Module, a small board with onlyCPU/RAM/flash and a few other core components, withconnectors to access all other peripherals. Can be used tobuild end products for small to medium quantities.

I Community development platforms, a new trend to make aparticular SoC popular and easily available. Those areready-to-use and low cost, but usually have less peripheralsthan evaluation platforms. To some extent, can also be usedfor real products.

I Custom platform. Schematics for evaluation boards ordevelopment platforms are more and more commonly freelyavailable, 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

Page 39: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Criteria for choosing the hardware

I Make sure the hardware you plan to use is already supportedby the Linux kernel, and has an open-source bootloader,especially the SoC you’re targeting.

I Having support in the official versions of the projects (kernel,bootloader) is a lot better: quality is better, and new versionsare available.

I Some SoC vendors and/or board vendors do not contributetheir changes back to the mainline Linux kernel. Ask them todo so, or use another product if you can. A good measurementis to see the delta between their kernel and the official one.

I Between properly supported hardware in the officialLinux kernel and poorly-supported hardware, there willbe huge differences in development time and cost.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 39/526

Page 40: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

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

Page 41: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Global architecture

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 41/526

Page 42: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Software components

I Cross-compilation toolchainI Compiler that runs on the development machine, but generates

code for the target

I BootloaderI Started by the hardware, responsible for basic initialization,

loading and executing the kernel

I Linux KernelI Contains the process and memory management, network stack,

device drivers and provides services to user space applications

I C libraryI The interface between the kernel and the user space

applications

I Libraries and applicationsI Third-party or in-house

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 42/526

Page 43: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Embedded Linux work

Several distinct tasks are needed when deploying embedded Linuxin a product:

I Board Support Package developmentI A BSP contains a bootloader and kernel with the suitable

device drivers for the targeted hardwareI Purpose of our Kernel Development training

I System integrationI Integrate all the components, bootloader, kernel, third-party

libraries and applications and in-house applications into aworking system

I Purpose of this training

I Development of applicationsI Normal Linux applications, but using specifically chosen

libraries

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 43/526

Page 44: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Embedded Linux development environment

Embedded LinuxdevelopmentenvironmentFree Electrons

© Copyright 2004-2014, Free Electrons.Creative Commons BY-SA 3.0 license.Corrections, suggestions, contributions and translations are welcome!

Embedded LinuxDevelopers

Free Electrons

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 44/526

Page 45: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Embedded Linux solutions

I Two ways to switch to embedded LinuxI Use solutions provided and supported by vendors like

MontaVista, Wind River or TimeSys. These solutions comewith their own development tools and environment. They usea 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 aparticular vendor, and therefore use community solutions

I However, knowing the concepts, switching to vendor solutionswill be easy

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 45/526

Page 46: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

OS for Linux development

I We strongly recommend to use Linux as the desktop operatingsystem to embedded Linux developers, for multiple reasons.

I All community tools are developed and designed to run onLinux. Trying to use them on other operating systems(Windows, Mac OS X) will lead to trouble, and their usage onthose systems is generally not supported by communitydevelopers.

I As Linux also runs on the embedded device, all the knowledgegained from using Linux on the desktop will apply similarly tothe embedded device.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 46/526

Page 47: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Desktop Linux distribution

I Any good and sufficiently recent Linuxdesktop distribution can be used for thedevelopment workstation

I Ubuntu, Debian, Fedora, openSUSE,Red Hat, etc.

I We have chosen Ubuntu, as it is a widelyused and easy to use desktop Linuxdistribution

I The Ubuntu setup on the training laptopshas intentionally been left untouched afterthe normal installation process. Learningembedded Linux is also about learning thetools needed on the developmentworkstation!

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 47/526

Page 48: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Linux root and non-root users

I Linux is a multi-user operating systemI The root user is the administrator, and it can do privileged

operations such as: mounting filesystems, configuring thenetwork, creating device files, changing the systemconfiguration, installing or removing software

I All other users are unprivileged, and cannot perform thoseadministrator-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 configured so that the user accountcreated first is allowed to run privileged operations through aprogram 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

Page 49: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Software packages

I The distribution mechanism for software in GNU/Linux isdifferent from the one in Windows

I Linux distributions provides a central and coherent way ofinstalling, updating and removing applications and libraries:packages

I Packages contains the application or library files, andassociated meta-information, such as the version and thedependencies

I .deb on Debian and Ubuntu, .rpm on Red Hat, Fedora,openSUSE

I Packages are stored in repositories, usually on HTTP or FTPservers

I You should only use packages from official repositories foryour distribution, unless strictly required.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 49/526

Page 50: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Managing software packages (1)

Instructions for Debian based GNU/Linux systems(Debian, Ubuntu...)

I Package repositories are specified in/etc/apt/sources.list

I To update package repository lists:sudo apt-get update

I To find the name of a package to install, the best is to use thesearch engine on http://packages.debian.org or onhttp://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

Page 51: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

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 interfacesI Synaptic for GNOMEI 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

Page 52: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Host vs. target

I When doing embedded development, there is always a splitbetween

I The host, the development workstation, which is typically apowerful PC

I The target, which is the embedded system under development

I They are connected by various means: almost always a serialline for debugging purposes, frequently an Ethernetconnection, sometimes a JTAG interface for low-leveldebugging

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 52/526

Page 53: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Serial line communication program

I An essential tool for embedded development is a serial linecommunication 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 ofthem: picocom

I Installation with sudo apt-get install picocomI Run with picocom -b BAUD_RATE /dev/SERIAL_DEVICEI Exit with Control-A Control-X

I SERIAL_DEVICE is typicallyI ttyUSBx for USB to serial convertersI ttySx for real serial ports

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 53/526

Page 54: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Command line tips

I Using the command line is mandatory for many operationsneeded for embedded Linux development

I It is a very powerful way of interacting with the system, withwhich you can save a lot of time.

I Some useful tipsI You can use several tabs in the Gnome TerminalI 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 searchthrough the command history. Hit [Control] [r] again tosearch backwards in the history

I You can copy/paste paths directly from the file manager to theterminal by drag-and-drop.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 54/526

Page 55: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

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

Page 56: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Cross-compiling toolchains

Cross-compilingtoolchainsFree Electrons

© Copyright 2004-2014, Free Electrons.Creative Commons BY-SA 3.0 license.Corrections, suggestions, contributions and translations are welcome!

Embedded LinuxDevelopers

Free Electrons

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 56/526

Page 57: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Cross-compiling toolchains

Definition and Components

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 57/526

Page 58: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Definition (1)

I The usual development tools available on a GNU/Linuxworkstation is a native toolchain

I This toolchain runs on your workstation and generates codefor your workstation, usually x86

I For embedded system development, it is usually impossible ornot interesting to use a native toolchain

I The target is too restricted in terms of storage and/or memoryI The target is very slow compared to your workstationI 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 yourtarget.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 58/526

Page 59: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Definition (2)

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 59/526

Page 60: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Machines in build procedures

I Three machines must be distinguished when discussingtoolchain 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

Page 61: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Different toolchain build procedures

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 61/526

Page 62: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Components

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 62/526

Page 63: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Binutils

I Binutils is a set of tools to generate and manipulate binariesfor a given CPU architecture

I as, the assembler, that generates binary code from assemblersource code

I ld, the linkerI ar, ranlib, to generate .a archives, used for librariesI 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

Page 64: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Kernel headers (1)

I The C library and compiledprograms needs to interact withthe kernel

I Available system calls and theirnumbers

I Constant definitionsI Data structures, etc.

I Therefore, compiling the C libraryrequires kernel headers, and manyapplications also require them.

I Available in <linux/...> and<asm/...> and a few otherdirectories corresponding to theones visible in include/ in thekernel sources

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 64/526

Page 65: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

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 definitions, 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

Page 66: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Kernel headers (3)

I The kernel to user space ABI is backward compatibleI Binaries generated with a toolchain using kernel headers older

than the running kernel will work without problem, but won’tbe able to use the new system calls, data structures, etc.

I Binaries generated with a toolchain using kernel headers newerthan the running kernel might work on if they don’t use therecent features, otherwise they will break

I Using the latest kernel headers is not necessary, unless accessto the new kernel features is needed

I The kernel headers are extracted from the kernel sources usingthe headers_install kernel Makefile target.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 66/526

Page 67: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

GCC

I GNU Compiler Collection, the famous freesoftware compiler

I Can compile C, C++, Ada, Fortran, Java,Objective-C, Objective-C++, and generate codefor a large number of CPU architectures,including ARM, AVR, Blackfin, 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 underthe LGPL.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 67/526

Page 68: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

C library

I The C library is an essential component ofa Linux system

I Interface between the applications andthe kernel

I Provides the well-known standard C APIto ease application development

I Several C libraries are available:glibc, uClibc, eglibc, dietlibc, newlib, etc.

I The choice of the C library must be madeat the time of the cross-compilingtoolchain generation, as the GCC compileris compiled against a specific C library.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 68/526

Page 69: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Cross-compiling toolchains

C Libraries

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 69/526

Page 70: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

glibc

I License: LGPL

I C library from the GNU project

I Designed for performance, standardscompliance 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

Page 71: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

uClibc

I License: LGPLI Lightweight C library for small embedded systems

I High configurability: many features can be enabled or disabledthrough a menuconfig interface

I Works only with Linux/uClinux, works on most embeddedarchitectures

I No guaranteed binary compatibility. May need to recompileapplications when the library configuration changes.

I Focus on size rather than performanceI 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

Page 72: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

uClibc (2)

I Most of the applications compile with uClibc. This applies toall 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-inheritancemutexes, NPTL support is very new, fixed Name ServiceSwitch functionality, etc.

I Used on a large number of production embedded products,including consumer electronic devices

I Supported by all commercial embedded Linux providers (proofof maturity).

I Warning: though some development is still happening, themaintainers 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

Page 73: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

eglibc

I Embedded glibc, LGPL license too

I Was a variant of glibc, better adapted to theneeds of embedded systems, because of pastdisagreement with glibc maintainers.

I eglibc’s goals included reduced footprint,configurable components, better support forcross-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 haschanged and its features are now included inglibc. Version 2.19 (Feb. 2014) was the lastrelease.

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

Page 74: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Honey, I shrunk the programs!

I Executable size comparison on ARM, tested with eglibc 2.15and uClibc 0.9.33.2

I Plain “hello world” program (stripped):helloworld static dynamic

uClibc 18kB 2.5kBuClibc with Thumb-2 14kB 2.4kBeglibc with Thumb-2 361kB 2.7kB

I Busybox (stripped):busybox static dynamic

uClibc 750kB 603kBuClibc with Thumb-2 533kB 439kBeglibc with Thumb-2 934kB 444kB

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 74/526

Page 75: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Other smaller C libraries

I Several other smaller C libraries have been developed, butnone of them have the goal of allowing the compilation oflarge existing applications

I They need specially written programs and applicationsI Choices:

I Dietlibc, http://www.fefe.de/dietlibc/. Approximately70 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

Page 76: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Cross-compiling toolchains

Toolchain Options

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 76/526

Page 77: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

ABI

I When building a toolchain, the ABI used to generate binariesneeds to be defined

I ABI, for Application Binary Interface, defines the callingconventions (how function arguments are passed, how thereturn value is passed, how system calls are made) and theorganization 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 EABII 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

Page 78: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Floating point support

I Some processors have a floating point unit, some others donot.

I For example, many ARMv4 and ARMv5 CPUs do not have afloating point unit. Since ARMv7, a VFP unit is mandatory.

I For processors having a floating point unit, the toolchainshould generate hard float code, in order to use the floatingpoint instructions directly

I For processors without a floating point unit, two solutionsI Generate hard float code and rely on the kernel to emulate the

floating point instructions. This is very slow.I Generate soft float code, so that instead of generating floating

point instructions, calls to a user space library are generated

I Decision taken at toolchain configuration time

I Also possible to configure which floating point unit should beused

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 78/526

Page 79: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

CPU optimization flags

I A set of cross-compiling tools is specific to a CPUarchitecture (ARM, x86, MIPS, PowerPC)

I However, with the -march=, -mcpu=, -mtune= options, onecan 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 noother -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’tprevent 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

Page 80: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

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

Page 81: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Building a toolchain manually

Building a cross-compiling toolchain by yourself is a difficult andpainful task! Can take days or weeks!

I Lots of details to learn: many components to build,complicated configuration

I Lots of decisions to make (such as C library version, ABI,floating point mechanisms, component versions)

I Need kernel headers and C library sources

I Need to be familiar with current gcc issues and patches onyour platform

I Useful to be familiar with building and configuring tools

I See the Crosstool-NG docs/ directory for details on howtoolchains are built.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 81/526

Page 82: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Get a pre-compiled toolchain

I Solution that many people chooseI Advantage: it is the simplest and most convenient solutionI Drawback: you can’t fine tune the toolchain to your needs

I Determine what toolchain you need: CPU, endianism, Clibrary, component versions, ABI, soft float or hard float, etc.

I Check whether the available toolchains match yourrequirements.

I Possible choicesI Sourcery CodeBench toolchainsI Linaro toolchainsI 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

Page 83: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Sourcery CodeBench

I CodeSourcery was a company with an extended expertise onfree software toolchains: gcc, gdb, binutils and glibc. It hasbeen bought by Mentor Graphics, which continues to providesimilar services and products

I They sell toolchains with support, but they also provide a”Lite” version, which is free and usable for commercialproducts

I They have toolchains available forI ARMI MIPSI PowerPCI SuperHI x86

I Be sure to use the GNU/Linux versions. The EABI versionsare 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

Page 84: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

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 modified sources of stable releases ofgcc, with these optimizations for ARM (mainly forrecent Cortex A CPUs).

I As any gcc release, these sources can be used by buildtools to build their own binary toolchains (Buildroot,OpenEmbedded...) This allows to support glibc, uClibcand 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

Page 85: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

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 whereveryou want.

I Then, add the path to toolchain binaries in your PATH:export PATH=/path/to/toolchain/bin/:$PATH

I Finally, compile your applicationsPREFIX-gcc -o foobar foobar.c

I PREFIX depends on the toolchain configuration, and allows todistinguish cross-compilation tools from native compilationutilities

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 85/526

Page 86: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Toolchain building utilities

Another solution is to use utilities that automate the process ofbuilding the toolchain

I Same advantage as the pre-compiled toolchains: you don’tneed to mess up with all the details of the build process

I But also offers more flexibility in terms of toolchainconfiguration, component version selection, etc.

I They also usually contain several patches that fix knownissues with the different components on some architectures

I Multiple tools with identical principle: shell scripts or Makefilethat automatically fetch, extract, configure, compile andinstall the different components

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 86/526

Page 87: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Toolchain building utilities (2)

I Crosstool-ngI Rewrite of the older Crosstool, with a menuconfig-like

configuration systemI Feature-full: supports uClibc, glibc, eglibc, hard and soft float,

many architecturesI Actively maintainedI http://crosstool-ng.org/

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 87/526

Page 88: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Toolchain building utilities (3)

Many root filesystem build systems also allow the construction of across-compiling toolchain

I BuildrootI Makefile-based, has a Crosstool-NG back-end, maintained by

the communityI http://www.buildroot.net

I PTXdistI Makefile-based, uClibc or glibc, maintained mainly by

PengutronixI http://pengutronix.de/software/ptxdist/

I OpenEmbedded / YoctoI A featureful, but more complicated build systemI 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

Page 89: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Crosstool-NG: installation and usage

I Installation of Crosstool-NG can be done system-wide, or justlocally in the source directory. For local installation:

./configure --enable-local

make

make install

I Some sample configurations for various architectures areavailable in samples, they can be listed using

./ct-ng list-samples

I To load a sample configuration

./ct-ng <sample-name>

I To adjust the configuration

./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

Page 90: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

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 containingI The C library and related libraries, compiled for the targetI The C library headers and kernel headers

I There is one sysroot for each variant: toolchains can bemultilib if they have several copies of the C library for differentconfigurations (for example: ARMv4T, ARMv5T, etc.)

I CodeSourcery ARM toolchain are multilib, the sysroots are inarm-none-linux-gnueabi/libc/,arm-none-linux-gnueabi/libc/armv4t/,arm-none-linux-gnueabi/libc/thumb2

I Crosstool-NG toolchains can be multilib too (stillexperimental), otherwise the sysroot is inarm-unknown-linux-uclibcgnueabi/sysroot

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 90/526

Page 91: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Practical lab - Using Crosstool-NG

Time to build your toolchain

I Configure Crosstool-NG

I Run it to build your owncross-compiling toolchain

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 91/526

Page 92: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Bootloaders

BootloadersFree Electrons

© Copyright 2004-2014, Free Electrons.Creative Commons BY-SA 3.0 license.Corrections, suggestions, contributions and translations are welcome!

Embedded LinuxDevelopers

Free Electrons

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 92/526

Page 93: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Bootloaders

Boot Sequence

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 93/526

Page 94: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Bootloaders

I The bootloader is a piece of code responsible forI Basic hardware initializationI Loading of an application binary, usually an operating system

kernel, from flash storage, from the network, or from anothertype of non-volatile storage.

I Possibly decompression of the application binaryI Execution of the application

I Besides these basic functions, most bootloaders provide a shellwith various commands implementing different 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

Page 95: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Bootloaders on x86 (1)

I The x86 processors are typically bundled on aboard with a non-volatile memory containing aprogram, the BIOS.

I This program gets executed by the CPU afterreset, and is responsible for basic hardwareinitialization and loading of a small piece of codefrom non-volatile storage.

I This piece of code is usually the first 512 bytesof a storage device

I This piece of code is usually a 1st stagebootloader, which will load the full bootloaderitself.

I The bootloader can then offer all its features. Ittypically understands filesystem formats so thatthe kernel file can be loaded directly from anormal filesystem.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 95/526

Page 96: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Bootloaders on x86 (2)

I GRUB, Grand Unified Bootloader, the most powerful one.http://www.gnu.org/software/grub/

I Can read many filesystem formats to load the kernel image andthe configuration, provides a powerful shell with variouscommands, 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

Page 97: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Booting on embedded CPUs: case 1

I When powered, the CPU starts executing codeat a fixed address

I There is no other booting mechanism providedby the CPU

I The hardware design must ensure that a NORflash chip is wired so that it is accessible at theaddress at which the CPU starts executinginstructions

I The first stage bootloader must be programmedat this address in the NOR

I NOR is mandatory, because it allows randomaccess, which NAND doesn’t allow

I Not very common anymore (unpractical, andrequires NOR flash)

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 97/526

Page 98: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Booting on embedded CPUs: case 2

I The CPU has an integrated boot code in ROMI BootROM on AT91 CPUs, “ROM code” on OMAP, etc.I Exact details are CPU-dependent

I This boot code is able to load a first stage bootloader from astorage device into an internal SRAM (DRAM not initializedyet)

I Storage device can typically be: MMC, NAND, SPI flash,UART, etc.

I The first stage bootloader isI Limited in size due to hardware constraints (SRAM size)I Provided either by the CPU vendor or through community

projects

I This first stage bootloader must initialize DRAM and otherhardware devices and load a second stage bootloader intoRAM

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 98/526

Page 99: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Booting on ARM Atmel AT91

I RomBoot: tries to find a valid bootstrap imagefrom various storage sources, and load it intoSRAM (DRAM not initialized yet). Size limitedto 4 KB. No user interaction possible in standardboot mode.

I AT91Bootstrap: runs from SRAM. Initializes theDRAM, the NAND or SPI controller, and loadsthe secondary bootloader into RAM and starts it.No user interaction possible.

I U-Boot: runs from RAM. Initializes some otherhardware devices (network, USB, etc.). Loads thekernel image from storage or network to RAMand starts it. Shell with commands provided.

I Linux Kernel: runs from RAM. Takes over thesystem completely (bootloaders no longer exists).

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 99/526

Page 100: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Booting on ARM TI OMAP3

I ROM Code: tries to find a valid bootstrap imagefrom various storage sources, and load it intoSRAM or RAM (RAM can be initialized by ROMcode through a configuration header). Sizelimited to <64 KB. No user interaction possible.

I X-Loader or U-Boot: runs from SRAM.Initializes the DRAM, the NAND or MMCcontroller, and loads the secondary bootloaderinto RAM and starts it. No user interactionpossible. File called MLO.

I U-Boot: runs from RAM. Initializes some otherhardware devices (network, USB, etc.). Loads thekernel image from storage or network to RAMand starts it. Shell with commands provided. Filecalled u-boot.bin or u-boot.img.

I Linux Kernel: runs from RAM. Takes over thesystem completely (bootloaders no longer exists).

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 100/526

Page 101: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Booting on Marvell SoC

I ROM Code: tries to find a valid bootstrap imagefrom various storage sources, and load it intoRAM. The RAM configuration is described in aCPU-specific header, prepended to the bootloaderimage.

I U-Boot: runs from RAM. Initializes some otherhardware devices (network, USB, etc.). Loads thekernel image from storage or network to RAMand starts it. Shell with commands provided. Filecalled u-boot.kwb.

I Linux Kernel: runs from RAM. Takes over thesystem completely (bootloaders no longer exists).

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 101/526

Page 102: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Generic bootloaders for embedded CPUs

I We will focus on the generic part, the main bootloader,offering 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 DenxThe most used on ARM, also used on PPC, MIPS, x86, m68k,NIOS, etc. The de-facto standard nowadays. We will study itin detail.http://www.denx.de/wiki/U-Boot

I Barebox, a new architecture-neutral bootloader, written as asuccessor of U-Boot. Better design, better code, activedevelopment, but doesn’t yet have as much hardware supportas U-Boot.http://www.barebox.org

I There are also a lot of other open-source or proprietarybootloaders, often architecture-specific

I RedBoot, Yaboot, PMON, etc.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 102/526

Page 103: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Bootloaders

The U-boot bootloader

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 103/526

Page 104: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

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 athttp://www.denx.de/wiki/U-Boot/Documentation

I The latest development source code is available in a Gitrepository:http://git.denx.de/?p=u-boot.git;a=summary

I Development and discussions happen around an openmailing-list http://lists.denx.de/pipermail/u-boot/

I Since the end of 2008, it follows a fixed-interval releaseschedule. 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

Page 105: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

U-Boot configuration

I Get the source code from the website, and uncompress itI The include/configs/ directory contains one configuration

file for each supported boardI It defines the CPU type, the peripherals and their

configuration, the memory mapping, the U-Boot features thatshould be compiled in, etc.

I It is a simple .h file that sets C pre-processor constants. Seethe README file for the documentation of these constants. Thisfile can also be adjusted to add or remove features fromU-Boot (commands, etc.).

I Assuming that your board is already supported by U-Boot,there should be one entry corresponding to your board in theboards.cfg file.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 105/526

Page 106: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

U-Boot configuration file 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

Page 107: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Configuring and compiling U-Boot

I U-Boot must be configured before being compiledI make BOARDNAME_configI Where BOARDNAME is the name of the board, as visible in the

boards.cfg file (first column).

I Make sure that the cross-compiler is available in PATH

I Compile U-Boot, by specifying the cross-compiler prefix.Example, if your cross-compiler executable is arm-linux-gcc:make CROSS_COMPILE=arm-linux-

I The main result is a u-boot.bin file, which is the U-Bootimage. Depending on your specific platform, there may beother 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

Page 108: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Installing U-Boot

I U-Boot must usually be installed in flash memory to beexecuted by the hardware. Depending on the hardware, theinstallation of U-Boot is done in a different way:

I The CPU provides some kind of specific boot monitor withwhich you can communicate through serial port or USB usinga specific protocol

I The CPU boots first on removable media (MMC) beforebooting from fixed media (NAND). In this case, boot fromMMC to reflash a new version

I U-Boot is already installed, and can be used to flash a newversion of U-Boot. However, be careful: if the new version ofU-Boot doesn’t work, the board is unusable

I The board provides a JTAG interface, which allows to write tothe flash memory remotely, without any system running on theboard. It also allows to rescue a board if the bootloaderdoesn’t work.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 108/526

Page 109: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

U-boot prompt

I Connect the target to the host through a serial consoleI 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 offers a set of commands. We will study themost important ones, see the documentation for a completereference or the help command.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 109/526

Page 110: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Information commands

Flash information (NOR and SPI flash)

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 flash 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

Page 111: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Important commands (1)

I The exact set of commands depends on the U-Bootconfiguration

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 givenaddress in RAM

I ext2load, loads a file from an ext2 filesystem to RAMI And also ext2ls to list files, ext2info for information

I fatload, loads a file from a FAT filesystem to RAMI And also fatls and fatinfo

I tftp, loads a file 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

Page 112: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Important commands (2)

I loadb, loads, loady, load a file from the serial line to RAM

I usb, to initialize and control the USB subsystem, mainly usedfor USB storage devices such as USB keys

I mmc, to initialize and control the MMC subsystem, used forSD and microSD cards

I nand, to erase, read and write contents to NAND flash

I erase, protect, cp, to erase, modify protection and write toNOR flash

I md, displays memory contents. Can be useful to check thecontents loaded in memory, or to look at hardware registers.

I mm, modifies memory contents. Can be useful to modifydirectly hardware registers, for testing purposes.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 112/526

Page 113: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Environment variables commands (1)

I U-Boot can be configured through environment variables,which affect the behavior of the different commands.

I Environment variables are loaded from flash to RAM atU-Boot startup, can be modified and saved back to flash forpersistence

I There is a dedicated location in flash (or in MMC storage) tostore the U-Boot environment, defined in the boardconfiguration file

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 113/526

Page 114: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

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 flash

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 114/526

Page 115: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

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

Page 116: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Important U-Boot env variables

I bootcmd, contains the command that U-Boot willautomatically execute at boot time after a configurable 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 willcontact 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 runsbootcmd

I autostart, if yes, U-Boot starts automatically an image thathas been loaded into memory

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 116/526

Page 117: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Scripts in environment variables

I Environment variables can contain small scripts, to executeseveral commands and test the results of commands.

I Useful to automate booting or upgrade processesI Several commands can be chained using the ; operatorI Tests can be done using

if command ; then ... ; else ... ; fiI Scripts are executed using run <variable-name>I You can reference other variables using ${variable-name}

I ExampleI 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

Page 118: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Transferring files to the target

I U-Boot is mostly used to load and boot a kernel image, but italso allows to change the kernel image and the root filesystemstored in flash.

I Files must be exchanged between the target and thedevelopment 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 thefastest and most efficient solution.

I Through a USB key, if U-Boot supports the USB controller ofyour platform

I Through a SD or microSD card, if U-Boot supports the MMCcontroller 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

Page 119: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

TFTP

I Network transfer from the development workstation andU-Boot on the target takes place through TFTP

I Trivial File Transfer ProtocolI Somewhat similar to FTP, but without authentication and over

UDP

I A TFTP server is needed on the development workstationI sudo apt-get install tftpd-hpaI All files in /var/lib/tftpboot are then visible through TFTPI A TFTP client is available in the tftp-hpa package, for

testing

I A TFTP client is integrated into U-BootI Configure the ipaddr and serverip environment variablesI Use tftp <address> <filename> to load a file

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 119/526

Page 120: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

U-boot mkimage

I The kernel image that U-Boot loads and boots must beprepared, so that a U-Boot specific header is added in front ofthe image

I This header gives details such as the image size, the expectedload 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 configure U-Boot for anyboard of any architecture and compile it. Then installmkimage:cp tools/mkimage /usr/local/bin/

I The special target uImage of the kernel Makefile can then beused 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

Page 121: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Practical lab - U-Boot

Time to start the practical lab!

I Communicate with the board usinga serial console

I Configure, build and install U-Boot

I Learn U-Boot commands

I Set up TFTP communication withthe board

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 121/526

Page 122: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Linux kernel introduction

Linux kernelintroductionFree Electrons

© Copyright 2004-2014, Free Electrons.Creative Commons BY-SA 3.0 license.Corrections, suggestions, contributions and translations are welcome!

Embedded LinuxDevelopers

Free Electrons

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 122/526

Page 123: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Linux kernel introduction

Linux features

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 123/526

Page 124: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

History

I The Linux kernel is one component of a system, which alsorequires libraries and applications to provide features to endusers.

I The Linux kernel was created as a hobby in 1991 by a Finnishstudent, Linus Torvalds.

I Linux quickly started to be used as the kernel for free softwareoperating systems

I Linus Torvalds has been able to create a large and dynamicdeveloper and user community around Linux.

I Nowadays, more than one thousand people contribute to eachkernel 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

Page 125: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Linux kernel key features

I Portability and hardwaresupport. Runs on mostarchitectures.

I Scalability. Can run onsuper computers as well ason tiny devices (4 MB ofRAM is enough).

I Compliance to standardsand interoperability.

I Exhaustive networkingsupport.

I Security. It can’t hide itsflaws. Its code is reviewedby many experts.

I Stability and reliability.

I Modularity. Can includeonly what a system needseven at run time.

I Easy to program. You canlearn from existing code.Many useful resources onthe net.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 125/526

Page 126: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Linux kernel in the system

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 126/526

Page 127: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Linux kernel main roles

I Manage all the hardware resources: CPU, memory, I/O.

I Provide a set of portable, architecture and hardwareindependent APIs to allow user space applications andlibraries to use the hardware resources.

I Handle concurrent accesses and usage of hardwareresources from different applications.

I Example: a single network interface is used by multiple userspace applications through various network connections. Thekernel 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

Page 128: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

System calls

I The main interface between the kernel and user space is theset of system calls

I About 300 system calls that provide the main kernel servicesI File and device operations, networking operations,

inter-process communication, process management, memorymapping, timers, threads, synchronization primitives, etc.

I This interface is stable over time: only new system calls canbe added by the kernel developers

I This system call interface is wrapped by the C library, anduser space applications usually never make a system calldirectly 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

Page 129: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Pseudo filesystems

I Linux makes system and kernel information available in userspace through pseudo filesystems, sometimes also calledvirtual filesystems

I Pseudo filesystems allow applications to see directories andfiles that do not exist on any real storage: they are createdand updated on the fly by the kernel

I The two most important pseudo filesystems areI proc, usually mounted on /proc:

Operating system related information (processes, memorymanagement 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

Page 130: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Inside the Linux kernel

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 130/526

Page 131: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Linux license

I The whole Linux sources are Free Software released under theGNU 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 andredistribute them.

I When you produce Linux based devices, you must release thesources to the recipient, with the same rights, with norestriction.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 131/526

Page 132: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

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 architecturesExamples: 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

Page 133: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Linux kernel introduction

Linux versioning scheme anddevelopment process

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 133/526

Page 134: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Until 2.6 (1)

I One stable major branch every 2 or 3 yearsI Identified by an even middle numberI Examples: 1.0.x, 2.0.x, 2.2.x, 2.4.x

I One development branch to integrate new functionalities andmajor changes

I Identified by an odd middle numberI Examples: 2.1.x, 2.3.x, 2.5.xI 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

Page 135: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Until 2.6 (2)

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 135/526

Page 136: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Changes since Linux 2.6

I Since 2.6.0, kernel developers have been able to introducelots of new features one by one on a steady pace, withouthaving to make disruptive changes to existing subsystems.

I Since then, there has been no need to create a newdevelopment branch massively breaking compatibility with thestable branch.

I Thanks to this, more features are released to users at afaster pace.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 136/526

Page 137: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

3.x stable branch

I From 2003 to 2011, the official kernel versions were named2.6.x.

I Linux 3.0 was released in July 2011I This is only a change to the numbering scheme

I Official 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 effectively 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

Page 138: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

New development model

Using merge and bug fixing windows

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 138/526

Page 139: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

New development model - Details

I After the release of a 3.x version (for example), a two-weeksmerge window opens, during which major additions aremerged.

I The merge window is closed by the release of test version3.(x+1)-rc1

I The bug fixing period opens, for 6 to 10 weeks.

I At regular intervals during the bug fixing period,3.(x+1)-rcY test versions are released.

I When considered sufficiently stable, kernel 3.(x+1) isreleased, and the process starts again.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 139/526

Page 140: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

More stability for the kernel source tree

I Issue: bug and security fixes only releasedfor most recent stable kernel versions.

I Some people need to have a recent kernel,but with long term support for securityupdates.

I You could get long term support from acommercial embedded Linux provider.

I You could reuse sources for the kernelused in Ubuntu Long Term Supportreleases (5 years of free security updates).

I The http://kernel.org front pageshows which versions will be supported forsome time (up to 2 or 3 years), and whichones 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

Page 141: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

What’s new in each Linux release?

I The official list of changes for each Linux release is just ahuge list of individual patches!commit aa6e52a35d388e730f4df0ec2ec48294590cc459Author: Thomas Petazzoni <[email protected]>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 outputthat is used to notify that an overcurrent situation is takingplace. This digital outputs are typically connected to GPIO inputs ofthe processor and can be used to be notified of those overcurrentsituations.

Therefore, we add a new overcurrent_pin[] array in the at91_usbh_datastructure so that boards can tell the AT91 OHCI driver which pins areused for the overcurrent notification, and an overcurrent_supportedboolean to tell the driver whether overcurrent is supported or not.

The code has been largely borrowed from ohci-da8xx.c andohci-s3c2410.c.

Signed-off-by: Thomas Petazzoni <[email protected]>Signed-off-by: Nicolas Ferre <[email protected]>

I Very difficult to find out the key changes and to get the globalpicture out of individual changes.

I Fortunately, there are some useful resources availableI http://wiki.kernelnewbies.org/LinuxChangesI http://lwn.netI 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

Page 142: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

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

Page 143: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Location of kernel sources

I The official versions of the Linux kernel, as released by LinusTorvalds, are available at http://www.kernel.org

I These versions follow the development model of the kernelI However, they may not contain the latest development from a

specific area yet. Some features in development might not beready for mainline inclusion yet.

I Many chip vendors supply their own kernel sourcesI Focusing on hardware support firstI Can have a very important delta with mainline LinuxI Useful only when mainline hasn’t caught up yet.

I Many kernel sub-communities maintain their own kernel, withusually 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 official 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

Page 144: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Getting Linux sources

I The kernel sources are available fromhttp://kernel.org/pub/linux/kernel as full tarballs(complete kernel sources) and patches (differences betweentwo kernel versions).

I However, more and more people use the git version controlsystem. Absolutely needed for kernel development!

I Fetch the entire kernel sources and historygit clone git://git.kernel.org/pub/scm/linux/

kernel/git/torvalds/linux.gitI Create a branch that starts at a specific stable version

git checkout -b <name-of-branch> v3.11I 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

Page 145: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Linux kernel size (1)

I Linux 3.10 sources:Raw size: 573 MB (43,000 files, approx 15,800,000 lines)gzip compressed tar archive: 105 MBbzip2 compressed tar archive: 83 MB (better)xz compressed tar archive: 69 MB (best)

I Minimum Linux 2.6.29 compiled kernel size withCONFIG_EMBEDDED, for a kernel that boots a QEMU PC (IDEhard drive, ext2 filesystem, ELF executable support):532 KB (compressed), 1325 KB (raw)

I Why are these sources so big?Because they include thousands of device drivers, manynetwork protocols, support many architectures andfilesystems...

I The Linux core (scheduler, memory management...) is prettysmall!

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 145/526

Page 146: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

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

Page 147: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Getting Linux sources

I Full tarballsI Contain the complete kernel sources: long to download and

uncompress, but must be done at least onceI 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 versionsI It assumes you already have a base version and you apply the

correct patches in the right order. Quick to download andapply

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 inhttp://kernel.org/pub/linux/kernel/

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 147/526

Page 148: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Patch

I A patch is the difference between two source treesI 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

Page 149: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Contents of a patch

I One section per modified file, starting with a headerdiff -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 modified part of the file, starting withheader with the affected line numbers@@ -1,7 +1,7 @@

I Three lines of context before the changeVERSION = 2

PATCHLEVEL = 6

SUBLEVEL = 11

I The change itself-EXTRAVERSION =

+EXTRAVERSION = .1

I Three lines of context after the changeNAME=Woozy Numbat

# *DOCUMENTATION*

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 149/526

Page 150: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Using the patch command

The patch command:

I Takes the patch contents on its standard input

I Applies the modifications described by the patch into thecurrent directory

patch usage examples:

I patch -p<n> < diff_file

I cat diff_file | patch -p<n>

I xzcat diff_file.xz | patch -p<n>

I bzcat diff_file.bz2 | patch -p<n>

I zcat diff_file.gz | patch -p<n>

I Notes:I n: number of directory levels to skip in the file pathsI You can reverse apply a patch with the -R optionI 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

Page 151: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

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 fixes to the current stable version

(from 3.x to 3.x.y)

I Can be downloaded in gzip, bzip2 or xz (much smaller)compressed files.

I Always produced for n=1(that’s what everybody does... do it too!)

I Need to run the patch command inside the kernel sourcedirectory

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

Page 152: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

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

Page 153: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Linux kernel introduction

Kernel configuration

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 153/526

Page 154: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Kernel configuration and build system

I The kernel configuration and build system is based onmultiple Makefiles

I One only interacts with the main Makefile, present at thetop directory of the kernel source tree

I Interaction takes placeI using the make tool, which parses the MakefileI through various targets, defining which action should be done

(configuration, compilation, installation, etc.). Runmake help to see all available targets.

I ExampleI 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

Page 155: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Kernel configuration (1)

I The kernel contains thousands of device drivers, filesystemdrivers, network protocols and other configurable items

I Thousands of options are available, that are used toselectively compile parts of the kernel source code

I The kernel configuration is the process of defining the set ofoptions with which you want your kernel to be compiled

I The set of options dependsI On your hardware (for device drivers, etc.)I On the capabilities you would like to give to your kernel

(network capabilities, filesystems, real-time, etc.)

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 155/526

Page 156: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Kernel configuration (2)

I The configuration is stored in the .config file at the root ofkernel sources

I Simple text file, 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 file, and show the same set of options

I To modify a kernel in a GNU/Linux distribution: theconfiguration files are usually released in /boot/, togetherwith 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

Page 157: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Kernel or module?

I The kernel image is a single file, resulting from the linkingof all object files that correspond to features enabled in theconfiguration

I This is the file that gets loaded in memory by the bootloaderI All included features are therefore available as soon as the

kernel starts, at a time where no filesystem exists

I Some features (device drivers, filesystems, etc.) can howeverbe compiled as modules

I Those are plugins that can be loaded/unloaded dynamically toadd/remove features to the kernel

I Each module is stored as a separate file in the filesystem,and therefore access to a filesystem is mandatory to usemodules

I This is not possible in the early boot procedure of the kernel,because no filesystem is available

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 157/526

Page 158: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Kernel option types

I There are different types of optionsI bool options, they are either

I true (to include the feature in the kernel) orI false (to exclude the feature from the kernel)

I tristate options, they are eitherI true (to include the feature in the kernel image) orI module (to include the feature as a kernel module) orI false (to exclude the feature)

I int options, to specify integer valuesI hex options, to specify hexadecimal valuesI 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

Page 159: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Kernel option dependencies

I There are dependencies between kernel options

I For example, enabling a network driver requires the networkstack to be enabled

I Two types of dependenciesI depends on dependencies. In this case, option A that depends

on option B is not visible until option B is enabledI select dependencies. In this case, with option A depending

on option B, when option A is enabled, option B isautomatically enabled

I make xconfig allows to see all options, even those thatcannot be selected because of missing dependencies. In thiscase, they are displayed in gray

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 159/526

Page 160: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

make xconfig

make xconfig

I The most common graphical interface to configure the kernel.

I Make sure you readhelp -> introduction: useful options!

I File browser: easier to load configuration files

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

Page 161: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

make xconfig screenshot

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 161/526

Page 162: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

make xconfig search interface

Looks for a keyword in the parameter name. Allows to select orunselect found parameters.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 162/526

Page 163: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Kernel configuration options

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 163/526

Page 164: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Corresponding .config file excerpt

Options are grouped by sections and are prefixed with CONFIG_.

#

# CD-ROM/DVD Filesystems

#

CONFIG_ISO9660_FS=m

CONFIG_JOLIET=y

CONFIG_ZISOFS=y

CONFIG_UDF_FS=y

CONFIG_UDF_NLS=y

#

# DOS/FAT/NT Filesystems

#

# CONFIG_MSDOS_FS is not set

# CONFIG_VFAT_FS is not set

CONFIG_NTFS_FS=m

# CONFIG_NTFS_DEBUG is not set

CONFIG_NTFS_RW=y

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 164/526

Page 165: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

make gconfig

make gconfig

I GTK based graphicalconfiguration interface.Functionality similar to thatof make xconfig.

I Just lacking a searchfunctionality.

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

Page 166: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

make menuconfig

make menuconfig

I Useful when no graphics areavailable. Pretty convenienttoo!

I Same interface found inother 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

Page 167: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

make nconfig

make nconfig

I A newer, similar textinterface

I More user friendly (forexample, easier to accesshelp 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

Page 168: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

make oldconfig

make oldconfig

I Needed very often!

I Useful to upgrade a .config file from an earlier kernel release

I Issues warnings for configuration parameters that no longerexist in the new kernel.

I Asks for values for new parameters

If you edit a .config file by hand, it’s strongly recommended torun make oldconfig afterwards!

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 168/526

Page 169: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Undoing configuration changes

A frequent problem:

I After changing several kernel configuration settings, yourkernel no longer works.

I If you don’t remember all the changes you made, you can getback to your previous configuration:$ cp .config.old .config

I All the configuration interfaces of the kernel (xconfig,menuconfig, oldconfig...) keep this .config.old backupcopy.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 169/526

Page 170: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Configuration per architecture

I The set of configuration options is architecture dependentI Some configuration options are very architecture-specificI Most of the configuration options (global kernel options,

network subsystem, filesystems, most of the device drivers) arevisible in all architectures.

I By default, the kernel build system assumes that the kernel isbeing built for the host architecture, i.e. native compilation

I The architecture is not defined inside the configuration, but ata higher level

I We will see later how to override this behaviour, to allow theconfiguration of kernels for a different architecture

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 170/526

Page 171: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Linux kernel introduction

Compiling and installing the kernelfor the host system

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 171/526

Page 172: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Kernel compilation

I makeI in the main kernel source directoryI Remember to run multiple jobs in parallel if you have multiple

CPU cores. Example: make -j 4I No need to run as root!

I GeneratesI vmlinux, the raw uncompressed kernel image, at the ELF

format, useful for debugging purposes, but cannot be bootedI arch/<arch>/boot/*Image, the final, usually compressed,

kernel image that can be bootedI bzImage for x86, zImage for ARM, vmImage.gz for Blackfin,

etc.

I arch/<arch>/boot/dts/*.dtb, compiled Device Tree files(on some architectures)

I All kernel modules, spread over the kernel source tree, as .ko

files.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 172/526

Page 173: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Kernel installation

I make installI Does the installation for the host system by default, so needs

to be run as root. Generally not used when compiling for anembedded system, and it installs files on the developmentworkstation.

I InstallsI /boot/vmlinuz-<version>

Compressed kernel image. Same as the one inarch/<arch>/boot

I /boot/System.map-<version>

Stores kernel symbol addressesI /boot/config-<version>

Kernel configuration for this version

I Typically re-runs the bootloader configuration utility to takethe new kernel into account.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 173/526

Page 174: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Module installation

I make modules_installI 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) files, in the same directorystructure 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 dependenciesI 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

Page 175: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Kernel cleanup targets

I Clean-up generated files (to forcere-compilation):make clean

I Remove all generated files. Needed whenswitching from one architecture to another.Caution: it also removes your .config file!make mrproper

I Also remove editor backup and patch reject files(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

Page 176: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

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

Page 177: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Cross-compiling the kernel

When you compile a Linux kernel for another CPU architecture

I Much faster than compiling natively, when the target systemis much slower than your GNU/Linux workstation.

I Much easier as development tools for your GNU/Linuxworkstation are much easier to find.

I To make the difference with a native compiler, cross-compilerexecutables are prefixed by the name of the target system,architecture and sometimes library. Examples:mips-linux-gcc, the prefix is mips-linux-

arm-linux-gnueabi-gcc, the prefix is arm-linux-gnueabi-

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 177/526

Page 178: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Specifying cross-compilation (1)

The CPU architecture and cross-compiler prefix are defined throughthe ARCH and CROSS_COMPILE variables in the toplevel Makefile.

I ARCH is the name of the architecture. It is defined by thename 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 prefix of the cross compilation toolsI 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

Page 179: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Specifying cross-compilation (2)

Two solutions to define 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 whenyou run any make command, causing your build andconfiguration to be screwed up.

I Define 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 file that you source everytime you start working on the project. If you only work on asingle architecture with always the same toolchain, you couldeven put these settings in your ~/.bashrc file to make thempermanent and visible from any terminal.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 179/526

Page 180: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Predefined configuration files

I Default configuration files available, per board or per-CPUfamily

I They are stored in arch/<arch>/configs/, and are justminimal .config files

I This is the most common way of configuring a kernel forembedded platforms

I Run make help to find if one is available for your platform

I To load a default configuration file, just runmake acme_defconfig

I This will overwrite your existing .config file!

I To create your own default configuration fileI make savedefconfig, to create a minimal configuration fileI 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

Page 181: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Configuring the kernel

I After loading a default configuration file, you can adjust theconfiguration to your needs with the normal xconfig,gconfig or menuconfig interfaces

I You can also start the configuration from scratch withoutloading a default configuration file

I As the architecture is different from your host architectureI Some options will be different from the native configuration

(processor and architecture specific options, specific drivers,etc.)

I Many options will be identical (filesystems, network protocols,architecture-independent drivers, etc.)

I Make sure you have the support for the right CPU, the rightboard and the right device drivers.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 181/526

Page 182: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Device Tree

I Many embedded architectures have a lot of non-discoverablehardware.

I Depending on the architecture, such hardware is eitherdescribed using C code directly within the kernel, or using aspecial hardware description language in a Device Tree.

I ARM, PowerPC, OpenRISC, ARC, Microblaze are examples ofarchitectures using the Device Tree.

I A Device Tree Source, written by kernel developers, iscompiled into a binary Device Tree Blob, passed at boot timeto the kernel.

I There is one different Device Tree for each board/platformsupported by the kernel, available inarch/arm/boot/dts/<board>.dtb.

I The bootloader must load both the kernel image and theDevice 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

Page 183: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Building and installing the kernel

I Run make

I Copy the final kernel image to the target storageI can be uImage, zImage, vmlinux, bzImage in

arch/<arch>/bootI 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, asthe kernel image is a single file, easy to handle

I It is however possible to customize the make install behaviourin arch/<arch>/boot/install.sh

I make modules_install is used even in embeddeddevelopment, as it installs many modules and description files

I make INSTALL_MOD_PATH=<dir>/ modules_installI The INSTALL_MOD_PATH variable is needed to install the

modules in the target root filesystem instead of your host rootfilesystem.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 183/526

Page 184: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

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. Itis done automatically by the kernel make uImage target.

I On some ARM platforms, make uImage requires passing aLOADADDR environment variable, which indicates at whichphysical memory address the kernel will be executed.

I In addition to the kernel image, U-Boot can also pass aDevice Tree Blob to the kernel.

I The typical boot process is therefore:

1. Load zImage or uImage at address X in memory2. Load <board>.dtb at address Y in memory3. 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

Page 185: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Kernel command line

I In addition to the compile time configuration, the kernelbehaviour can be adjusted with no recompilation using thekernel command line

I The kernel command line is a string that defines variousarguments to the kernel

I It is very important for system configurationI root= for the root filesystem (covered later)I console= for the destination of kernel messagesI Many more exist. The most important ones are documented in

Documentation/kernel-parameters.txt in kernel sources.

I This kernel command line is eitherI Passed by the bootloader. In U-Boot, the contents of the

bootargs environment variable is automatically passed to thekernel

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

Page 186: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Practical lab - Kernel cross-compiling

I Set up the cross-compilingenvironment

I Configure the kernel Makefileaccordingly

I Cross-compile the kernel for an arm

platform

I On this platform, interact with thebootloader and boot your kernel

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 186/526

Page 187: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

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

Page 188: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

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 timeinitializing devices and kernel features that you only need later.

I Caution: once loaded, have full control and privileges in thesystem. 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

Page 189: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Module dependencies

I Some kernel modules can depend on other modules, whichneed to be loaded first.

I Example: the usb-storage module depends on thescsi_mod, libusual and usbcore modules.

I Dependencies are described in/lib/modules/<kernel-version>/modules.dep

This file 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

Page 190: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Kernel log

When a new module is loaded, related information is available inthe kernel log.

I The kernel keeps its messages in a circular buffer (so that itdoesn’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 filtered 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 "<n>Debug info" > /dev/kmsg

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 190/526

Page 191: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

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 moduleobject file must be given.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 191/526

Page 192: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Understanding module loading issues

I When loading a module fails, insmod often doesn’t give youenough 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

Page 193: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Module utilities (2)

I sudo modprobe <module_name>

Most common usage of modprobe: tries to load all themodules the given module depends on, and then this module.Lots of other options are available. modprobe automaticallylooks in /lib/modules/<version>/ for the object filecorresponding to the given module name.

I lsmod

Displays the list of loaded modulesCompare 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

Page 194: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

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 (forexample, no more processes opening a device file)

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

Page 195: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

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 file in/etc/modprobe.d/:options snd-intel8x0m index=-2

I Through the kernel command line, when the driver is builtstatically into the kernel:snd-intel8x0m.index=-2

I snd-intel8x0m is the driver nameI index is the driver parameter nameI -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

Page 196: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Check module parameter values

How to find the current values for the parameters of a loadedmodule?

I Check /sys/module/<name>/parameters.

I There is one file per parameter, containing the parametervalue.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 196/526

Page 197: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Useful reading

Linux Kernel in a Nutshell, Dec 2006

I By Greg Kroah-Hartman, O’Reillyhttp://www.kroah.com/lkn/

I A good reference book and guide onconfiguring, compiling and managing theLinux kernel sources.

I Freely available on-line!Great companion to the printed book foreasy electronic searches!Available as single PDF file onhttp://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

Page 198: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Linux Root Filesystem

Linux RootFilesystemFree Electrons

© Copyright 2004-2014, Free Electrons.Creative Commons BY-SA 3.0 license.Corrections, suggestions, contributions and translations are welcome!

Embedded LinuxDevelopers

Free Electrons

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 198/526

Page 199: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

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

Page 200: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Filesystems

I Filesystems are used to organize data in directories and fileson storage devices or on the network. The directories and filesare organized as a hierarchy

I In Unix systems, applications and users see a single globalhierarchy of files and directories, which can be composed ofseveral filesystems.

I Filesystems are mounted in a specific location in thishierarchy of directories

I When a filesystem is mounted in a directory (called mountpoint), the contents of this directory reflects the contents ofthe storage device

I When the filesystem is unmounted, the mount point is emptyagain.

I This allows applications to access files 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

Page 201: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

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

Page 202: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

mount / umount

I mount allows to mount filesystemsI mount -t type device mountpointI type is the type of filesystemI device is the storage device, or network location to mountI mountpoint is the directory where files of the storage device

or network location will be accessibleI mount with no arguments shows the currently mounted

filesystems

I umount allows to unmount filesystemsI This is needed before rebooting, or before unplugging a USB

key, because the Linux kernel caches writes in memory toincrease performance. umount makes sure that those writesare committed to the storage.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 202/526

Page 203: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Root filesystem

I A particular filesystem is mounted at the root of the hierarchy,identified by /

I This filesystem is called the root filesystemI As mount and umount are programs, they are files inside a

filesystem.I They are not accessible before mounting at least one

filesystem.

I As the root filesystem is the first mounted filesystem, itcannot 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 filesystem is available, the kernel panicsPlease 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

Page 204: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Location of the root filesystem

I It can be mounted from different locationsI From the partition of a hard diskI From the partition of a USB keyI From the partition of an SD cardI From the partition of a NAND flash chip or similar type of

storage deviceI From the network, using the NFS protocolI From memory, using a pre-loaded filesystem (by the

bootloader)I etc.

I It is up to the system designer to choose the configuration forthe system, and configure the kernel behaviour with root=

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 204/526

Page 205: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Mounting rootfs from storage devices

I Partitions of a hard disk or USB keyI root=/dev/sdXY, where X is a letter indicating the device,

and Y a number indicating the partitionI /dev/sdb2 is the second partition of the second disk drive

(either USB key or ATA hard drive)

I Partitions of an SD cardI root=/dev/mmcblkXpY, where X is a number indicating the

device and Y a number indicating the partitionI /dev/mmcblk0p2 is the second partition of the first device

I Partitions of flash storageI root=/dev/mtdblockX, where X is the partition numberI /dev/mtdblock3 is the fourth partition of a NAND flash chip

(if only one NAND flash chip is present)

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 205/526

Page 206: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Mounting rootfs over the network (1)

Once networking works, your root filesystem could be a directoryon your GNU/Linux development host, exported by NFS (NetworkFile System). This is very convenient for system development:

I Makes it very easy to update files on the root filesystem,without rebooting. Much faster than through the serial port.

I Can have a big root filesystem even if you don’t have supportfor internal or external storage yet.

I The root filesystem can be huge. You can even build nativecompiler tools and build all the tools you need on the targetitself (better to cross-compile though).

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 206/526

Page 207: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Mounting rootfs over the 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 file:/home/tux/rootfs 192.168.1.111(rw,no_root_squash,no_subtree_check)

I 192.168.1.111 is the client IP addressI 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

Page 208: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Mounting rootfs over the network (3)

I On the target systemI The kernel must be compiled with

I CONFIG_NFS_FS=y (NFS support)I CONFIG_IP_PNP=y (configure IP at boot 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

Page 209: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Mounting rootfs over the network (4)

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 209/526

Page 210: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

rootfs in memory: initramfs (1)

I It is also possible to have the root filesystem integrated intothe kernel image

I It is therefore loaded into memory together with the kernelI This mechanism is called initramfs

I It integrates a compressed archive of the filesystem into thekernel image

I Variant: the compressed archive can also be loaded separatelyby the bootloader.

I It is useful for two casesI Fast booting of very small root filesystems. As the filesystem is

completely loaded at boot time, application startup is very fast.I As an intermediate step before switching to a real root

filesystem, located on devices for which drivers not part of thekernel image are needed (storage drivers, filesystem drivers,network drivers). This is always used on the kernel ofdesktop/server distributions to keep the kernel image sizereasonable.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 210/526

Page 211: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

rootfs in memory: initramfs (2)

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 211/526

Page 212: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

rootfs in memory: initramfs (3)

I The contents of an initramfs are defined at the kernelconfiguration level, with the CONFIG_INITRAMFS_SOURCEoption

I Can be the path to a directory containing the root filesystemcontents

I Can be the path to a cpio archiveI Can be a text file describing the contents of the initramfs

(see documentation for details)

I The kernel build process will automatically take the contentsof the CONFIG_INITRAMFS_SOURCE option and integrate theroot filesystem into the kernel 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

Page 213: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Linux Root Filesystem

Contents

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 213/526

Page 214: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Root filesystem organization

I The organization of a Linux root filesystem in terms ofdirectories is well-defined by the Filesystem HierarchyStandard

I http://www.linuxfoundation.org/collaborate/

workgroups/lsb/fhs

I Most Linux systems conform to this specificationI Applications expect this organizationI It makes it easier for developers and users as the filesystem

organization is similar in all systems

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 214/526

Page 215: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Important directories (1)

/bin Basic programs

/boot Kernel image (only when the kernel is loaded from afilesystem, not common on non-x86 architectures)

/dev Device files (covered later)

/etc System-wide configuration

/home Directory for 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 filesystem

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 215/526

Page 216: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Important directories (2)

/root Home directory of the root user

/sbin Basic system programs

/sys Mount point of the sysfs virtual filesystem

/tmp Temporary files

/usr /usr/bin Non-basic programs/usr/lib Non-basic libraries

/usr/sbin Non-basic system programs

/var Variable data files. This includes spool directoriesand files, administrative and logging data, andtransient and temporary files

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 216/526

Page 217: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Separation of programs and libraries

I Basic programs are installed in /bin and /sbin and basiclibraries 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 mountedover the network, through NFS

I In order to allow the system to boot when the network wasdown, some binaries and libraries are stored in /bin, /sbinand /lib

I /bin and /sbin contain programs like ls, ifconfig, cp,bash, etc.

I /lib contains the C library and sometimes a few other basiclibraries

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

Page 218: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Linux Root Filesystem

Device Files

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 218/526

Page 219: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Devices

I One of the kernel important role is to allow applications toaccess hardware devices

I In the Linux kernel, most devices are presented to user spaceapplications through two different abstractions

I Character deviceI Block device

I Internally, the kernel identifies each device by a triplet ofinformation

I Type (character or block)I Major (typically the category of device)I Minor (typically the identifier of the device)

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 219/526

Page 220: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Types of devices

I Block devicesI A device composed of fixed-sized blocks, that can be read and

written to store dataI Used for hard disks, USB keys, SD cards, etc.

I Character devicesI Originally, an infinite stream of bytes, 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 buffersI 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

Page 221: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Devices: everything is a file

I A very important Unix design decision was to represent mostof the “system objects” as files

I It allows applications to manipulate all “system objects” withthe normal file API (open, read, write, close, etc.)

I So, devices had to be represented as files to the applications

I This is done through a special artifact called a device file

I It is a special type of file, that associates a file name visible touser space applications to the triplet (type, major, minor) thatthe kernel understands

I All device files are by convention stored in the /dev directory

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 221/526

Page 222: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Device files examples

Example of device files in a Linux 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 file API to write data to aserial 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

Page 223: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Creating device files

I On a basic Linux system, the device files have to be createdmanually using the mknod command

I mknod /dev/<device> [c|b] major minorI Needs root privilegesI Coherency between device files and devices handled by the

kernel is left to the system developer

I On more elaborate Linux systems, mechanisms can be addedto create/remove them automatically when devices appearand disappear

I devtmpfs virtual filesystem, since kernel 2.6.32I udev daemon, solution used by desktop and server Linux

systemsI 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

Page 224: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Linux Root Filesystem

Pseudo Filesystems

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 224/526

Page 225: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

proc virtual filesystem

I The proc virtual filesystem exists since the beginning of LinuxI It allows

I The kernel to expose statistics about running processes in thesystem

I The user to adjust at runtime various system parameters aboutprocess management, memory management, etc.

I The proc filesystem is used by many standard user spaceapplications, and they expect it to be mounted in /proc

I Applications such as ps or top would not work without theproc filesystem

I Command to mount /proc:mount -t proc nodev /proc

I Documentation/filesystems/proc.txt in the kernelsources

I man proc

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 225/526

Page 226: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

proc contents

I One directory for each running process in the systemI /proc/<pid>I cat /proc/3840/cmdlineI It contains details about the files opened by the 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 lineI /proc/sys contains many files that can be written to to

adjust kernel parametersI 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

Page 227: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

sysfs filesystem

I The sysfs filesystem is a feature integrated in the 2.6 Linuxkernel

I It allows to represent in user space the vision that the kernelhas of the buses, devices and drivers in the system

I It is useful for various user space applications that need to listand 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

Page 228: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Linux Root Filesystem

Minimal filesystem

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 228/526

Page 229: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Basic applications

I In order to work, a Linux system needs at least a fewapplications

I An init application, which is the first user space applicationstarted by the kernel after mounting the root filesystem

I The kernel tries to run /sbin/init, /bin/init, /etc/initand /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 bootprocess is stopped.

I The init application is responsible for starting all other userspace applications and services

I Usually a shell, to allow a user to interact with the systemI Basic Unix applications, to copy files, move files, list files

(commands like mv, cp, mkdir, cat, etc.)I Those basic components have to be integrated into the root

filesystem to make it usable

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 229/526

Page 230: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Overall booting process

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 230/526

Page 231: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Overall booting process with initramfs

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 231/526

Page 232: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Busybox

BusyboxFree Electrons

© Copyright 2004-2014, Free Electrons.Creative Commons BY-SA 3.0 license.Corrections, suggestions, contributions and translations are welcome!

Embedded LinuxDevelopers

Free Electrons

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 232/526

Page 233: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Why Busybox?

I A Linux system needs a basic set of programs to workI An init programI A shellI Various basic utilities for file manipulation and system

configuration

I In normal Linux systems, those programs are provided bydifferent projects

I coreutils, bash, grep, sed, tar, wget, modutils, etc. areall different projects

I A lot of different components to integrateI Components not designed with embedded systems constraints

in mind: they are not very configurable and have a wide rangeof features

I Busybox is an alternative solution, extremely common onembedded systems

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 233/526

Page 234: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

General purpose toolbox: BusyBox

I Rewrite of many useful Unix command line utilitiesI Integrated into a single project, which makes it easy to work

withI Designed with embedded systems in mind: highly configurable,

no unnecessary features

I All the utilities are compiled into a single executable,/bin/busybox

I Symbolic links to /bin/busybox are created for eachapplication integrated into Busybox

I For a fairly featureful configuration, less than 500 KB(statically compiled with uClibc) or less than 1 MB (staticallycompiled 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

Page 235: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

BusyBox commands!

Commands available 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

Page 236: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Applet highlight: Busybox init

I Busybox provides an implementation of an init program

I Simpler than the init implementation found on desktop/serversystems: no runlevels are implemented

I A single configuration file: /etc/inittabI Each line has the form <id>::<action>:<process>

I Allows to run services at startup, and to make sure thatcertain services are always running on the system

I See examples/inittab in Busybox for details on theconfiguration

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 236/526

Page 237: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

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 viversion!

I Tip: you can learn vi on the desktop, by running thevimtutor command.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 237/526

Page 238: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Configuring BusyBox

I Get the latest stable sources from http://busybox.net

I Configure BusyBox (creates a .config file):I make defconfig

Good to begin with BusyBox.Configures BusyBox with all options for regular users.

I make allnoconfig

Unselects all options. Good to configure only what you need.

I make xconfig (graphical, needs the libqt3-mt-dev

package)or make menuconfig (text)Same configuration interfaces as the ones used by the Linuxkernel (though older versions are used).

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 238/526

Page 239: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

BusyBox make xconfig

You can choose:

I the commandsto compile,

I and even thecommandoptions andfeatures thatyou need!

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 239/526

Page 240: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Compiling BusyBox

I Set the cross-compiler prefix in the configuration interface:BusyBox Settings -> Build Options -

> Cross Compiler prefix

Example: arm-linux-

I Set the installation directory in the configuration interface:BusyBox Settings -> Installation Options -

> BusyBox installation prefix

I Add the cross-compiler path to the PATH environmentvariable:export PATH=/usr/xtools/arm-unknown-linux-

uclibcgnueabi/bin:$PATH

I Compile BusyBox:make

I Install it (this creates a Unix directory structure symbolic linksto the busybox executable):make install

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 240/526

Page 241: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Practical lab - A tiny embedded system

I Make Linux boot on a directory onyour workstation, shared by NFS

I Create and configure a minimalisticLinux 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

Page 242: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Block filesystems

Block filesystemsFree Electrons

© Copyright 2004-2014, Free Electrons.Creative Commons BY-SA 3.0 license.Corrections, suggestions, contributions and translations are welcome!

Embedded LinuxDevelopers

Free Electrons

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 242/526

Page 243: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Block vs. flash

I Storage devices are classified in two main types: blockdevices and flash devices

I They are handled by different subsystems and differentfilesystems

I Block devices can be read and written to on a per-blockbasis, without erasing.

I Hard disks, floppy disks, RAM disksI USB keys, Compact Flash, SD card: these are based on flash

storage, but have an integrated controller that emulates ablock device, managing and erasing flash sectors in atransparent way.

I Flash devices can be read, but writing requires erasing, andoften occurs on a larger size than the “block” size.

I NOR flash, NAND flash

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 243/526

Page 244: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Block device list

I The list of all block devices available in the system can befound 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

Page 245: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Traditional block filesystems

Traditional filesystems

I Can be left in a non-coherent state after a system crash orsudden poweroff, which requires a full filesystem check afterreboot.

I ext2: traditional Linux filesystem(repair it with fsck.ext2)

I vfat: traditional Windows filesystem(repair it with fsck.vfat on GNU/Linux or Scandisk onWindows)

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 245/526

Page 246: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Journaled filesystems

I Designed to stay in acorrect state evenafter system crashesor a sudden poweroff

I All writes are firstdescribed in thejournal before beingcommitted to files

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 246/526

Page 247: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Filesystem recovery after crashes

I Thanks to thejournal, the filesystemis never left in acorrupted state

I Recently saved datacould still be lost

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 247/526

Page 248: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Journaled block filesystems

Journaled filesystems

I ext3: ext2 with journal extensionext4: the new generation with many improvements.Ready for production. They are the default filesystems for allLinux systems in the world.

I The Linux kernel supports many other filesystems: reiserFS,JFS, XFS, etc. Each of them have their own characteristics,but are more oriented towards server or scientific workloads

I Btrfs (“Butter FS”)The next generation. Great performance. In mainline but stillexperimental.

We recommend ext2 for very small partitions (< 5 MB), becauseother filesystems need too much space for metadata (ext3 andext4 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

Page 249: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Creating ext2/ext3/ext4 volumes

I To create an empty ext2/ext3/ext4 filesystem on a blockdevice or inside an already-existing image file

I mkfs.ext2 /dev/hda3I mkfs.ext3 /dev/sda2I mkfs.ext4 /dev/sda3I mkfs.ext2 disk.img

I To create a filesystem image from a directory containing allyour files and directories

I Use the genext2fs tool, from the package of the same nameI genext2fs -d rootfs/ rootfs.imgI 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

Page 250: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Mounting filesystem images

I Once a filesystem image has been created, one can access andmodifies its contents from the development workstation, usingthe 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 thecontents of the rootfs.img file.

I This is possible thanks to loop, which is a kernel driver thatemulates a block device with the contents of a file.

I Do not forget to run umount before using the filesystemimage!

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 250/526

Page 251: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

F2FS

http://en.wikipedia.org/wiki/F2FS

I Filesystem optimized for block devices based on NAND flash

I Available in the mainline Linux kernel

I Benchmarks: best performer on flash 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

Page 252: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Squashfs

Squashfs: http://squashfs.sourceforge.net

I Read-only, compressed filesystem for block devices. Fine forparts of a filesystem which can be 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 onembedded systems with slow CPUs (at the expense of aslightly degraded compression rate)

I Now supports XZ algorithm, for a much better compressionrate, at the expense of higher CPU usage and time.

Benchmarks: (roughly 3 times smaller than ext3, and 2-4 timesfaster)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

Page 253: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Squashfs - How to use

I Need to install the squashfs-tools packageI Creation of the image

I On your workstation, create your filesystem image:mksquashfs rootfs/ rootfs.sqfs

I Caution: if the image already exists remove it first,or use the -noappend option.

I Installation of the imageI Let’s assume your partition on the target is in /dev/sdc1I Copy the filesystem image on the device

dd if=rootfs.sqfs of=/dev/sdc1

Be careful when using dd to not overwrite the incorrectpartition!

I Mount your filesystem:mount -t squashfs /dev/sdc1 /mnt/root

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 253/526

Page 254: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

tmpfs

Not a block filesystem of course!Perfect to store temporary data in RAM: system log files,connection data, temporary files...

I tmpfs configuration:File systems -> Pseudo filesystems

Lives in the Linux file cache. Doesn’t waste RAM: unlikeramdisks, no need to copy files to the file cache, grows andshrinks to accommodate stored files. Saves RAM: can swapout pages to disk when needed.

I How to use: choose a name to distinguish the various tmpfsinstances 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

Page 255: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Mixing read-only and read-write filesystems

Good idea to split your block storage into:

I A compressed read-only partition(Squashfs)Typically used for the root filesystem(binaries, kernel...).Compression saves space. Read-onlyaccess protects your system from mistakesand data corruption.

I A read-write partition with a journaledfilesystem (like ext3)Used to store user or configuration data.Guarantees filesystem integrity afterpower off or crashes.

I Ram storage for temporary files (tmpfs)

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 255/526

Page 256: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Practical lab - Block filesystems

I Creating partitions on your blockstorage

I Booting your system with a mix offilesystems: SquashFS for the rootfilesystem (including applications),ext3 for configuration and userdata, and tmpfs for temporarysystem files.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 256/526

Page 257: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Flash filesystems

Flash filesystemsFree Electrons

© Copyright 2004-2014, Free Electrons.Creative Commons BY-SA 3.0 license.Corrections, suggestions, contributions and translations are welcome!

Embedded LinuxDevelopers

Free Electrons

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 257/526

Page 258: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

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

Page 259: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

MTD devices

I MTD devices are visible in /proc/mtd

I The mtdchar driver creates a character device for each MTDdevice of the system

I Usually named /dev/mtdX or /dev/mtdXro, major 90. Evenminors for read-write access (/dev/mtdX), odd minors forread-only access (/dev/mtdXro)

I Provide ioctl() to erase and manage the flashI Used by the mtd-utils utilities

I The mtdblock driver creates a block device for each MTDdevice of the system

I Usually named /dev/mtdblockX, major 31. Minor is thenumber of the MTD device

I Allows read/write block-level access. But bad blocks are nothandled, and no wear leveling is done for writes.

I Primary use: mounting filesystems

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 259/526

Page 260: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

MTD partitioning

I MTD devices are usually partitionedI It allows to use different areas of the flash for different

purposes: read-only filesystem, read-write filesystem, backupareas, bootloader area, kernel area, etc.

I Unlike block devices, which contains their own partition table,the partitioning of MTD devices is described externally

I Specified in the board Device TreeI Hard-coded into the kernel code (if no Device Tree)I Specified through the kernel command line

I Each partition becomes a separate MTD deviceI Different from block device labeling (hda3, sda2)I /dev/mtd1 is either the second partition of the first flash

device, or the first partition of the second flash device

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 260/526

Page 261: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Definition of MTD partitions (1)

The Device Tree is the standard place to device MTD partitions forboards 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

Page 262: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Definition of MTD partitions (2)

For boards or platforms that do not use the Device Tree, MTDpartitions can be defined in the kernel. Legacy example fromarch/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

Page 263: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Modifying MTD partitions (1)

I MTD partitions can fortunately be defined through the kernelcommand line.

I First need to find the name of the MTD device. Look at thekernel log at boot time. In the below example, the MTDdevice 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

Page 264: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

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 defined 6 partitions in 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

Page 265: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Modifying MTD partitions (3)

I Partition sizes must be multiple of the erase block size.You can use sizes in hexadecimal too. Remember the belowsizes: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

Page 266: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

mtd-utils

I A set of utilities to manipulate MTD devicesI mtdinfo to get detailed information about an MTD deviceI flash_eraseall to completely erase a given MTD deviceI flashcp to write to NOR flashI nandwrite to write to NAND flashI UBI utilitiesI Flash filesystem image creation tools: mkfs.jffs2,

mkfs.ubifs

I Usually available as the mtd-utils package in yourdistribution

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

Page 267: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

jffs2

I Today’s standard filesystem for MTD flash

I Nice features: on the fly compression(saves storage space and reduces I/O),power down reliable, wear-leveling andECC.

I Drawbacks: doesn’t scale wellI Mount time depending on filesystem

size: the kernel has to scan the wholefilesystem at mount time, to read whichblock belongs to each file.

I Need to use theCONFIG_JFFS2_SUMMARY kernel optionto store such information in flash. Thisdramatically reduces mount time (from16 s to 0.8 s for a 128 MB partition).

I http://www.linux-

mtd.infradead.org/doc/jffs2.htmlFree Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 267/526

Page 268: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

jffs2 - How to 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 jffs2: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 jffs2 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

Page 269: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

How to create a jffs2 image

I mkfs.jffs2 command available in the mtd-utils package.Caution: unlike some mkfs commands, it doesn’t create afilesystem, but a filesystem image.

I First, find the erase block 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 jffs2 image contentsuntil the end of the final erase block.

I It is fine if the jffs2 image is smaller than the MTD partition.The jffs2 file system will use the entire partition anyway.

I The --no-cleanmarkers option is for NAND flash only.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 269/526

Page 270: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Mounting a jffs2 image on your host

Useful to edit jffs2 images on your development systemMounting an MTD device as a loop device is a bit complex task.Here’s an example for jffs2, for your reference:

I First find the erase block size used to create the jffs2 image.Let’s assume it is 256KiB (262144 bytes).

I Create a block device from the imagelosetup /dev/loop0 root.jffs2

I Emulate an MTD device from a block device,using the block2mtd kernel modulemodprobe block2mtd block2mtd=/dev/loop0,262144

I Load the mtdblock driver if neededmodprobe mtdblock

I Finally, mount the filesystem (create /mnt/jffs2 if 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

Page 271: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Initializing jffs2 partitions from U-boot

You may not want to have mtd-utils on your target!

I Create a JFFS2 image on your workstationI In the U-Boot prompt:

I Download the jffs2 image to RAM with tftp

Or copy this image to RAM from external storage(U-boot understands FAT filesystems and supports USBstorage)

I Flash it inside an MTD partition(exact instructions depending on flash type, NOR or NAND,reuse the instructions used to flash your kernel). Make sure towrite only the size of the image, not more!

I Limitation: need to split the jffs2 image in several chunksif bigger than the RAM size.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 271/526

Page 272: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Booting from a jffs2 root partition

I To boot on a jffs2 root filesystem, addroot=/dev/mtdblock<x> and rootfstype=jffs2 to theLinux command line arguments.

I The Linux kernel cannot autodetect the type of a rootfilesystem in MTD flash. That’s why the rootfstype kernelparameter is needed.

I For root partitions on a block device, using rootfstype (forexample: rootfstype=ext3) can reduce boot time a littlebit. This way, the kernel doesn’t have to try all the blockfilesystems it supports.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 272/526

Page 273: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

yaffs2

I Mainly supports NAND flash

I No compression

I Wear leveling, ECC, power failureresistant

I Fast boot time

I Code available separately through git(Dual GPL / Proprietary licensefor 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

Page 274: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

yaffs2 - How to use

I Erase a partition:flash_eraseall /dev/mtd2

I The filesystem is automatically formatted at the first mount:mount -t yaffs2 /dev/mtdblock2 /mnt/flash

I Images can be created with the mkyaffs tool, fromyaffs-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

Page 275: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

UBI (1)

Unsorted Block 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 writesacross all physical blocks.

I Takes care of managing the erase blocks and wear leveling.Makes filesystems easier to implement.

I Wear leveling can operate on the whole storage, not only onindividual partitions (strong advantage).

I Volumes can be dynamically resized or, on the opposite, canbe read-only (static).

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 275/526

Page 276: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

UBI (2)

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 276/526

Page 277: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

UBIFS

http://www.linux-

mtd.infradead.org/doc/ubifs.html

I The next generation of the jffs2filesystem, from the same linux-mtddevelopers.

I Works on top of UBI volumes

I Has a noticeable metadata overhead onvery small partitions (4M, 8M)

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 277/526

Page 278: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

UBI layout example

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 278/526

Page 279: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

UBI - Preparation

I Have /dev/ mounted as a devtmpfs filesystem

I Erase your flash partition while preserving your erase countersubiformat /dev/mtd1

Seehttp://www.linux-mtd.infradead.org/faq/ubi.html ifyou 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 thefull 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

Page 280: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

UBI - Volume management

I Volume creation with ubimkvolI ubimkvol /dev/ubi0 -N test -s 116MiBI ubimkvol /dev/ubi0 -N test -m (max available size)I The volume is then identified as ubi0:test for the

mount/umount commands

I Volume removal with ubirmvolI ubirmvol /dev/ubi0 -N test

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 280/526

Page 281: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

UBIFS - How to use

I When a UBI volume is created, creating an empty UBIFSfilesystem is just a matter of mounting it

I mount -t ubifs ubi0:test /mnt/flash

I Images of UBIFS filesystems can be created using themkfs.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

Page 282: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Ubinize (1)

After creating images of UBIFS filesystems, images of a full UBIspace, 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 configuration file describing the UBI 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

Page 283: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Ubinize (2)

I ubinize -o ubi.img -p 256KiB -m 4096 ubi.iniI 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 creatingUBI and UBIFS images.

I See http://www.linux-

mtd.infradead.org/doc/ubi.html#L_flasher_algo forwhat is required when flashing UBI images containing UBIFSvolumes.

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

Page 284: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

UBIFS - How to prepare a root fs

I Create the UBIFS image for the root filesystem

I Create the UBI device image (with ubinize)

I Flash it from the bootloaderI 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

Page 285: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Flash filesystem benchmarks

http://elinux.org/Flash_Filesystem_Benchmarks

I jffs2I Worst performanceI Requires CONFIG_SUMMARY to have acceptable boot time

I yaffs2I Good performance, but not in mainline Linux

I ubifsI Best solution and performance for medium and big partitionsI 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

Page 286: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Issues with flash-based block storage

I Flash storage made available only through a block interface.

I Hence, no way to access a low level flash interface and use theLinux filesystems doing wear leveling.

I No details about the layer (Flash Translation Layer) they use.Details are kept as trade secrets, and may hide poorimplementations.

I Can use flashbench(https://github.com/bradfa/flashbench) to find outthe erase block size and optimize filesystem formating.

I Not knowing about the wear leveling algorithm, it is highlyrecommended 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

Page 287: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Reducing the number of writes

I Of course, do not use your flash storage as swap area (rare inembedded systems anyway)

I Mount your filesystems as read-only, or use read-onlyfilesystems (SquashFS), whenever possible.

I Keep volatile files in RAM (tmpfs)

I Don’t use the sync mount option (commits writesimmediately). Use the fsync() system call for per-filesynchronization.

I You may decide to do without journaled filesystems. Theycause more writes, but are also much more power downresistant (trade-off).

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 287/526

Page 288: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Useful reading

I Arnd Bergmann: Optimizing Linux with cheap flash drivesIn depth coverage of flash storage with a block interface.http://lwn.net/Articles/428584/

I Managing flash 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

Page 289: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Practical lab - Flash Filesystems

I Creating partitions in your internalflash storage.

I Use a read-only JFFS2 partition forthe system

I Use a read-write JFFS2 partitionfor data

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 289/526

Page 290: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Embedded Linux system development

Embedded LinuxsystemdevelopmentFree Electrons

© Copyright 2004-2014, Free Electrons.Creative Commons BY-SA 3.0 license.Corrections, suggestions, contributions and translations are welcome!

Embedded LinuxDevelopers

Free Electrons

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 290/526

Page 291: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Contents

I Using open-source componentsI Tools for the target device

I NetworkingI System utilitiesI Language interpretersI Audio, video and multimediaI Graphical toolkitsI DatabasesI 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

Page 292: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Embedded Linux system development

Leveraging open-source componentsin an Embedded Linux system

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 292/526

Page 293: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Third party libraries and applications

I One of the advantages of embedded Linux is the wide rangeof third-party libraries and applications that one can leveragein its product

I They are freely available, freely distributable, and thanks totheir open-source nature, they can be analyzed and modifiedaccording to the needs of the project

I However, efficiently re-using these components is not alwayseasy. One must:

I Find these componentsI Choose the most appropriate onesI Cross-compile themI 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

Page 294: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Find existing components

I Free Software Directoryhttp://directory.fsf.org

I Look at other embedded Linux products, and see what theircomponents are

I Look at the list of software packaged by embedded Linuxbuild systems

I These are typically chosen for their suitability to embeddedsystems

I Ask the community or Google

I This presentation will also feature a list of components forcommon needs

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 294/526

Page 295: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Choosing components

I Not all free software components are necessarily good tore-use. One must pay attention to:

I Vitality of the developer and user communities. This vitalityensures long-term maintenance of the component, andrelatively good support. It can be measured by looking at themailing-list traffic and the version control system activity.

I Quality of the component. Typically, if a component isalready available through embedded build systems, and has adynamic user community, it probably means that the quality isrelatively good.

I License. The license of the component must match yourlicensing constraints. For example, GPL libraries cannot beused in proprietary applications.

I Technical requirements. Of course, the component mustmatch your technical requirements. But don’t forget that youcan 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

Page 296: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Licenses (1)

I All software that are under a free software license give fourfreedoms to all users

I Freedom to useI Freedom to studyI Freedom to copyI Freedom to modify and distribute modified copies

I See http://www.gnu.org/philosophy/free-sw.html fora definition of Free Software

I Open Source software, as per the definition of the OpenSource Initiative, are technically similar to Free Software interms of freedoms

I See http://www.opensource.org/docs/osd for thedefinition of Open Source Software

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 296/526

Page 297: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Licenses (2)

I Free Software licenses fall in two main categoriesI The copyleft licensesI The non-copyleft licenses

I The concept of copyleft is to ask for reciprocity in thefreedoms given to a user.

I The result is that when you receive a software under acopyleft free software license and distribute modified versionsof it, you must do so under the same license

I Same freedoms to the new usersI It’s an incentive to contribute back your changes instead of

keeping them secret

I Non-copyleft licenses have no such requirements, andmodified versions can be kept proprietary, but they still requireattribution

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 297/526

Page 298: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

GPL

I GNU General Public LicenseI Covers around 55% of the free software projects

I Including the Linux kernel, Busybox and many applications

I Is a copyleft licenseI Requires derivative works to be released under the same licenseI 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, Busyboxand others)

I More and more programs covered by version 3, released in2007

I Major change for the embedded market: the requirement thatthe user must be able to run the modified versions on thedevice, 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

Page 299: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

GPL: redistribution

I No obligation when the software is not distributedI You can keep your modifications secret until the product

delivery

I It is then authorized to distribute binary versions, if one of thefollowing conditions is met:

I Convey the binary with a copy of the source on a physicalmedium

I Convey the binary with a written offer valid for 3 years thatindicates how to fetch the source code

I Convey the binary with the network address of a locationwhere 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 preservedI See section 4. and 5.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 299/526

Page 300: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

LGPL

I GNU Lesser General Public License

I Covers around 10% of the free software projectsI A copyleft license

I Modified versions must be released under the same licenseI But, programs linked against a library under the LGPL do not

need to be released under the LGPL and can be keptproprietary.

I However, the user must keep the ability to update the libraryindependently from the program. Dynamic linking is theeasiest solution. Statically linked executables are only possibleif the developer provides a way to relink with an update (withsource code or linkable object files).

I Used instead of the GPL for most of the libraries, includingthe 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

Page 301: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Licensing: examples

I You make modifications to the Linux kernel (to add drivers oradapt to your board), to Busybox, U-Boot or other GPLsoftware

I You must release the modified versions under the same license,and be ready to distribute the source code to your customers

I You make modifications to the C library or any other LGPLlibrary

I You must release the modified versions under the same license

I You create an application that relies on LGPL librariesI You can keep your application proprietary, but you must link

dynamically with the LGPL libraries

I You make modifications to a non-copyleft licensed softwareI You can keep your modifications proprietary, but you must still

credit the authors

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 301/526

Page 302: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Non-copyleft licenses

I A large family of non-copyleft licenses that are relativelysimilar in their requirements

I A few examplesI Apache license (around 4%)I BSD license (around 6%)I MIT license (around 4%)I X11 licenseI Artistic license (around 9 %)

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 302/526

Page 303: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

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

Page 304: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Is this free software?

I Most of the free software projects are covered by 10well-known licenses, so it is fairly easy for the majority ofproject to get a good understanding of the license

I Otherwise, read the license text

I Check Free Software Foundation’s opinionhttp://www.fsf.org/licensing/licenses/

I Check Open Source Initiative’s opinionhttp://www.opensource.org/licenses

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 304/526

Page 305: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Respect free software licenses

I Free Software is not public domain software, the distributorshave obligations due to the licenses

I Before using a free software component, make sure the licensematches your project constraints

I Make sure to keep a complete list of the free softwarepackages you use, the original version you used and to keepyour modifications and adaptations well-separated from theoriginal version

I Conform to the license requirements before shipping theproduct to the customers

I Free Software licenses have been enforced successfully incourts. Organizations which can help:

I GPL-violations.org, http://www.gpl-violations.orgI 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

Page 306: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Keeping changes separate (1)

I When integrating existing open-source components in yourproject, it is sometimes needed to make modifications to them

I Better integration, reduced footprint, bug fixes, new features,etc.

I Instead of mixing these changes, it is much better to keepthem separate from the original component version

I If the component needs to be upgraded, easier to know whatmodifications were made to the component

I If support from the community is requested, important toknow how different the component we’re using is from theupstream version

I Makes contributing the changes back to the communitypossible

I It is even better to keep the various changes made on a givencomponent 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

Page 307: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Keeping changes separate (2)

I The simplest solution is to use QuiltI Quilt is a tool that allows to maintain a stack of patches over

source codeI Makes it easy to add, remove modifications from a patch, to

add and remove patches from stack and to update themI The stack of patches can be integrated into your version

control systemI https://savannah.nongnu.org/projects/quilt/

I Another solution is to use a version control systemI Import the original component version into your version

control systemI 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

Page 308: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Embedded Linux system development

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

Page 309: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

ssh server and client: Dropbear

http://matt.ucc.asn.au/dropbear/dropbear.html

I Very small memory footprint ssh server for embedded systems

I Satisfies most needs. Both client and server!

I Size: 110 KB, statically compiled with uClibc on i386.(OpenSSH client and server: approx 1200 KB, dynamicallycompiled with glibc on i386)

I Useful to:I Get a remote console on the target deviceI Copy files to and from the target device (scp or rsync -e ssh).

I An alternative to OpenSSH, used on desktop and serversystems.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 309/526

Page 310: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Benefits of a web 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 computersconnected to the device. No need to support multipleoperating systems!

I Just need to develop static or dynamic HTML pages (possiblywith powerful client-side JavaScript).Easy way of providing access to device information andparameters.

I Reduced hardware costs (no LCD, very little storage spaceneeded)

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 310/526

Page 311: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Web servers

I BusyBox http server: http://busybox.netI Tiny: only adds 9 K to BusyBox (dynamically

linked with glibc on i386, with all featuresenabled.)

I Sufficient features for many devices with a webinterface, including CGI, http authenticationand script support (like PHP, with a separateinterpreter).

I License: GPL

I lighttpd: http://lighttpd.net

Low footprint server good at managing highloads.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

Page 312: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Network utilities (1)

I avahi is an implementation of Multicast DNS ServiceDiscovery, that allows programs to publish and discoverservices on a local network

I bind, a DNS server

I iptables, the user space tools associated to the Linux firewall,Netfilter

I iw and wireless tools, the user space tools associated toWireless devices

I netsnmp, implementation of the SNMP protocol

I openntpd, implementation of the Network Time Protocol, forclock 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

Page 313: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Network utilities (2)

I pppd, implementation of the Point to Point Protocol, usedfor dial-up connections

I samba, implements the SMB and CIFS protocols, used byWindows to share files 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

Page 314: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Embedded Linux system development

Tools for the target device: Systemutilities

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 314/526

Page 315: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

System utilities

I dbus, 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 withoutwriting an in-kernel driver

I Utilities for kernel subsystems: i2c-tools for I2C, input-toolsfor input, mtd-utils for MTD devices, usbutils for USBdevices

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 315/526

Page 316: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Embedded Linux system development

Tools for the target device: Languageinterpreters

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 316/526

Page 317: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Language interpreters

I Interpreters for the most common scripting languages areavailable. Useful for

I Application developmentI Web services developmentI Scripting

I Languages supportedI LuaI PythonI PerlI RubyI TCLI PHP

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 317/526

Page 318: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Embedded Linux system development

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

Page 319: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Audio, video and multimedia

I GStreamer, a multimedia frameworkI Allows to decode/encode a wide variety of codecs.I Supports hardware encoders and decoders through plugins,

proprietary/specific plugins are often provided by SoC vendors.

I alsa-lib, the user space tools associated to the ALSA soundkernel subsystem

I Directly using encoding and decoding libraries, if you decidenot to use GStreamer:libavcodec, libogg, libtheora, libvpx, flac, libvorbis, libmad,libsndfile, speex, etc.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 319/526

Page 320: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Embedded Linux system development

Tools for the target device: Graphicaltoolkits

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 320/526

Page 321: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Embedded Linux system development

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

Page 322: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

DirectFB (1)

I Low-level graphical libraryI Lines, rectangles, triangles drawing and fillingI Blitting, flippingI Text drawingI Windows and transparencyI Image loading and video display

I But also handles input event handling: mouse, keyboard,joystick, touchscreen, etc.

I Provides accelerated graphic operations on a few hardwareplatforms

I Single-application by default, but multiple applications canshare the framebuffer 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

Page 323: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

DirectFB: architecture

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 323/526

Page 324: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

DirectFB: usage (1)

I Multimedia applicationsI For example the Disko framework, for set-top box related

applications

I “Simple” graphical applicationsI Industrial controlI 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

Page 325: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

DirectFB: architecture

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 325/526

Page 326: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

X.org - KDrive

I Stand-alone simplified version of the X server,for embedded systems

I Formerly know as Tiny-XI Kdrive is integrated in the official X.org server

I Works on top of the Linux frame buffer, thanksto the Xfbdev variant of the server

I Real X serverI 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

Page 327: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Kdrive: architecture

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 327/526

Page 328: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Kdrive: usage

I Can be directly programmed using Xlib / XCBI Low-level graphic libraryI Probably doesn’t make sense since DirectFB is a more

lightweight solution for an API of roughly the same level (nowidgets)

I Or, usually used with a toolkit on top of itI GtkI QtI Enlightment Foundation LibrariesI Others: Fltk, WxEmbedded, etc

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 328/526

Page 329: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Wayland

I Intended to be a simpler replacement for X

I Wayland is a protocol for a compositor to talk toits clients as well as a C library implementationof that protocol.

I Weston: a minimal and fast referenceimplementation of a Wayland compositor, and issuitable for many embedded and mobile usecases.

I Still experimental. Porting Gtk and Qt toWayland 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

Page 330: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Wayland: architecture

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 330/526

Page 331: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Embedded Linux system development

Graphical toolkits: “High-level”solutions

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 331/526

Page 332: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Gtk

I The famous toolkit, providingwidget-based high-level APIs to developgraphical applications

I Standard API in C, but bindings exist forvarious languages: C++, Python, etc.

I Works on top of X.org.

I No windowing system, a lightweightwindow manager needed to run severalapplications. 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

Page 333: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Gtk stack components

I Glib, core infrastructureI Object-oriented infrastructure GObjectI 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

Page 334: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

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

Page 335: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Gtk examples (2)

Maemo tablet / phone interfaceGTK 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

Page 336: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Qt (1)

I The other famous toolkit, providing widget-based high-levelAPIs to develop graphical applications

I Implemented in C++I the C++ library is required on the target systemI standard API in C++, but with bindings for other languages

I Works either on top ofI FramebufferI X11I DirectFB back-end integrated in version 4.4, which allows to

take advantage of the acceleration provided by DirectFBdrivers

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 336/526

Page 337: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Qt (2)

I Qt is more than just a graphical toolkit, it also offers acomplete development framework: data structures, threads,network, databases, XML, etc.

I See our presentation Qt for non graphical applicationspresentation at ELCE 2011 (Thomas Petazzoni):http://j.mp/W4PK85

I Qt Embedded has an integrated windowing system, allowingseveral applications to share the same screen

I Very well documented

I Since version 4.5, available under the LGPL, allowingproprietary applications

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 337/526

Page 338: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Qt’s usage

Qt on the Dash Expressnavigation system

Qt on the Netflix player by Roku

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 338/526

Page 339: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Other less frequent solutions

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

Page 340: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Embedded Linux system development

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

Page 341: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Lightweight database - SQLite

http://www.sqlite.org

I SQLite is a small C library that implements a self-contained,embeddable, lightweight, zero-configuration SQL databaseengine

I The database engine of choice for embedded Linux systemsI Can be used as a normal libraryI 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

Page 342: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Embedded Linux system development

Tools for the target device: Webbrowsers

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 342/526

Page 343: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

WebKit

http://webkit.org/

I Web browser engine. Application framework thatcan 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 etAndroid default browsers ... Google Chrome nowuses a fork of its WebCore component). Used bye-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

Page 344: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Embedded Linux system development

Example of components used in realdevices

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 344/526

Page 345: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Industrial applications

I In many industrial applications, the system is only responsiblefor monitoring and control a device

I Such a system is usually relatively simple in terms ofcomponents

I KernelI BusyBoxI C libraryI Applications relying directly on the C library, sometimes using

the real-time capabilities of the Linux kernelI 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

Page 346: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Digital Photo Frame: requirements

I Example taken from a conference of Matt Porter, EmbeddedAlley at ELC 2008

I Hardware: ARM SoC with DSP, audio, 800x600 LCD,MMC/SD, NAND, buttons, speakers

I The photo frame must be able toI Display to the LCDI Detect SD card insertion, notify applications of the insertion so

that applications can build a catalog of the pictures on the SDcard

I Modern 3D GUI with nice transitionsI Navigation through buttonsI 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

Page 347: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Digital Photo Frame: components (1)

I Base systemI Components present in virtually all embedded Linux systemsI The U-Boot bootloaderI Linux Kernel

I Drivers for SD/MMC, framebuffer, sound, input devices

I BusyboxI Build system, in this case was OpenEmbeddedI Components: u-boot, Linux, busybox

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 347/526

Page 348: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Digital Photo Frame: components (2)

I Event handling to detect SD card insertionI udev, that receives events from the kernel, creates device

nodes, and sends events to halI hal, which maintains a database of available devices and

provides a dbus APII dbus to connect hal with the application. The application

subscribes to hal event through dbus and gets notified whenthey are 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

Page 349: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Digital Photo Frame: components (3)

I JPEG displayI libjpeg to decode the picturesI jpegtran to resize and rotate themI FIM (Fbi Improved) for dithering

I MP3 supportI libmad for playingI libid3 for ID3 tags readingI libm3u to support playlistsI 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

Page 350: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Digital Photo Frame: components (4)

I 3D interfaceI Vincent, an open-source implementation of OpenGL ESI Clutter, higher-level API to develop 3D applications

I Application itselfI Manages media eventsI Uses the JPEG libraries to decode and render picturesI Receives Linux input events from buttons and draws

OpenGL-based UI developed with ClutterI Manage a user-defined configurationI Play the music with the MP3-related librariesI Display photo slideshow

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 350/526

Page 351: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Embedded Linux system development

System building

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 351/526

Page 352: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

System building: goal and solutions

I GoalI Integrate all the software components, both third-party and

in-house, into a working root filesystemI It involves the download, extraction, configuration, compilation

and installation of all components, and possibly fixing issuesand adapting configuration files

I Several solutionsI ManuallyI System building toolsI Distributions or ready-made filesystems

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 352/526

Page 353: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

System building: manually

I Manually building a target system involves downloading,configuring, compiling and installing all the components of thesystem.

I All the libraries and dependencies must be configured,compiled and installed in the right order.

I Sometimes, the build system used by libraries or applicationsis not very cross-compile friendly, so some adaptations arenecessary.

I There is no infrastructure to reproduce the build from scratch,which might cause problems if one component needs to bechanged, 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

Page 354: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

System building: manually (2)

I Manual system building is not recommended for productionprojects

I However, using automated tools often requires the developerto dig into specific issues

I Having a basic understanding of how a system can be builtmanually is therefore very useful to fix issues encountered withautomated tools

I We will first study manual system building, and during apractical lab, create a system using this method

I Then, we will study the automated tools available, and use oneof them during a lab

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 354/526

Page 355: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

System foundations

I A basic root file system needs at leastI 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, ashell and other traditional Unix command line tools. This isusually provided by Busybox

I The C library and the related libraries (thread, math, etc.)installed in /lib

I A few configuration files, such as /etc/inittab, andinitialization scripts in /etc/init.d

I On top of this foundation common to most embedded Linuxsystem, 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

Page 356: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Target and build spaces

I The system foundation, Busybox and C library, are the core ofthe target root filesystem

I However, when building other components, one mustdistinguish two directories

I The target space, which contains the target root filesystem,everything that is needed for execution of the application

I The build space, which will contain a lot more files than thetarget space, since it is used to keep everything needed tocompile libraries and applications. So we must keep theheaders, documentation, and other configuration files

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 356/526

Page 357: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Build systems

I Each open-source component comes with a mechanism toconfigure, compile and install it

I A basic MakefileI 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 AutotoolsI 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

Page 358: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Autotools and friends

I A family of tools, which associated together form a completeand extensible build system

I autoconf is used to handle the configuration of the softwarepackage

I automake is used to generate the Makefiles needed to buildthe software package

I pkgconfig is used to ease compilation against already installedshared libraries

I libtool is used to handle the generation of shared libraries in asystem-independent way

I Most of these tools are old and relatively complicated to use,but they are used by a majority of free software packagestoday. One must have a basic understanding of what they doand how they work.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 358/526

Page 359: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

automake / autoconf / autoheader

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 359/526

Page 360: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

automake / autoconf

I Files written by the developerI configure.in describes the configuration options and the

checks done at configure timeI Makefile.am describes how the software should be built

I The configure script and the Makefile.in files aregenerated by autoconf and automake respectively.

I They should never be modified directlyI They are usually shipped pre-generated in the software

package, because there are several versions of autoconf andautomake, and they are not completely compatible

I The Makefile files are generated at configure time, beforecompiling

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

Page 361: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Configuring and compiling: native case

I The traditional steps to configure and compile an autotoolsbased package are

I Configuration of the package./configure

I Compilation of the packagemake

I Installation of the packagemake install

I Additional arguments can be passed to the ./configure

script to adjust the component configuration.

I Only the make install needs to be done as root if theinstallation should take place system-wide

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 361/526

Page 362: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Configuring 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 defined to point tothe proper cross-compilation tools. The host tuple is also bydefault used as prefix.

I configure script 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 definesfrom which location the software will run in the targetenvironment. Usually, /usr is fine.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 362/526

Page 363: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Configuring and compiling: cross case (2)

I If one simply runs make install, the software will beinstalled in the directory passed as --prefix. Forcross-compiling, one must pass the DESTDIR argument tospecify where the software must be installed.

I Making the distinction between the prefix (as passed with--prefix at configure time) and the destination directory (aspassed 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

Page 364: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Installation (1)

I The autotools based software packages provide both ainstall and install-strip make targets, used to installthe software, either stripped or unstripped.

I For applications, the software is usually installed in<prefix>/bin, with configuration files 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 file (a .la file)I The pkgconfig description file in <prefix>/lib/pkgconfigI Include files in <prefix>/include/I Sometimes a <libname>-config program in <prefix>/binI 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

Page 365: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Installation (2)

Contents of usr/lib after installation of libpng and zlib

I libpng libtool description files./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-config description files./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

Page 366: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Installation in the build and target spaces

I From all these files, everything except documentation isnecessary to build an application that relies on libpng.

I These files will go into the build space

I However, only the library .so binaries in <prefix>/lib andsome symbolic links are needed to execute the application onthe target.

I Only these files will go in the target space

I The build space must be kept in order to build otherapplications or recompile existing applications.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 366/526

Page 367: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

pkg-config

I pkg-config is a tool that allows to query a small database toget information on how to compile programs that depend onlibraries

I The database is made of .pc files, installed by default in<prefix>/lib/pkgconfig/.

I pkg-config is used by the configure script to get thelibrary configurations

I It can 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 forthe *.pc files, and assumes that the paths in these files arecorrect.

I PKG_CONFIG_PATH allows to set another location for the *.pc

files and PKG_CONFIG_SYSROOT_DIR to prepend a prefix tothe paths mentioned in the .pc files.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 367/526

Page 368: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Let’s find the libraries

I When compiling an application or a library that relies on otherlibraries, the build process by default looks in /usr/lib forlibraries and /usr/include for headers.

I The first thing to do is to set the CFLAGS and LDFLAGS

environment variables:export CFLAGS=-I/my/build/space/usr/include/

export LDFLAGS=-L/my/build/space/usr/lib

I The libtool files (.la files) must be modified because theyinclude 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 tothe location of the .pc files and thePKG_CONFIG_SYSROOT_DIR variable must be set to the buildspace directory.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 368/526

Page 369: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Practical lab - Manual cross-compiling

I Manually cross-compilingapplications and libraries

I Learning about common techniquesand issues.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 369/526

Page 370: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

System building tools: principle

I Different tools are available to automate the process ofbuilding a target system, including the kernel, and sometimesthe toolchain.

I They automatically download, configure, compile and installall the components in the right order, sometimes afterapplying patches to fix cross-compiling issues.

I They already contain a large number of packages, that shouldfit your main requirements, and are easily extensible.

I The build becomes reproducible, which allows to easily changethe configuration of some components, upgrade them, fixbugs, etc.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 370/526

Page 371: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Available system building tools

Large choice of tools

I Buildroot, developed by the communityhttp://www.buildroot.net

I PTXdist, developed by Pengutronixhttp://pengutronix.de/software/ptxdist/

I OpenWRT, originally a fork of Buildroot for wireless routers, now amore generic projecthttp://www.openwrt.org

I LTIB. Good support for Freescale boards, but small communityhttp://ltib.org/

I OpenEmbedded, more flexible but also far more complicatedhttp://www.openembedded.org, its industrialized version Yoctoand vendor-specific derivatives such as Arago

I Vendor specific tools (silicon vendor or embedded Linux vendor)

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 371/526

Page 372: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Buildroot (1)

I Allows to build a toolchain, a root filesystem image with manyapplications 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 basicutilities to more elaborate software stacks: X.org, GStreamer,Qt, Gtk, WebKit, Python, PHP, etc.

I Good for small to medium embedded systems, with a fixed setof features

I No support for generating packages (.deb or .ipk)I Needs complete rebuild for most configuration changes.

I Active 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

Page 373: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Buildroot (2)

I Configuration takes place througha *config interface similar to thekernelmake menuconfig

I Allows to defineI Architecture and specific CPUI Toolchain configurationI Set of applications and libraries

to integrateI Filesystem images to generateI Kernel and bootloader

configuration

I Build by just runningmake

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 373/526

Page 374: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Buildroot: adding a new package (1)

I A package allows to integrate a user application or library toBuildroot

I Each package has its own directory (such aspackage/gqview). This directory contains:

I A Config.in file (mandatory), describing the configurationoptions for the package. At least one is needed to enable thepackage. This file must be sourced from package/Config.in

I A gqview.mk file (mandatory), describing how the package isbuilt.

I Patches (optional). Each file of the form 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

Page 375: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Buildroot: adding a new package (2)

I For a simple package with a single configuration option toenable/disable it, the Config.in file looks like:

config BR2_PACKAGE_GQVIEW

bool "gqview"

depends on BR2_PACKAGE_LIBGTK2

help

GQview is an image viewer for Unix operating systems

http://prdownloads.sourceforge.net/gqview

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

Page 376: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Buildroot: adding new package (3)

I Create the gqview.mk file to describe the 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 prefix of all variables must beidentical to the suffix of the main configuration optionBR2_PACKAGE_GQVIEW

I The autotools-package infrastructure knows how to buildautotools packages. A more generic generic-package

infrastructure is available for packages not using the autotoolsas their build system.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 376/526

Page 377: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

OpenEmbedded / Yocto

I The most versatile and powerful embedded Linux build system

I A collection of recipes (.bb files)I A tool that processes the recipes: bitbake

I Integrates 2000+ application and libraries, is highlyconfigurable, can generate binary packages to make thesystem customizable, supports multiple versions/variants ofthe same package, no need for full rebuild when theconfiguration is changed.

I Configuration takes place by editing various configuration files

I Good for larger embedded Linux systems, or people lookingfor more configurability and extensibility

I Drawbacks: very steep learning curve, very long first build.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 377/526

Page 378: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Distributions - Debian

Debian GNU/Linux, http://www.debian.org

I Provides the easiest environment for quickly buildingprototypes and developing applications. Countlessruntime and development packages available.

I But brobably too costly to maintain andunnecessarily 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

Page 379: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Distributions - Others

Fedora

I http://fedoraproject.org/wiki/

Architectures/ARM

I Supported on various recent ARM boards(such as Beaglebone Black). Pidorasupports Raspberry Pi too.

I Supports QEMU emulated ARM boardstoo (Versatile Express board)

I Shipping the same version as for desktops!

Ubuntu

I Had some releases for ARM mobilemultimedia devices, but stopped atversion 12.04. Now focusing on ARMservers only.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 379/526

Page 380: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Embedded distributions

Distributions designed for specific types of devices

I Android: http://www.android.com/

Google’s distribution for phones and tablet PCs.Except the Linux kernel, very different user spacethan other Linux distributions. Very successful, lotsof 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

Page 381: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Application frameworks

Not real distributions you can download. Instead, theyimplement middleware running on top of the Linux kerneland 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 TVsand In Vehicle Infotainment devices.Supported by big phone manufacturers and operatorsHTML5 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

Page 382: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

System emulator: QEMU

http://qemu.org

Fast processor emulator

I Useful to develop root filesystems and applications when thehardware is not available yet (binary compatible developmentboards are also possible).

I Emulates the processor and various peripherals (“Systememulation): x86, ppc, arm, sparc, mips, m68k...

I Many different 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

Page 383: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Practical lab - Buildroot

I Rebuild the same system, this timewith 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

Page 384: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Embedded Linux system development

Commercial embedded Linuxsolutions

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 384/526

Page 385: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Commercial embedded Linux solutions

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 tothe community.

I As long as they distribute the sources to their users, the GPLdoesn’t require vendors to share their sources with any thirdparty.

I Graphical toolkits developed by the vendors are usuallyproprietary, trying to make it easier to create and embeddedLinux 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

Page 386: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Commercial solution strengths

I Technical advantagesI Well tested and supported

kernel and tool versionsI Often supporting patches

not supported by themainline kernel yet(example: real-timepatches)

I Complete development toolsets: kernels, toolchains,utilities, binaries forimpressive lists of targetplatforms

I Integrated utilities forautomatic kernel image,initramfs and filesystemgeneration.

I Graphical developmentstools

I Development tools availableon multiple platforms: GNU/ Linux, Solaris, Windows...

I Support servicesI Useful if you don’t have

your own supportresources

I Long term supportcommitment, even forversions considered asobsolete by thecommunity, but not byyour users!

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 386/526

Page 387: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Commercial or community solutions?

Commercial distributions and tool sets

I Best if you don’t have your own support resources and have asufficient budget

I Help focusing on your primary job: making an embeddeddevice.

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 Linuxexpertise, investigate issues by yourselves, and train your ownsupport 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

Page 388: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Embedded Linux application development

Embedded LinuxapplicationdevelopmentFree Electrons

© Copyright 2004-2014, Free Electrons.Creative Commons BY-SA 3.0 license.Corrections, suggestions, contributions and translations are welcome!

Embedded LinuxDevelopers

Free Electrons

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 388/526

Page 389: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Contents

I Application developmentI Developing applications on embedded LinuxI Building your applications

I Source managementI Integrated development environments (IDEs)I Version control systems

I Debugging and analysis toolsI DebuggersI Memory checkersI System analysis

I Development environmentsI Developing on Windows

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 389/526

Page 390: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Embedded Linux application development

Developing applications on embeddedLinux

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 390/526

Page 391: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Application development

I An embedded Linux system is just a normal Linux system,with usually a smaller selection of components

I In terms of application development, developing on embeddedLinux is exactly the same as developing on a desktop Linuxsystem

I All existing skills can be re-used, without any particularadaptation

I All existing libraries, either third-party or in-house, can beintegrated into the embedded Linux system

I Taking into account, of course, the limitation of the embeddedsystems 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

Page 392: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Programming language

I The default programming language for system-levelapplication 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 applicationsI The C++ library must be added to the systemI 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 applicationdevelopment, web applications or scripts

I But they require an interpreter on the embedded system andhave usually higher memory consumption and slightly lowerperformance

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

Page 393: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

C library or higher-level libraries?

I For many applications, the C library already provides arelatively large set of features

I file and device I/O, networking, threads and synchronization,inter-process communication

I Thoroughly described in the glibc manual, or in any Linuxsystem programming book

I However, the API carries a lot of history and is not necessarilyeasy to grasp for new comers

I Therefore, using a higher level framework, such as Qt or theGtk stack, might be a good idea

I These frameworks are not only graphical libraries, their core isseparate from the graphical part

I But of course, these libraries have some memory and storagefootprint, 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

Page 394: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Building your applications

I For simple applications that do not need to be really portableor provide compile-time configuration options, a simpleMakefile will be sufficient

I For more complicated applications, or if you want to be ableto run your application on a desktop Linux PC and on thetarget 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 ownbuild system for applications, called qmake.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 394/526

Page 395: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Simple Makefile (1)

I Case of an application that only uses the C library, containstwo source files and generates a 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

Page 396: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Simple Makefile (2)

I 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)

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 396/526

Page 397: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Embedded Linux application development

Integrated DevelopmentEnvironments (IDE)

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 397/526

Page 398: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

KDevelop

http://kdevelop.org

I A 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 alsoGTK, Gnome, kernel drivers, embedded (Opie)...

I Many features: editor, syntax highlighting, codecompletion, compiler interface, debuggerinterface, file manager, class browser...

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

Page 399: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

KDevelop screenshot

Ruby debugger

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 399/526

Page 400: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Eclipse (1)

http://www.eclipse.org/

I An extensible, plug-in based softwaredevelopment kit, typically used for creating IDEs.

I Supported by the Eclipse foundation, anon-profit consortium of major software industryvendors (IBM, Intel, Borland, Nokia, WindRiver, 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

Page 401: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Eclipse (2)

I Eclipse is actually a platform composed of many projects:http://www.eclipse.org/projects/

I Some projects are dedicated to integrating into Eclipsefeatures useful for embedded developers (cross-compilation,remote development, remote debugging, etc.)

I The platform is used by major embedded Linux softwarevendors for their (proprietary) system development kits:MontaVista DevRocket, TimeSys TimeStorm, Wind RiverWorkbench, Sysgo ELinOS.

I Used by free software build systems and developmentenvironments 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

Page 402: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Other popular solutions

I Many embedded Linux developerssimply use Vim or Emacs. Theycan integrate with debuggers,source code browsers such ascscope, offer syntax highlightingand more.

I Geany is an easy-to-use graphicalcode editor.

I CodeBlocks is also quite popular,since it’s also available on theWindows platform.

All those editors are available in mostLinux distributions, simply install themand try them out!

Vim

Emacs

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 402/526

Page 403: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Embedded Linux application development

Version control systems

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 403/526

Page 404: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

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, orchoose 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 conflicts with differentbranches (merging)

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 404/526

Page 405: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Traditional version control systems

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 longerexists in the open-source community.

I Should no longer be used for new projectsI http:

//en.wikipedia.org/wiki/Concurrent_Versions_System

I SubversionI Created as a replacement of CVS, removing many of its

limitations.I Commits on several files, proper renaming support, better

performance, etc.I The user interface is very similar to CVSI 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

Page 406: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Distributed source control systems (1)

No longer have a central repository

I More adapted to the way the Free Software communitydevelops software and organizes

I Allows each developer to have a full local history of theproject, to create local branches. Makes each developer’s workeasier.

I People get working copies from other people’s working copies,and exchange changes between themselves. Branching andmerging is made easier.

I Make it easier for new developers to join, making their ownexperiments 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

Page 407: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Distributed source control systems (2)

I GitI Initially designed and developed by Linus Torvalds for Linux

kernel developmentI 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 projectsI http://en.wikipedia.org/wiki/Git_(software)

I MercurialI Another system, created with the same goals as Git.I Used by some big projects tooI 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

Page 408: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Embedded Linux application development

Debuggers

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 408/526

Page 409: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

GDB

The GNU Project Debuggerhttp://www.gnu.org/software/gdb/

I The debugger on GNU/Linux, available for mostembedded 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 aprogram, set breakpoints or change internalvariables. You can also use it to see what aprogram was doing when it crashed (by loadingits memory image, dumped into a core file).

See also http://en.wikipedia.org/wiki/Gdb

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 409/526

Page 410: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

GDB crash course

I A few useful GDB commandsI break foobar

puts a breakpoint at the entry of function foobar()I break foobar.c:42

puts a breakpoint in foobar.c, line 42I print var or print task->files[0].fd

prints the variable var, or a more complicated reference. GDBcan also nicely display structures with all their members

I continue

continue the executionI next

continue to the next line, stepping over function callsI 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

Page 411: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

GDB graphical front-ends

I DDD - Data Display Debuggerhttp://www.gnu.org/software/ddd/

A popular graphical front-end, with advanced data plottingcapabilities.

I GDB/Insighthttp://sourceware.org/insight/

From the GDB maintainers.

I KDbghttp://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

Page 412: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Embedded Linux application development

Remote debugging

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 412/526

Page 413: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Remote debugging

I In a non-embedded environment, debugging takes place usinggdb or one of its front-ends.

I gdb has direct access to the binary and libraries compiled withdebugging symbols.

I However, in an embedded context, the target platformenvironment is often too limited to allow direct debuggingwith gdb (2.4 MB on x86).

I Remote debugging is preferredI gdb is used on the development workstation, offering 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

Page 414: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Remote debugging: architecture

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 414/526

Page 415: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

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

Page 416: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Post mortem analysis

I When an application crashes due to a segmentation fault andthe application was not under control of a debugger, we getno information about the crash

I Fortunately, Linux can generate a core file that contains theimage of the application memory at the moment of the crash,and gdb can use this core file to let us analyze the state of thecrashed application

I On the targetI Use ulimit -c unlimited to enable the generation of a core

file when a crash occurs

I On the hostI After the crash, transfer the core file from the target to the

host, and runARCH-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

Page 417: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Embedded Linux application development

Memory checkers

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 417/526

Page 418: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

DUMA

Detect Unintended Memory Accesshttp://duma.sourceforge.net/

I Fork and replacement for Electric Fence

I Stops your program on the exact instruction that overruns orunderruns a malloc() memory buffer.

I GDB will then display the source-code line that causes thebug.

I Works by using the virtual-memory hardware to create ared-zone at the border of each buffer - touch that, and yourprogram 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

Page 419: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Valgrind (1)

http://valgrind.org/

I GNU GPL Software suite for debugging andprofiling programs.

I Supported platforms: Linux on x86, x86 64,ppc32, ppc64 and arm (armv7 only: Cortex A8,A9 and A5)

I Can detect many memory management andthreading bugs.

I Profiler: provides information helpful to speedup your program and reduce its memory usage.

I The most popular tool for this usage. Even usedby projects with hundreds of programmers.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 419/526

Page 420: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Valgrind (2)

I Can be used to run any program, without theneed to recompile it.

I Example usageValgrind --leak-check=yes ls -la

I Works by adding its own instrumentation to yourcode and then running in on its own virtual cpucore.Significantly slows down execution, but still finefor 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

Page 421: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Embedded Linux application development

System analysis

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 421/526

Page 422: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

strace

System call tracerhttp://sourceforge.net/projects/strace/

I Available on all GNU/Linux systemsCan be built by your cross-compiling toolchain generator.

I Even easier: drop a ready-made static binary for yourarchitecture, just when you need it. Seehttp://git.free-electrons.com/users/michael-

opdenacker/static-binaries/tree/strace

I Allows to see what any of your processes is doing:accessing files, allocating memory...Often sufficient to find 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

Page 423: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

strace example output

> strace cat Makefileexecve("/bin/cat", ["cat", "Makefile"], [/* 38 vars */]) = 0brk(0) = 0x98b4000access("/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) = 0xb7f85000access("/etc/ld.so.preload", R_OK) = -1 ENOENT (No such file or directory)open("/etc/ld.so.cache", O_RDONLY) = 3fstat64(3, {st_mode=S_IFREG|0644, st_size=111585, ...}) = 0mmap2(NULL, 111585, PROT_READ, MAP_PRIVATE, 3, 0) = 0xb7f69000close(3) = 0access("/etc/ld.so.nohwcap", F_OK) = -1 ENOENT (No such file or directory)open("/lib/tls/i686/cmov/libc.so.6", O_RDONLY) = 3read(3, "\177ELF\1\1\1\0\0\0\0\0\0\0\0\0\3\0\3\0\1\0\0\0\320h\1\0004\0\0\0\344"..., 512) = 512fstat64(3, {st_mode=S_IFREG|0755, st_size=1442180, ...}) = 0mmap2(NULL, 1451632, PROT_READ|PROT_EXEC, MAP_PRIVATE|MAP_DENYWRITE, 3, 0) = 0xb7e06000mprotect(0xb7f62000, 4096, PROT_NONE) = 0mmap2(0xb7f63000, 12288, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_DENYWRITE, 3, 0x15c) = 0xb7f63000mmap2(0xb7f66000, 9840, PROT_READ|PROT_WRITE, MAP_PRIVATE|MAP_FIXED|MAP_ANONYMOUS, -1, 0) = 0xb7f66000close(3) = 0

Hint: follow the open file descriptors returned by open().This tells you what files system calls are run on.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 423/526

Page 424: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

ltrace

A tool to trace library calls used by a program and all the signals itreceives

I Very useful complement to strace, which shows only systemcalls.

I Of course, works even if you don’t have the sources

I Allows to filter library calls with regular expressions, or just bya 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

Page 425: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

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

Page 426: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

ltrace summary

Example summary at the end of the ltrace output (-c option)

Process 17019 detached

% time seconds usecs/call calls errors syscall

------ ----------- ----------- --------- --------- ----------------

100.00 0.000050 50 1 set_thread_area

0.00 0.000000 0 48 read

0.00 0.000000 0 44 write

0.00 0.000000 0 80 63 open

0.00 0.000000 0 19 close

0.00 0.000000 0 1 execve

0.00 0.000000 0 2 2 access

0.00 0.000000 0 3 brk

0.00 0.000000 0 1 munmap

0.00 0.000000 0 1 uname

0.00 0.000000 0 1 mprotect

0.00 0.000000 0 19 mmap2

0.00 0.000000 0 50 46 stat64

0.00 0.000000 0 18 fstat64

------ ----------- ----------- --------- --------- ----------------

100.00 0.000050 288 111 total

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 426/526

Page 427: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

OProfile

http://oprofile.sourceforge.net

I A system-wide profiling tool

I Can 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 alreadyavailable 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

Page 428: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Callgrind / KCachegrind

I Cachegrind / Callgrind: part of the Valgrind tool suiteCollects function call statistics and call graphs. Useful toknow 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 profilers (suchas OProfile), and from profiling output from Python, Perl andPHP.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 428/526

Page 429: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

KCachegrind screenshot

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 429/526

Page 430: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Practical lab - App. development and debugging

Application development

I Compile your own application withthe ncurses library

Remote debugging

I Set up remote debugging tools onthe target: strace, ltraceand gdbserver.

I Debug a simple application runningon the target using remotedebugging

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 430/526

Page 431: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Embedded Linux application development

Developing on Windows

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 431/526

Page 432: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Developing on Windows!?

Using a GNU/Linux workstation is the easiest way to createsoftware for GNU/Linux or embedded Linux

I You use the same tools and environment as all communitydevelopers do. Much fewer issues you are the only one to face.

I You get familiar with the system. Essential for understandingissues.

However, some developers have no choice: Windows is the onlydesktop OS allowed in their company.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 432/526

Page 433: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

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 inGNU/Linux

I Allows to compile and run many GNU/Linuxprograms on Windows: shells, compiler, httpservers, X Window, GTK...

I Very easy to install. Can choose which tools todownload and install.

I For embedded Linux system developers: makes itpossible to use GNU toolchains (compiled forWindows) 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

Page 434: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Cygwin limitations

Cygwin is not a complete substitute for a real GNU/Linux system.

I Almost all developers work on GNU/Linux or on another Unixplatform (typically BSD). Don’t expect them to test that theirtools build on Windows with Cygwin.

I The number of Cygwin users is quite small.You may be the first to face or report building issues on thisplatform 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

Page 435: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

VMware

http://en.wikipedia.org/wiki/VMware

I License: 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 fordownload.

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

Page 436: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

VirtualBox

http://virtualbox.org from Oracle

I PC emulation solution available on bothWindows 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 USBsupport).No binaries released for Windows so far (butpossible).

I Based on QEMU’s core engine. Performancesimilar 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

Page 437: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Real-time in embedded Linux systems

Real-time inembedded LinuxsystemsFree Electrons

© Copyright 2004-2014, Free Electrons.Creative Commons BY-SA 3.0 license.Corrections, suggestions, contributions and translations are welcome!

Embedded LinuxDevelopers

Free Electrons

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 437/526

Page 438: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Real-time in embedded Linux systems

Introduction

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 438/526

Page 439: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Embedded Linux and real time

I Due to its advantages, Linux and open-source software aremore and more commonly used in embedded applications

I However, some applications also have real-time constraintsI 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

Page 440: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Embedded Linux and real time

I Linux is an operating system part of the large Unix familyI It was originally designed as a time-sharing system

I The main goal was to get the best throughput from theavailable hardware, by making the best possible usage ofresources (CPU, memory, I/O)

I Time determinism was not taken into account

I On the opposite, real-time constraints imply timedeterminism, even at the expense of lower global throughput

I Best throughput and time determinism are contradictoryrequirements

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 440/526

Page 441: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Linux and real-time approaches (1)

I Over time, two major approaches have been taken to bringreal-time requirements into Linux

I Approach 1I 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 thePREEMPT RT project.

I Approach 2I Add a layer below the Linux kernel that will handle all the

real-time requirements, so that the behaviour of Linux doesn’taffect 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

Page 442: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Linux and real-time approaches (2)

An alternative approach is to use specific hardware to run real-timework on:

I Dedicating a CPU core to a real-time OS or to a real-timeapplication, using some kind of hypervizor.

I Running real-time work on an FPGA

I Running real-time work on a dedicated microcontroller. Forexample, the TI AM335x CPU (used in the Beaglebone Black)has a ”Programmable Real-Time Unit and IndustrialCommunication Subsystem (PRU-ICSS)”, which can be usedfor real-time processing.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 442/526

Page 443: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Real-time in embedded Linux systems

Improving the main Linux kernel withPREEMPT RT

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 443/526

Page 444: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Understanding latency

I When developing real-time applications with a system such asLinux, the typical scenario is the following

I An event from the physical world happens and gets notified tothe CPU by means of an interrupt

I The interrupt handler recognizes and handles the event, andthen 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 toreact to the physical world event

I Real-time is about providing guaranteed worst case latenciesfor this reaction time, called latency

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 444/526

Page 445: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Linux kernel latency components

kernel latency = interrupt latency + handler duration + schedulerlatency + scheduler duration

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 445/526

Page 446: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Interrupt latency

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 446/526

Page 447: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Sources of interrupt latency

I One of the concurrency prevention mechanism used in thekernel is the spinlock

I It has several variants, but one of the variant commonly usedto prevent concurrent accesses between a process context andan interrupt context works by disabling interrupts

I Critical sections protected by spinlocks, or other section inwhich interrupts are explicitly disabled will delay the beginningof 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

Page 448: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Interrupt handler duration

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 448/526

Page 449: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Interrupt handler implementation

I In Linux, many interrupt handlers are split in two partsI A top-half, started by the CPU as soon as interrupts are

enabled. It runs with the interrupt line disabled and issupposed to complete as quickly as possible.

I A bottom-half, scheduled by the top-half, which starts after allpending top-halves have completed their execution.

I Therefore, for real-time critical interrupts, bottom-halvesshouldn’t be used: their execution is delayed by all otherinterrupts in the system.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 449/526

Page 450: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Scheduler latency

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 450/526

Page 451: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Understanding preemption (1)

I The Linux kernel is a preemptive operating system

I When a task runs in user space mode and gets interrupted byan interruption, if the interrupt handler wakes up anothertask, this task can be scheduled as soon as we return from theinterrupt handler.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 451/526

Page 452: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Understanding preemption (2)

I However, when the interrupt comes while the task is executinga system call, this system call has to finish before another taskcan be scheduled.

I By default, the Linux kernel does not do kernel preemption.

I This means that the time before which the scheduler will becalled 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

Page 453: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Scheduler duration

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 453/526

Page 454: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Other non-deterministic mechanisms

I Outside of the critical path detailed previously, othernon-deterministic mechanisms of Linux can affect theexecution time of real-time tasks

I Linux is highly based on virtual memory, as provided by anMMU, so that memory is allocated on demand. Whenever anapplication accesses code or data for the first time, it isloaded on demand, which can creates huge delays.

I Many C library services or kernel services are not designedwith real-time constraints in mind.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 454/526

Page 455: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Priority inversion

A process with a low priority might hold a lock needed by a higherpriority process, effectively 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

Page 456: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Interrupt handler priority

In Linux, interrupt handlers are executed directly by the CPUinterrupt mechanisms, and not under control of the Linuxscheduler. Therefore, all interrupt handlers have a higher prioritythan 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

Page 457: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

The PREEMPT RT project

I Long-term project lead by Linux kernel developers IngoMolnar, Thomas Gleixner and Steven Rostedt

I https://rt.wiki.kernel.org

I The goal is to gradually improve the Linux kernel regardingreal-time requirements and to get these improvements mergedinto the mainline kernel

I PREEMPT RT development works very closely with themainline development

I Many of the improvements designed, developed and debuggedinside PREEMPT RT over the years are now part of themainline Linux kernel

I The project is a long-term branch of the Linux kernel thatultimately should disappear as everything will have beenmerged

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 457/526

Page 458: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Improvements in the mainline kernel

I Coming from thePREEMPT RT project

I Since the beginning of 2.6I O(1) schedulerI Kernel preemptionI Better POSIX real-time

API support

I Since 2.6.18I Priority inheritance

support for mutexes

I Since 2.6.21I High-resolution timers

I Since 2.6.30I Threaded interrupts

I Since 2.6.33I Spinlock annotations

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 458/526

Page 459: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

New preemption options in Linux 2.6

2 new preemption models offered by standard Linux 2.6:

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 459/526

Page 460: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

1st option: no forced 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 whichoverall throughput is key.

I Best to reduce task switching to maximize CPU and cacheusage (by reducing context switching).

I Still benefits from some Linux 2.6 improvements: O(1)scheduler, increased multiprocessor safety (work on RTpreemption was useful to identify hard to find SMP bugs).

I Can also benefit from a lower timer frequency (100 Hz insteadof 250 or 1000).

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 460/526

Page 461: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

2nd option: voluntary kernel preemption

CONFIG_PREEMPT_VOLUNTARY

Kernel code can preempt itself

I Typically for desktop systems, for quicker application reactionto 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

Page 462: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

3rd option: preemptible kernel

CONFIG_PREEMPT

Most kernel code can be involuntarily preempted at any time.When a process becomes runnable, no more need to wait for kernelcode (typically a system call) to return before running thescheduler.

I Exception: kernel critical sections (holding spinlocks). In acase you hold a spinlock on a uni-processor system, kernelpreemption could run another process, which would loopforever if it tried to acquire the same spinlock.

I Typically for desktop or embedded systems with latencyrequirements 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

Page 463: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Priority inheritance

I One classical solution to the priority inversion problem iscalled priority inheritance

I The idea is that when a task of a low priority holds a lockrequested by a higher priority task, the priority of the first taskgets temporarily raised to the priority of the second task: ithas inherited its priority.

I In Linux, since 2.6.18, mutexes support priority inheritance

I In user space, priority inheritance must be explicitly enabledon a per-mutex basis.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 463/526

Page 464: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

High resolution timers

I The resolution of the timers used to be bound to theresolution of the regular system tick

I Usually 100 Hz or 250 Hz, depending on the architecture andthe configuration

I A resolution 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 programinterrupts at the right moment.

I Hardware timers are multiplexed, so that a single hardwaretimer is sufficient to handle a large number ofsoftware-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

Page 465: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Threaded interrupts

I To solve the interrupt inversion problem, PREEMPT RT hasintroduced the concept of threaded interrupts

I The interrupt handlers run in normal kernel threads, so thatthe priorities of the different interrupt handlers can beconfigured

I The real interrupt handler, as executed by the CPU, is only incharge of masking the interrupt and waking-up thecorresponding thread

I The idea of threaded interrupts also allows to use sleepingspinlocks (see later)

I Merged since 2.6.30, the conversion of interrupt handlers tothreaded interrupts is not automatic: drivers must be modified

I In PREEMPT RT, all interrupt handlers are switched tothreaded interrupts

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 465/526

Page 466: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

The future of the PREEMPT RT patchset

Conclusion of the Realtime Linux minisummit (Nov. 2013):

I The PREEMPT RT project will be done in 2014, ”one way oranother”. The first way would be to get most of the rest ofthe code upstream, but that will require more of an effort,from a wider group than is currently involved. The alternativeis to decide that the 95% of the realtime work alreadyupstream is good enough and to drop further efforts.

I Main issue: lack of companies devoting enough engineeringresources 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

Page 467: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Real-time in embedded Linux systems

PREEMPT RT specifics

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 467/526

Page 468: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

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 bymutexes (or so-called sleeping spinlocks)

I Instead of providing mutual exclusion by disabling interruptsand preemption, they are just normal locks: when contentionhappens, the process is blocked and another one is selected bythe 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 asnormal spinlocks.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 468/526

Page 469: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

CONFIG PREEMPT RT (2)

I With CONFIG_PREEMPT_RT, virtually all kernel code becomespreemptible

I An interrupt can occur at any time, when returning from theinterrupt handler, the woken up process can start immediately.

I This is the last big part of PREEMPT RT that isn’t fully inthe 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 nowdifferentiated from spinlocks that can be converted to sleepingspinlocks. This has reduced a lot the PREEMPT RT patchsize!

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 469/526

Page 470: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Threaded interrupts

I The mechanism of threaded interrupts in PREEMPT RT isstill different from the one merged in mainline

I In PREEMPT RT, all interrupt handlers are unconditionallyconverted to threaded interrupts.

I This is a temporary solution, until interesting drivers inmainline get gradually converted to the new threadedinterrupt 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

Page 471: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Real-time in embedded Linux systems

Setting up PREEMPT RT

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 471/526

Page 472: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

PREEMPT RT setup (1)

I PREEMPT RT is delivered as a patch against the mainlinekernel

I Best to have a board supported by the mainline kernel,otherwise the PREEMPT RT patch may not apply and mayrequire some adaptations

I At the moment, only ”even” releases of the Linux kernel aresupported: 3.0, 3.2, 3.4, 3.6, 3.8, 3.10, 3.12, 3.14.

I Quick set upI Download the latest PREEMPT RT patch

from http://www.kernel.org/pub/linux/kernel/projects/rt/

I Download and extract the corresponding mainline kernelversion

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

Page 473: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

PREEMPT RT setup (2)

I In the kernel configuration, be sure to enableI CONFIG_PREEMPT_RTI High-resolution timers

I Compile your kernel, and boot

I You are now running the real-time Linux kernel

I Of course, some system configuration remains to be done, inparticular setting appropriate priorities to the interruptthreads, which depend on your application.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 473/526

Page 474: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Real-time in embedded Linux systems

Real-time application developmentwith PREEMPT RT

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 474/526

Page 475: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Development and compilation

I No special library is needed, the POSIX real-time API is partof the standard C library

I The glibc or eglibc C libraries are recommended, as supportfor some real-time features is not available in uClibc yet

I Priority inheritance mutexes or NPTL on some architectures,for example

I Compile a programI ARCH-linux-gcc -o myprog myprog.c -lrt

I To get the documentation of the POSIX APII Install the manpages-posix-dev packageI Run man function-name

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 475/526

Page 476: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Process, thread?

I Confusion about the terms process, thread and taskI 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 theprocess

I They start executing a function passed as argument topthread_create()

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 476/526

Page 477: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Process, thread: kernel point of view

I The kernel represents each thread running in the system by astructure of type task_struct

I From a scheduling point of view, it makes no differencebetween the initial thread of a process and all additionalthreads created dynamically using pthread_create()

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 477/526

Page 478: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Creating threads

I Linux 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 willbe 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

Page 479: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Scheduling classes (1)

I The Linux kernel scheduler support different scheduling classesI The default class, in which processes are started by default is

a time-sharing classI All processes, regardless of their priority, get some CPU timeI The proportion of CPU time they get is dynamic and affected

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_RRI 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 priorityprocesses 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

Page 480: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Scheduling classes (2)

I An existing program can be started in a specific schedulingclass with a specific priority using the chrt command line tool

I Example: chrt -f 99 ./myprog

I The sched_setscheduler() API can be used to change thescheduling 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

Page 481: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Scheduling classes (3)

I The priority can be set on a per-thread basis when a thread iscreated

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 defined this way: stack size,etc.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 481/526

Page 482: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Memory locking

I In order to solve the non-determinism introduced by virtualmemory, memory can be locked

I Guarantee that the system will keep it allocatedI 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 pagesI Stack pagesI Dynamically-allocated memory

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 482/526

Page 483: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Mutexes

I Allows mutual exclusion between two threads in the sameaddress 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

Page 484: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Timers

I Timer creation

timer_create(clockid_t clockid, struct sigevent *evp,

timer_t *timerid);

I clockid is usually CLOCK_MONOTONIC. sigevent defineswhat happens upon timer expiration: send a signal or start afunction in a new thread. timerid is the returned timeridentifier.

I Configure the timer for 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

Page 485: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Timers (2)

I Delete 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

Page 486: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Signals

I Signals are asynchronous notification mechanismsI Notification occurs either

I By the call of a signal handler. Be careful with the limitationsof signal handlers!

I By being unblocked from the sigwait(), sigtimedwait() orsigwaitinfo() functions. Usually better.

I Signal behaviour can be configured using sigaction()

I The mask of blocked signals can be changed withpthread_sigmask()

I Delivery of a signal using pthread_kill() or tgkill()

I All signals between SIGRTMIN and SIGRTMAX, 32 signals underLinux.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 486/526

Page 487: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Inter-process communication

I SemaphoresI Usable between different processes using named semaphoresI sem_open(), sem_close(), sem_unlink(), sem_init(),

sem_destroy(), sem_wait(), sem_post(), etc.

I Message queuesI Allows processes to exchange data in the form of messages.I mq_open(), mq_close(), mq_unlink(), mq_send(),

mq_receive(), etc.

I Shared memoryI Allows processes to communicate by sharing a segment of

memoryI 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

Page 488: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Real-time in embedded Linux systems

Debugging latencies inPREEMPT RT

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 488/526

Page 489: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

ftrace - Kernel function tracer

Infrastructure that can be used for debugging or analyzinglatencies and performance issues in the kernel.

I Very well documented inDocumentation/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

Page 490: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Using ftrace

I Tracing information available through the debugfs virtual fs(CONFIG_DEBUG_FS in the Kernel Hacking section)

I Mount this filesystem as follows:mount -t debugfs nodev /sys/kernel/debug

I When tracing is enabled (see the next slides), tracinginformation 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

Page 491: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Scheduling latency tracer

CONFIG_SCHED_TRACER (Kernel Hacking section)

I Maximum recorded time between waking up a top priority task andits 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

Page 492: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Exemple of worst-case latencies

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

Page 493: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Useful reading

About real-time support in the standard Linux kernel

I Inside the RT patch, Steven Rostedt, Red Hat, ELC 2013http://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

Page 494: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Real-time in embedded Linux systems

Real-time extensions to the Linuxkernel

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 494/526

Page 495: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Linux real-time extensions

Three generations

I RTLinux

I RTAI

I Xenomai

A common principle

I Add an extra layer betweenthe hardware and the Linuxkernel, to manage real-timetasks separately.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 495/526

Page 496: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

RTLinux

First real-time extension for Linux, created by Victor Yodaiken.

I Nice, but the author filed a software patent covering theaddition of real-time support to general operating systems asimplemented in RTLinux!

I Its Open Patent License drew many developers away andfrightened users. Community projects like RTAI and Xenomainow attract most developers and users.

I February, 2007: RTLinux rights sold to Wind River. Today, nolonger 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

Page 497: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

RTAI

http://www.rtai.org/ - Real-Time Application Interface forLinux

I Created in 1999, by Prof. Paolo Mantegazza (long timecontributor to RTLinux), Dipartimento di IngegneriaAerospaziale Politecnico di Milano (DIAPM).

I Community project. Significant user base. Attractedcontributors frustrated by the RTLinux legal issues.

I Only really actively maintained on x86

I May offer slightly better latencies than Xenomai, at theexpense of a less maintainable and less portable code base

I Since RTAI is not really maintained on ARM and otherembedded architectures, our presentation is focused onXenomai.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 497/526

Page 498: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Xenomai project

http://www.xenomai.org/

I Started in 2001 as a project aiming at emulatingtraditional RTOS.

I Initial goals: facilitate the porting of programs toGNU / Linux.

I Initially related to the RTAI project (as the RTAI/ fusion branch), now independent.

I Skins mimicking the APIs of traditional RTOSsuch as VxWorks, pSOS+, and VRTXsa as wellas the POSIX API, and a “native” API.

I Aims at working both as a co-kernel and on topof PREEMPT RT in future upstream Linuxversions.

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

Page 499: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Xenomai architecture

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 499/526

Page 500: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

The Adeos interrupt pipeline abstraction

I From the Adeos point of view, guest OSes are prioritizeddomains.

I For each event (interrupts, exceptions, syscalls, etc...), thevarious domains may handle the event or pass it down thepipeline.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 500/526

Page 501: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Xenomai features

I Factored real-time core with skins implementing variousreal-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 theLinux kernel version (backward and forward compatible whenreasonable)

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

Page 502: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Xenomai user space real-time support

I Xenomai supports real-time in user space on 5 architectures,including 32 and 64 bits variants.

I Two modes are defined for a threadI the primary mode, where the thread is handled by the Xenomai

schedulerI the secondary mode, when it is handled by the Linux scheduler.

I Thanks to the services of the Adeos I-pipe service, Xenomaisystem calls are defined.

I A thread migrates from secondary mode to primary modewhen such a system call is issued

I It migrates from primary mode to secondary mode when aLinux system call is issued, or to handle gracefully exceptionalevents 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

Page 503: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Life of a Xenomai application

I Xenomai applications are started like normal Linux processes,they are initially handled by the Linux scheduler and haveaccess to all Linux services

I After their initialization, they declare themselves as real-timeapplications, which migrates them to primary mode. In thismode:

I They are scheduled directly by the Xenomai scheduler, so theyhave the real-time properties offered by Xenomai

I They don’t have access to any Linux service, otherwise theyget migrated back to secondary mode and loose all real-timeproperties

I They can only use device drivers that are implemented inXenomai, not the ones of the Linux kernel

I Need to implement device drivers in Xenomai, and to splitreal-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

Page 504: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Real Time Driver Model (RTDM)

I An approach to unify the interfaces for developing devicedrivers 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 driversI Network-style device drivers

I See the whitepaper onhttp://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

Page 505: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Real-time in embedded Linux systems

Setting up Xenomai

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 505/526

Page 506: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

How to build Xenomai

I Download Xenomai sources athttp://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 Linuxsources.

I Run the kernel configuration menu.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 506/526

Page 507: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Linux options for Xenomai configuration

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 507/526

Page 508: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Xenomai user space support

I User space libraries are compiled using the traditionalautotools

I ./configure --host=arm-linux && make && make

DESTDIR=/your/rootfs/ install

I Xenomai installs pkg-config files which helps you to compileyour 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

Page 509: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Real-time in embedded Linux systems

Developing applications on Xenomai

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 509/526

Page 510: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

The POSIX skin

I The POSIX skin allows to recompile without changes atraditional POSIX application so that instead of using Linuxreal-time services, it uses Xenomai services

I http://www.xenomai.org/index.php/Porting_POSIX_

applications_to_XenomaiI Clocks and timers, condition variables, message queues,

mutexes, semaphores, shared memory, signals, threadmanagement

I Good for existing code or programmers familiar with thePOSIX API

I Of course, if the application uses any Linux service that isn’tavailable 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

Page 511: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Communication with a normal task

I If a Xenomai real-time application using the POSIX skinwishes to communicate with a separate non-real-timeapplication, 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 applicationI Open /dev/rtpPORTX, where PORTX is the XDDP portI Use read() and write()

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 511/526

Page 512: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

The native API (1)

I A Xenomai specific API for developing real-time tasksI http://xenomai.org/documentation/branches/v2.4.x/

pdf/Native-API-Tour-rev-C.pdfI Usable both in user space and kernel space. Development of

tasks in user space is the preferred way.I More coherent and more flexible API than the POSIX API.

Easier to learn and understand. Certainly the way to go fornew applications.

I Applications should include <native/service.h>, whereservice 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

Page 513: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

The native API (2)

I Task management servicesI rt_task_create(), rt_task_start(),

rt_task_suspend(), rt_task_resume(),rt_task_delete(), rt_task_join(), etc.

I Counting semaphore servicesI rt_sem_create(), rt_sem_delete(), rt_sem_p(),

rt_sem_v(), etc.

I Message queue servicesI rt_queue_create(), rt_queue_delete(),

rt_queue_alloc(), rt_queue_free(), rt_queue_send(),rt_queue_receive(), etc.

I Mutex servicesI 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

Page 514: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

The native API (3)

I Alarm servicesI rt_alarm_create(), rt_alarm_delete(),

rt_alarm_start(), rt_alarm_stop(), rt_alarm_wait(),etc.

I Memory heap servicesI Allows to share memory between processes and/or to

pre-allocate a pool of memoryI rt_heap_create(), rt_heap_delete(), rt_heap_alloc(),

rt_heap_bind()

I Condition variable servicesI 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

Page 515: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Xenomai and normal task communication

I Using rt pipesI 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 applicationI Open the corresponding /dev/rtpX file, the minor is specified

at rt_pipe_create() timeI Then, just read() and write() to the opened file

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 515/526

Page 516: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Xenomai worst-case latencies results

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 athttp://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

Page 517: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Books

Building Embedded Linux 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

Page 518: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

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 membercompanies are German (Thomas Gleixner is on board).Supports the use of PREEMPT RT and Xenomai andcontributes to these projects. Shares useful documentationand resources.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 518/526

Page 519: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Practical lab - Real-time - Scheduling latency

I Check clock accuracy.

I Start processes with real-timepriority.

I Build a real-time applicationagainst the standard POSIXreal-time API, and againstXenomai’s POSIX skin

I Compare scheduling latency onyour system, between a standardkernel and a kernel with Xenomai.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 519/526

Page 520: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

References

ReferencesFree Electrons

© Copyright 2004-2014, Free Electrons.Creative Commons BY-SA 3.0 license.Corrections, suggestions, contributions and translations are welcome!

Embedded LinuxDevelopers

Free Electrons

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 520/526

Page 521: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Books

I Embedded Linux Primer, Second Edition,Prentice HallBy Christopher Hallinan, October 2010Covers a very wide range of interesting topics.http://j.mp/17NYxBP

I Building Embedded Linux Systems, O’ReillyBy Karim Yaghmour, Jon Masters, Gilad Ben-Yossef,Philippe Gerum and others (including MichaelOpdenacker), August 2008http://oreilly.com/catalog/9780596529680/

I Embedded Linux System Design andDevelopmentP. Raghavan, A. Lad, S. Neelakandan, Auerbach,Dec. 2005. Very good coverage of the POSIXprogramming 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

Page 522: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Web sites

I ELinux.org, http://elinux.org, a Wiki entirely dedicatedto embedded Linux. Lots of topics covered: real-time,filesystem, multimedia, tools, hardware platforms, etc.Interesting to explore to discover new things.

I LWN, http://lwn.net, very interesting news site aboutLinux in general, and specifically about the kernel. Weeklynewsletter, available for free after one week for non-payingvisitors.

I Linux Gizmos, http://linuxgizmos.com, a news siteabout embedded Linux, mainly oriented on hardwareplatforms related news.

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 522/526

Page 523: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

International conferences

Useful conferences featuring embedded Linux and kernel topics

I Embedded Linux Conference:http://embeddedlinuxconference.com/

Organized by the CE Linux Forum: California (San Francisco,Spring), in Europe (Fall). Very interesting kernel and userspace topics for embedded systems developers. Presentationslides freely available

I Linux Plumbers, http://linuxplumbersconf.orgConference 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

Page 524: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Last slides

Last slidesFree Electrons

© Copyright 2004-2014, Free Electrons.Creative Commons BY-SA 3.0 license.Corrections, suggestions, contributions and translations are welcome!

Embedded LinuxDevelopers

Free Electrons

Free Electrons. Embedded Linux, kernel, drivers and Android development, consulting, training and support. http://free-electrons.com 524/526

Page 525: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Evaluation form

Please take a few minutes to rate this training session, byanswering 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

Page 526: Embedded Linux system development Embedded Linux system ...ppedreiras.av.it.pt/resources/str1617/praticas/embedded-linux... · Embedded Linux system development ... ... I Don’t

Last slide

Thank you!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