Introduction To Embedded Linux - Bootlin

Transcription

Captronic SeminarIntroduction toEmbedded LinuxThomas Petazzonithomas.petazzoni@bootlin.com Copyright 2004-2018, Bootlin.Creative Commons BY-SA 3.0 license.Corrections, suggestions, contributions and translations are welcome!- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.comembedded Linux and kernel engineering1/103

Thomas Petazzoni Thomas Petazzoni Embedded Linux engineer and trainer at Bootlin since January 2008Linux user and developer since 2000Given more than 120 days of embedded Linux training around the worldLinux kernel development, embedded Linux system integration, boot time and powerconsumption optimization, consulting, for various customers on ARM, MIPS,Blackfin and x86 based systems Major contributor to Buildroot, an open-source, simple and fast embedded Linuxbuild system- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com2/103

Bootlin Bootlin, specialized in Embedded Linux, since 2005 Strong emphasis on community relation Training Embedded Linux system development Linux kernel and device driver development Upcoming public sessions in Avignon, Lyon and Toulouse, or sessions at customerlocation All training materials freely available under a Creative Commons license. Development and consulting Board Support Package development or improvementKernel and driver developmentEmbedded Linux system integrationPower-management, boot-time, performance audits and improvementEmbedded Linux application development- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com3/103

Bootlin customers- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com4/103

Bootlin trainings- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com5/103

Agenda Introduction: open-source and free software principles, advantages in theembedded space, hardware needed for embedded Linux Open Source for embedded systems: tools, bootloaders, kernel, systemfoundations, graphics and multimedia, networking, real-time, etc. Development process of an embedded Linux system Commercial support, community support Android Conclusion Q&A- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com6/103

About free software- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com7/103

Birth of free software 1983, Richard Stallman: GNU project and concept of “free software”. Start ofdevelopment of gcc, gdb, glibc, etc. developed. 1991, Linus Torvalds launches the Linux project, a Unix-like operating systemkernel. Together with GNU software and other free software components, itcreates a complete and usable operating system: GNU/Linux 1995, Linux is more and more widely used on server systems. 2000, Linux is more and more widely used in embedded systems 2005, Linux is more and more widely used in desktop systemsFree software is no longer a “new” thing, it has been well established for many years- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com8/103

Free softwareA program is considered free when its license offers to all its users the followingfreedoms: Freedom to run the software, for any purpose Freedom to study how the software works, and change it Freedom to redistribute copies Freedom to distribute copies of modified versionsThese freedoms are granted for both commercial and non-commercial use, withoutdistinction.Those freedoms imply that the source code is available, it can modified tomatch the needs of a given product, and the result can be distributed tocustomers good match for embedded systems!- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com9/103

Advantages of open-source in embeddedsystems- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com10/103

Re-using components The key advantage when using Linux and open-source components in embeddedsystems is the ability to re-use existing components. The open-source ecosystem already provides many components for standardfeatures, from hardware support to network protocols, going through multimedia,graphic, cryptographic libraries, etc. As soon as a hardware device, or a protocol, or a feature is wide-spread enough,high chance of having open-source components that support it. Allows to quickly design and develop complicated products, based on existingcomponents. No-one should re-develop yet another operating system kernel, TCP/IP stack,USB stack or another graphical toolkit library. Allows to focus on the added value of your product.- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com11/103

Low cost Free software can be duplicated on as many devices as you want, free of charge. If your embedded system uses only free software, you can reduce the cost ofsoftware to zero. Even the development tools are free, unless you choose acommercial embedded Linux edition. Allows to have an higher budget for the hardware or to increase thecompany’s skills and knowledge- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com12/103

Full control With open-source, you have the source code for all components in your system Allows unlimited modifications, changes, tuning, debugging, optimization, for anunlimited period of time Without locking or dependency from a third-party vendor To be true, non open-source components must be avoided when the system isdesigned and developed Allows to have full control over the software part of your system- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com13/103

Quality Many open-source components are widely used, on millions of systems Higher quality than what an in-house development can produce, or evenproprietary vendors Of course, not all open-source components are of good quality, but most of thewidely-used ones are. Allows to design your system with high-quality components at thefoundations- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com14/103

Test of possible components Open-source being freely available, it is easy to get one and evaluate it Allows to easily study several options while making a choice Much easier than purchasing and demonstration procedures needed with mostproprietary products Allows to easily explore new possibilities and solutions- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com15/103

Community support Open-source software components are developed by communities of developersand users This community can provide a high-quality support: you can directly contact themain developers of the component you are using Often better than traditional support, but one needs to understand how thecommunity works to properly use the community support possibilities Allows to speed up the resolution of problems when developing your system- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com16/103

Taking part into the community Possibility of taking part into the development community of some of thecomponents used in the embedded systems: bug reporting, test of new versions orfeatures, patches that fix bugs or add new features, etc. Most of the time the open-source components are not the core value of theproduct: it’s the interest of everybody to contribute back. For the engineers: a very motivating way of being recognized outside thecompany, communication with others in the same field, opening of newpossibilities, etc. For the managers: motivation factor for engineers, allows the company to berecognized in the open-source community and therefore get support more easilyand be more attractive to open-source developers- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com17/103

Drawbacks Large choice: community support or commercial support? Which company forthe support? Which software solution? At the same time a strength and a drawback of free software and open source New skills needed compared to bare metal development or development withtraditional embedded operating systems. Need for training Need for recruiting new profiles Licensing fear Generally over-exaggerated- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com18/103

Hardware for embedded Linux- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com19/103

Processor architecture The Linux kernel and most other architecture-dependent component support awide range of 32 and 64 bits architectures x86 and x86 64, as found on PC platforms, but also embedded systems(multimedia, industrial) ARM, with hundreds of different SoC (multimedia, industrial) PowerPC (mainly real-time, industrial applications) MIPS (mainly networking applications) SuperH (mainly set top box and multimedia applications) Blackfin (DSP architecture) Microblaze (soft-core for Xilinx FPGA) Coldfire, SCore, Tile, Xtensa, Cris, FRV, AVR32, M32R Both MMU and no-MMU architectures are supported, even though no-MMUarchitectures have a few limitations. Linux is not designed for small microcontrollers. Besides the toolchain, the bootloader and the kernel, all other components aregenerally architecture-independent- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com20/103

RAM and storage RAM: a very basic Linux system can work within 8 MB of RAM, but a morerealistic system will usually require at least 32 MB of RAM. Depends on the typeand size of applications. Storage: a very basic Linux system can work within 4 MB of storage, but usuallymore is needed. flash storage is supported, both NAND and NOR flash, with specific filesystems Block storage including SD/MMC cards and eMMC is supported Not necessarily interesting to be too restrictive on the amount of RAM/storage:having flexibility at this level allows to re-use as many existing components aspossible.- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com21/103

Communication The Linux kernel has support for many common communication busses I2CSPICAN1-wireSDIOUSB And also extensive networking support Ethernet, Wifi, Bluetooth, CAN, etc. IPv4, IPv6, TCP, UDP, SCTP, DCCP, etc. Firewalling, advanced routing, multicast- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com22/103

ARM and System-on-Chip ARM is one of the most popular architectures used in embedded Linux systems ARM designs CPU cores (instruction sets, caches, MMU, etc.) and sells thedesign to licensees The licensees are founders (Texas Instruments, Freescale, ST Ericsson, Atmel,etc.), they integrate an ARM core with many peripherals, into a chip called aSoC, for System-on-chip Each founder provides different models of SoC, with different combination ofperipherals, power, power consumption, etc. The concept of SoC allows to reduce the number of peripherals needed on theboard, and therefore the cost of designing and building the board. Linux supports SoCs from most vendors, but not all, and not with the samelevel of functionality.- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com23/103

ARM and System-on-Chip- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com24/103

Atmel AT91SAM9263- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com25/103

Texas Instruments OMAP3430- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com26/103

Type of hardware platforms Evaluation platforms from the SoC vendor. Usually expensive, but manyperipherals are built-in. Generally unsuitable for real products. Component on Module, a small board with only CPU/RAM/flash and a fewother core components, with connectors to access all other peripherals. Can beused to build end products for small to medium quantities. Community development platforms, a new trend to make a particular SoCpopular and easily available. Those are ready-to-use and low cost, but usuallyhave less peripherals than evaluation platforms. To some extent, can also be usedfor real products. Custom platform. Schematics for evaluation boards or development platformsare more and more commonly freely available, making it easier to develop customplatforms.- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com27/103

Gumstix TI OMAP3530 (600 MHz, ARM Cortex-A8,PowerVR SGX, DSP) Component on Module 256 MB RAM 256 MB NAND (optional) Bluetooth, Wifi (optional) uSD 115-229 Development boards available at 49-229, with manyperipherals: LCD, Ethernet, UART, SPI, I2C, etc. http://www.gumstix.com- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com28/103

Armadeus Freescale i.MX 51, 800 MHz Component on Module 256/512 MB RAM 512 MB NAND FPGA Xilinx Spartan 6A 168 e Development boards available at 248 e, withperipherals (Ethernet, serial, etc.) and accessto UART, SPI, I2C, PWM, GPIO, USB, SD,LCD, Keypad, etc. http://www.armadeus.com- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com29/103

Calao TNY A9G20 Atmel AT91 9G20, 400 MHz Component on Module 64 MB RAM 256 MB NAND 152 e Many expansion boards available http://www.calao-systems.com- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com30/103

BeagleBoard XM TI OMAP DM3730 (1 GHz, cortexA8, PowerVR GPU, DSP) Community development platform 512 MB RAM eMMC, microSD, Ethernet HDMI, S-Video, Camera, audio Expansion: USB, I2C, SPI, LCD,UART, GPIO, etc. 149 http://beagleboard.org- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com31/103

Snowball ST Ericsson AP9500 (dual cortex A9,MALI GPU) Community development platform 1 GB RAM 4-8 GB eMMC, microSD HDMI, S-Video, audio Wifi, Bluetooth, Ethernet Accelerometer, Magnetometer,Gyrometer, GPS Expansion: USB, I2C, SPI, LCD, UART,GPIO, etc. 169 e to 244 e http://igloocommunity.org- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com32/103

Freescale Quick Start Freescale I.MX53 (1 GHz Cortex A8) Community development platform 1 GB RAM 4-8 GB eMMC, microSD LVDS, LCD, VGA, HDMI, audio Accelerometer, SD/MMC, microSD,SATA, Ethernet, USB Expansion: I2C, SPI, SSI, LCD, Camera 149 e- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com33/103

Atmel SAM9X35-EK Atmel AT91SAM9x35 (400 Mhz) Manufacturer evaluation board 128 MB RAM 256 MB NAND Flash, 4 MB flash SPI Ethernet, 2 UARTs, 2 CANs, USBhost/device uSD slot, MMC/SD/SDIO,LCD/touchscreen Zigbee, SPI, I2C, etc. 590- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com34/103

Criteria for choosing the hardware Make sure the hardware you plan to use is already supported by the Linux kernel,and has an open-source bootloader, especially the SoC you’re targeting. Having support in the official versions of the projects (kernel, bootloader) is a lotbetter: quality is better, and new versions are available. Some SoC vendors and/or board vendors do not contribute their changes back tothe Linux kernel. Ask them to do so, or use another product if you can. A goodmeasurement is to see the delta between their kernel and the official one. Between properly supported hardware in the official Linux kernel andpoorly-supported hardware, there will be huge differences in development time andcost.- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com35/103

Embedded systems using Linux- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com36/103

Consumer device: Internet box- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com37/103

Consumer device: Network Attached Storage- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com38/103

Consumer device: Television- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com39/103

Professional device: point of sale terminal- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com40/103

Industrial system: laser cutter- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com41/103

Industrial system: wind turbine- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com42/103

Industrial system: cow milking- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com43/103

Industrial system: snow removal equipment- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com44/103

Industrial system: sea pollution detection system- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com45/103

Industrial system: viticulture machine- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com46/103

Open-source components for embeddedsystems- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com47/103

Four major componentsEvery embedded Linux system needs four major components to work: Toolchain, which doesn’t run on the target platform, but allows to generate codefor the target from a development machine. Bootloader, which is responsible for the initial boot process of the system, andfor loading the kernel into memory Linux Kernel, with all the device drivers Root filesystem, which contains all the applications and libraries- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com48/103

Four major components- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com49/103

ToolchainThe toolchain is usually a cross-compilation toolchain: it runs on a developmentmachine and generates code for the embedded platform. It has the followingcomponents: binutils, the binary manipulation utility including an assembler and a linker gcc, the C/C (and more) compiler, which is the standard in the open-sourceworld a C library, which offers the POSIX interface to userspace applications. Several Clibraries are available: glibc, eglibc and uClibc, with different size/features. gdb, the debugger, which allows remote debugging- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com50/103

Getting a cross-compilation toolchain Pre-compiled toolchains are the easiest solution. The toolchains fromCodeSourcery are very popular.http://www.codesourcery.com/ Crosstool-NG is a tool that automates the process of generating the toolchain.Allows more flexibility than pre-compiled toolchains.http://www.crosstool-ng.org Embedded Linux build systems are also usually capable of generating their owncross-compilation toolchain.Make sure to get a toolchain that matches your hardware and your needs.The toolchains provided by the hardware vendors are often old and rusty.- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com51/103

Bootloader: principle The role of the bootloader is to initialize some basic hardware peripherals, loadthe Linux kernel image and run it. The boot process of most recent embedded processors is the following:1. The processor executes code in ROM, to load a first-stage bootloader fromNAND, SPI flash, serial port or SD card2. The first stage bootloader initializes the memory controller and a few otherperipherals, and loads a second stage bootloader. No interaction is possible with thisfirst stage bootloader, and it is typically provided by the CPU vendor.3. The second stage bootloader offers more features: usually a shell, withcommands. It allows to manipulate the storage devices, the network, configure theboot process, etc. This bootloader is typically generic and open-source.- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com52/103

Bootloader: principle- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com53/103

Open-source bootloaders U-Boot is the de-facto standard in open-source bootloaders. Available on ARM,PowerPC, MIPS, m68k, Microblaze, x86, NIOS, SuperH, Sparc. Huge hardwaresupport available, large number of features (networking, USB, SD, etc.)http://www.denx.de/wiki/U-Boot Barebox, a newer open-source bootloader, with a cleaner design than U-Boot,but less hardware support for the moment.http://www.barebox.org GRUB, the standard for x86 PC.http://www.gnu.org/software/grub/Make sure your hardware comes with one of these well-known open-source bootloaders.- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com54/103

Linux kernel The Linux kernel is a core piece of the system. It provides the major followingfeatures: Process managementMemory managementInter-process communication, timersDevice drivers for the hardware: input, sound, network, storage, graphics, dataacquisition, timers, GPIO, etc. Filesystems Networking Power management The Linux kernel has thousands of options to selectively enable or disable featuresdepending on the system needs. Under the GPLv2 license. http://www.kernel.org- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com55/103

Kernel vs. userspace The Linux kernel runs in privileged mode. It can access and control thehardware. Its role is to multiplex the available resources, and provide coherent andconsistent interfaces to userspace. Userspace is the set of applications and libraries that run on the system. Theywork in unprivileged mode. They must go through the kernel to access the systemresources, including the hardware. The kernel provides isolation betweenapplications.- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com56/103

Kernel vs. userspace- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com57/103

The kernel for your platformThe Linux kernel is highly-portable across architectures. Therefore, the code is splitinto several levels: Code generic to all architectures: ARM, x86, PowerPC, etc. Code specific to an architecture Code specific to a System-on-Chip: Atmel AT91, TI OMAP3, Freescale i.MX,etc. Code specific to a board, which consists of a particular SoC with additionalperipherals on the boardIf your SoC is well supported, the only part of the code that needs to be modified isthe part specific to the board. Except if you need additional device drivers.- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com58/103

Kernel compilation- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com59/103

Kernel programming The programming environment inside the Linux kernel is very different from theone in userspace: different API, different constraints, different mechanisms No standard C library, a specific API is available No memory protection Programming in the kernel is typically needed to adapt the kernel to a particular board write device drivers There are many resources on kernel programming: Linux Device Drivers, book from O’Reilly Essential Linux Device Drivers, book from Prentice Hall Kernel programming training materials from Bootlin- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com60/103

Root filesystem In a Linux system, applications, libraries, configuration and data are stored intofiles in a filesystem A global single hierarchy of directories and files is used to represent all the files inthe system, regardless of their storage medium or location. A particular filesystem, called the root filesystem is mounted at the root of thishierarchy. This root filesystem typically contains all files needed for the system to workproperly.- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com61/103

FilesystemsThe Linux kernel supports a wide-range of filesystem types: ext2, ext3, ext4 are the default filesystem types for Linux. They are usable onblock devices. jffs2, ubifs are the filesystems usable on flash devices (NAND, NOR, SPI flashes).Note that SD/MMC cards or USB keys are not flash devices, but block devices. squashfs is a read-only highly-compressed filesystem, appropriate for all systemfiles that never change. vfat, nfts, the Windows-world filesystems, are also supported for compatibility nfs, cifs are the two most important network filesystems supported- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com62/103

Minimum contents of a root filesystem An init application, which is the first application started by the kernel when thesystem boots. init is in charge of starting shells, system services and applications. A shell and associated tools, in order to interact with the system. The shell willtypically operate over a serial port. The C library, which implements the POSIX interface, used by all applications. A set of device files. Those are special files that allow applications to performoperations on the devices managed by the kernel. A typical Unix hierarchy, with the bin, dev, lib, sbin, usr/bin, usr/lib,usr/sbin, proc, sys The proc and sysfs virtual filesystems mounted- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com63/103

Busybox In a normal Linux system, all core components are spread into different projectsand not implemented with embedded constraints in mind Busybox provides a highly-configurable compilation of all the basic commandsneeded in a Linux system: cp, mv, sh, wget, grep, init, modprobe, udhcpc,httpd All those commands are compiled into a single binary, and the commands aresymbolic links to this binary. Very space-efficient on systems where staticcompilation is used. Under the GPLv2 license http://www.busybox.net- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com64/103

Application development in a basic system With just Busybox and the C library, we have a fully working embedded Linuxsystem The C library implements the well-known POSIX interface, which provides an APIfor process control, signals, file and device operations, timers, pipes, the Cstandard library (string functions, etc.), memory management, semaphores, sharedmemory, thread management, networking, etc. This is an already very comfortable environment to develop applications in C orC that can interact with the hardware devices, do some computations, reacton hardware devices. Thanks to Busybox, you can even easily provide a Web server to monitor thesystem.- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com65/103

Interaction with the hardwareThere are different ways of interacting with the hardware: The kernel has a device driver for the device, in which case it can be accessedeither through a device file in /dev with the standard file API, through a text filein the sysfs filesystem or through the networking API. Userspace applications running as root can use the /dev/mem special device toaccess directly the physical memory, or better use the UIO framework of thekernel. For the SPI and I2C busses, there are special /dev/spidevX and /dev/i2cdevXto send/receive messages on the bus, without having a kernel device driver. ForUSB, the libusb library allows userspace USB device drivers.The choice of one solution over another depends on the type of device, and the need ofinteraction with existing kernel subsystems.- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com66/103

Graphics- Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com67/103

Drawing X.org, http://www.x.org A client/server approach. The server manages the hardware (graphics and input),and provides a protocol to clients. The clients are all applications that want to drawthings and receive input events. The solution used on all desktop Linux systems, allows c

2000, Linux is more and more widely used in embedded systems 2005, Linux is more and more widely used in desktop systems Free software is no longer a “new” thing, it has been well established for many years - Kernel, drivers and embedded Linux - Development, consulting, training