L0 Embedded Linux

download L0 Embedded Linux

of 62

Transcript of L0 Embedded Linux

  • 7/27/2019 L0 Embedded Linux

    1/62

    Software for Embedded Systems

    (Embedded Linux)CS424

    Tathagata Ray

  • 7/27/2019 L0 Embedded Linux

    2/62

    Embedded Linux

    Embedded Linux is just like the distributions running on

    millions of desktops and servers but its adapted to aspecific use case.

    There are other embedded OS such as VxWorks, Integrity,and Symbian.

    The biggest difference between using a traditional EOS and

    Linux is the separation between kernel and theapplications.

    Embedded Linux is gradually becoming popular.

  • 7/27/2019 L0 Embedded Linux

    3/62

    Standards Based

    Embedded Linux adheres to industry standards. For example Portable Operating System Interface for Unix

    (POSIX) is a standard specified by IEEE for handlingthreads and interprocess communication. Most linuxdistributions are POSIX compliant.

  • 7/27/2019 L0 Embedded Linux

    4/62

    Process Isolation and control

    Manage tasks, isolate them from the kernel and each other

    Linux is multitasking OS.Creates process

    Process cant access the memory space of anotherprocess.

    Processes cant access arbitrary memory from thekernel. Need to use syscalls.

    Linux uses a virtual memory-management system.

    Many embedded system dont use virtual memory asthere is no disk.

    Uniform Interface to resources For example malloc() function allocates memory from

    the heap. It always works no matter what is theprocessor.

    Same is true for files.

  • 7/27/2019 L0 Embedded Linux

    5/62

    Other reasons

    Wide peripheral supports.

    Security

    Being open source is an advantage

    Privilege level

    Pluggable Authentication Module Its a framework.

    The framework provides a uniform way forauthentication-related activities to take place.

    http://docs.oracle.com/cd/E19082-01/819-2145/ch3pam-01/index.html

  • 7/27/2019 L0 Embedded Linux

    6/62

    Commercial reasons

    Apart from kernel, linux has a collection ofsoftware working together. Use thedesktop to run and test software forembedded system (cross compilation).

    No royalties. Complete control over the source code of

    the software

  • 7/27/2019 L0 Embedded Linux

    7/62

    Basic Steps of embedded Linux

    development process Target Hardware

    Create development Boards containing chip andcollection of peripherals and connectors.

    They are large, bulky and designed to be easilyhandled.

    Obtaining Linux

    Linux is nearly always included with a developmentboard and has support for the peripherals supported bythe chip or the development board.

    Booting Linux

    configure the software services Linux needs to boot and

    ensuring the cabling is proper and attached.

    Development Environment

    cross-compilation

    System design

    The linux distribution needs to be optimized accordingto the required design.

  • 7/27/2019 L0 Embedded Linux

    8/62

    Anatomy of Embedded Linux

    Software components of a embeddedLinux system

    Boot Loader

    This is the software thats first run on the system.

    Primary responsibility is to get the processor

    initialized and ready to run the operating system.

    Kernel is stored in flash memory which is copied toRAM by boot loader and sets the instruction pointerto that memory location and tells the processor tostart executing at the IPs current location.

  • 7/27/2019 L0 Embedded Linux

    9/62

    Anatomy of Embedded Linux

    Kernel

    Created by a Finnish computer science studentin 1991.

    Linux is ported to nearly every majorprocessor.

    Less kernel development work.

    Thin down the Kernel to reduce the bootingtime.

    No low-level programming required to run thekernel

  • 7/27/2019 L0 Embedded Linux

    10/62

    Anatomy of Embedded Linux

    Root File system

    A file system is a way of representing ahierarchical collection of directories, whereeach directory can contain other subdirectoriesand files.

    The top of the tree is called the root.

    On booting, Linux must be able to mount theroot file system.

  • 7/27/2019 L0 Embedded Linux

    11/62

    Anatomy of Embedded Linux Your Application

    When Linux starts, it looks for a program toexecute by default, or you can supply it withthe name of something to run.

    init (short for initialization) is

    a daemon process that is the direct orindirect ancestor of all other processes. Itautomatically adopts all orphaned processes.

    Init is the first process started during booting,

    and is typically assigned PID number 1. It isstarted by the kernel using a hard-coded filename, and if the kernel is unable tostart it, a kernel panic will result.

    Init continues running until the system is shutdown.

  • 7/27/2019 L0 Embedded Linux

    12/62

    Tool Chain

    In order to build the operating system andapplications, we need a tool chain.

    Tool chain provide the assembler,compiler, and linker, along with a number

    of utilities needed to develop a Linuxbased system

    Most common tool chain is GNU tools.

    The command sudo apt-get installgcc will install the IA-32 GCC binary tool

    chain for a debian/Ubuntu-based system.

  • 7/27/2019 L0 Embedded Linux

    13/62

    Tool Chain

    The host machine is the machine you develop your

    applications on. In most cases today that will be an IA-32-based machine

    running a desktop distribution of Linux such asFedora/Centos, Ubuntu, or SUSE/OpenSuse.

    The target device is the actual embedded device that you are

    developing the software for. The device could be based on IA-32, ARM, MIPS, PowerPC, or any of the other CPU architecturesupported by Linux.

  • 7/27/2019 L0 Embedded Linux

    14/62

    Cross Compilation

    When the host and target architecture are the same, you

    are said to be doing native development. When the host and target platforms differ, you are said to

    be doing cross development.

    In this case the tool chain that you download and run onyour host must be a specialized version capable of

    executing on the host CPU architecture but building for thetarget CPU architecture.

    All of the GNU tools support both native host developmentand cross-development configurations.

  • 7/27/2019 L0 Embedded Linux

    15/62

    Getting the tools

    The compiler is provided by GNU Compiler Collection (GCC)

    (http://gcc.gnu.org/), and the assembler, linker, library,and object manipulation tools are provided by the GNUbinutils project.

    There are a number of scripts that greatly simplify thegeneration of the tool chain for example crosstool-NG.

    It cross-builds the tool chain for all architectures supportedby GCC.

  • 7/27/2019 L0 Embedded Linux

    16/62

  • 7/27/2019 L0 Embedded Linux

    17/62

    Tools Overview

    ar : Creates, modifies, and extracts from archives. An

    archive is a collection of objects or libraries. It provides asimple way to distribute a large number of objects/librariesin a single file.

    as : The GNU assembler. The assembler can be used

    natively and is used to assembly the output of the GCC

    compiler. gcc :The GNU C and C++ compiler. The default invocation

    of cc performs preprocessing, compilation assembly, andlinking to generate an executable image.

  • 7/27/2019 L0 Embedded Linux

    18/62

  • 7/27/2019 L0 Embedded Linux

    19/62

    Kernel

    Source code of the kernel is available at

    http://www.kernel.org.

    Pick the latest matured Kernel to startyour work.

    Kernel Source Tree: kernel code + devicedrivers + platform support code.

    Know about the Kernel features. You maynot need all the features.

    Choose the right kernel version.

    http://www.kernel.org/http://www.kernel.org/
  • 7/27/2019 L0 Embedded Linux

    20/62

    Device driver support

    If the device is in the market for some

    time then driver is most likely present inthe kernel source tree.

    Otherwise get it from manufacturer.

    You can develop your own driver andupstream the driver.

  • 7/27/2019 L0 Embedded Linux

    21/62

    Building a Kernel

  • 7/27/2019 L0 Embedded Linux

    22/62

    Kernel source tree arch: The arch subdirectory contains all of the architecture specific kernel

    code. It has further subdirectories, one per supported architecture, forexample i386 and alpha.

    include: The include subdirectory contains most of the include filesneeded to build the kernel code. It too has further subdirectories includingone for every architecture supported. The include/asm subdirectory is asoft link to the real include directory needed for this architecture, forexample include/asm-i386. To change architectures you need to edit the

    kernel makefile and rerun the Linux kernel configuration program. init: This directory contains the initialization code for the kernel and it is a

    very good place to start looking at how the kernel works.

  • 7/27/2019 L0 Embedded Linux

    23/62

    Kernel Source Tree mm: This directory contains all of the memory management code. The

    architecture specific memory management code lives downin arch/*/mm/, for example arch/i386/mm/fault.c.

    drivers: All of the system's device drivers live in this directory. They arefurther sub-divided into classes of device driver, for example block.

    ipc: This directory contains the kernels inter-process communicationscode.

    modules: This is simply a directory used to hold built modules.

    fs: All of the file system code. This is further sub-divided into directories,one per supported file system, for example vfat and ext2.

    kernel: The main kernel code. Again, the architecture specific kernel codeis in arch/*/kernel.

    net: The kernel's networking code.

    lib: This directory contains the kernel's library code. The architecturespecific library code can be found in arch/*/lib/.

    scripts: This directory contains the scripts (forexample awk and tk scripts) that are used when the kernel is configured.

  • 7/27/2019 L0 Embedded Linux

    24/62

    Example

  • 7/27/2019 L0 Embedded Linux

    25/62

    Kernel options

    .config file controls the wide range of

    kernel configuration items.

    You can open .config and view but do notmake any changes in it manually.

  • 7/27/2019 L0 Embedded Linux

    26/62

    menuconfig

  • 7/27/2019 L0 Embedded Linux

    27/62

    .config file

  • 7/27/2019 L0 Embedded Linux

    28/62

    .config file CONFIG_FEATURE_XX=y: means that the feature is built into the kernel at build

    time.

    #CONFIG_FEATURE_XX is not set: This config is not included.

    CONFIG_FEATURE_XX=m. This item includes a feature as a dynamically loadedmodule. The feature is not statically compiled into the kernel. The modules arestored on the root file system and loaded automatically as required during the bootsequence.

    In general, the dynamically loaded modules are used for device drivers.

  • 7/27/2019 L0 Embedded Linux

    29/62

    Building the Kernel

  • 7/27/2019 L0 Embedded Linux

    30/62

    Building the Kernel

    The bzimage file consists of a compressed kernel and

    startup code that is used to decompress the kernel image. A map file is a list of addresses and an associated program

    symbols. The Linux kernel map file is system.map.

  • 7/27/2019 L0 Embedded Linux

    31/62

    Why compressed?

    The first reason is to save on the storage

    requirements for the kernel. In embeddedsystems the kernel is usually stored on aflash device (although general massstorage is also an option).

    The second reason to use compression isboot speed.

    Many embedded systems to keep the

    image uncompressed and run the kernelfrom flash without first copying tomemory. This is known as execute inplace (XIP).

  • 7/27/2019 L0 Embedded Linux

    32/62

    Root File System Build

    The file system layout in most cases will follow the layout

    defined by the Filesystem Hierarchy Standard (FHS). In many embedded systems, the FHS is not followed

    exactly, as the root file system is trimmed significantly.

    The FHS based file system follows the directory layoutbelow:

    /bin The programs that are normally accessible by allusers are stored here. For example, shell-sh or busybox.

    /dev This contains special block/character device andnamed pipe files.

    /etc This is the general configuration storage directory,

    such as the password file and dhcp/ network configurationinformation.

    /lib The shared libraries for other programs are storedhere.

    /lib/modules This holds the loadable device drivers in the

    form of kernel modules.

  • 7/27/2019 L0 Embedded Linux

    33/62

    Root File System

    /root This is the home directory for the root user.

    /sbin Programs that are normally run by the system, oras root, are stored here. The init program is stored here.

    /tmp Temporary files are created and stored here; theyare not guaranteed to survive a system restart.

    /usr This is where a lot of miscellaneous packages and

    configuration items are stored. /var This is for variable files; it stores files that change

    constantly during the operation of the system. Items suchas logs are stored here.

  • 7/27/2019 L0 Embedded Linux

    34/62

    Initial RAM disk

    The initial RAM disk (initrd) is an initial

    root file system that is mounted prior towhen the real root file system is available.

    The initrdis bound to the kernel and

    loaded as part of the kernel bootprocedure.

    The kernel then mounts this initrd as partof the two-stage boot process to load the

    modules to make the real file systemsavailable and get at the real root filesystem.

  • 7/27/2019 L0 Embedded Linux

    35/62

    Initial RAM disk

    The initrd contains a minimal set of

    directories and executables to achievethis, such as the insmodtool to installkernel modules into the kernel.

    In the case of desktop or server Linuxsystems, the initrd is a transient filesystem.

    Its lifetime is short, only serving as a

    bridge to the real root file system. In embedded systems with no mutable

    storage, the initrd is the permanent root

    file system.

  • 7/27/2019 L0 Embedded Linux

    36/62

    Busybox

    BusyBox was first written by Bruce Perens

    in 1996 for the Debian GNU/Linux setupdisk.

    The goal was to create a bootable

    GNU/Linux system on a single floppy diskthat could be used as an install andrescue disk.

    A single floppy disk can hold around 1.4-

    1.7MB, so there's not much roomavailable for the Linux kernel andassociated user applications.

  • 7/27/2019 L0 Embedded Linux

    37/62

    Busybox

    BusyBox exploits the fact that the standard Linux utilities

    share many common elements. For example, many file-based utilities (such

    asgrep and find) require code to recurse a directory insearch of files.

    When the utilities are combined into a single executable,

    they can share these common elements, which results in asmaller executable.

    In fact, BusyBox can pack almost 3.5MB of utilities intoaround 200KB. This provides greater functionality tobootable floppy disks and embedded devices that use

    Linux. The Busybox is a single statically linked executable.

  • 7/27/2019 L0 Embedded Linux

    38/62

    Busybox Linking

    Each Busybox command is created by

    creating a link to the Busybox executable.

    Create softlinks

  • 7/27/2019 L0 Embedded Linux

    39/62

    Busybox Linking

    You can also invoke BusyBox by issuing a command as an

    argument on the command line. For example, entering/bin/busybox ls

    will also cause BusyBox to behave as 'ls'.

    Of course, adding '/bin/busybox' into every command

    would be painful. So most people will invoke BusyBox using

    links to the BusyBox binary. For example, entering

    ln -s /bin/busybox ls

    ./ls

    will cause BusyBox to behave as 'ls' (if the 'ls' command

    has been compiled into BusyBox).

    Generally speaking, you should never need to make allthese links yourself, as the BusyBox build system will dothis for you when you run the 'make install' command.

    Creates soft links to busybox executable

    Different commands can be created by softlinks

  • 7/27/2019 L0 Embedded Linux

    40/62

    C Library

    Need C libraries and most commonly used is libc.

    The most commonly used implementation is that providedby the GNU C Library.

    provides all the functions defined in the standard. In fact, itcomplies with ISO C99, POSIX.1c, POSIX.1j, POSIX.1d,Unix98, and Single Unix Specification standards.

    Given that GLIBC is so comprehensive, it can be consideredtoo large for use in an embedded system.

    Lighter Invariants Embedded GLIBC

    uClibc (Clibc)

    Bionic C

  • 7/27/2019 L0 Embedded Linux

    41/62

    Boot sequence

    When system is first booted

    the processor executes code at a well-

    known location (BIOS in Flash memory). When a boot device is found, the first-

    stage boot loader is loaded into RAM and

    executed.

    its job is to load the second-stage

    boot loader.

    When the second-stage boot loader is in

    RAM and executing,

    Linux and an optional initial RAM

    disk (temporary root file system)are loaded into memory.

    The second-stage boot loader passes

    control to the kernel image and

    the kernel is decompressed and

    initialized.

    checks the system hardware,

    enumerates the attached hardware

    devices, mounts the root device,

    and then loads the necessarykernel modules.

    When complete, the first user-space

    program (init) starts, and high-level system

    initialization is performed.

    Two stages of booting system

  • 7/27/2019 L0 Embedded Linux

    42/62

    System startup

    In a PC, booting Linux begins in the BIOS

    at address 0xFFFF0.

    This address is a physical address, as theMMU has not been yet enabled.

    The first step of the BIOS is the power-onself test (POST). The job of the POST is toperform a check of the hardware.

    The second step of the BIOS is local

    device enumeration and initialization.

  • 7/27/2019 L0 Embedded Linux

    43/62

    Flash memory

  • 7/27/2019 L0 Embedded Linux

    44/62

    System startup

    The BIOS is made up of two parts:

    the POST code

    runtime services.

    After the POST is complete, it is flushed

    from memory, but the BIOS runtimeservices remain and are available to thetarget operating system.

  • 7/27/2019 L0 Embedded Linux

    45/62

    System startup

    To boot an operating system, the BIOS

    runtime searches for devices that are bothactive and bootable in the order ofpreference

    A boot device can be afloppy disk

    CD-ROM,

    partition on a hard disk,

    device on the networkUSB flash memory stick.

  • 7/27/2019 L0 Embedded Linux

    46/62

    System startup

    Commonly, Linux is booted from a hard

    disk, where the Master Boot Record (MBR)contains the primary boot loader.

    The MBR is a 512-byte sector, located in

    the first sector on the disk (sector 1 ofcylinder 0, head 0).

    After the MBR is loaded into RAM, theBIOS yields control to it.

  • 7/27/2019 L0 Embedded Linux

    47/62

    Stage 1 Boot Loader

    The primary boot loader that

    resides in the MBR is a 512-

    byte image containing both

    program code and a small

    partition table.

    The first 446 bytes are the

    primary boot loader, whichcontains both executable code

    and error message text.

    The next sixty-four bytes are

    the partition table, which

    contains a record for each of

    four partitions (sixteen byteseach).

    The MBR ends with two bytes

    that are defined as the magic

    number (0xAA55). The magic

    number serves as a validation

    check of the MBR.

  • 7/27/2019 L0 Embedded Linux

    48/62

    Stage 1 Boot Loader

    The job of the primary boot loader is to

    find and load the secondary boot loader(stage 2).

    It does this by looking through the

    partition table for an active partition.When it finds an active partition, it scansthe remaining partitions in the table toensure that they're all inactive.

    When this is verified, the active partition'sboot record is read from the device intoRAM and executed.

  • 7/27/2019 L0 Embedded Linux

    49/62

  • 7/27/2019 L0 Embedded Linux

    50/62

    GRUB

    GNU GRUB is a bootloader (can also be

    spelled boot loader) capable of loading avariety of free and proprietary operatingsystems.

    Linux, DOS, Windows, or BSD.

    GRUB can be run from or be installedto any device (floppy disk, hard disk, CD-ROM, USB drive, network drive) and can

    load operating systems from just as manylocations, including network drives.

    It can also decompress operating systemimages before booting them.

  • 7/27/2019 L0 Embedded Linux

    51/62

    Stage 2 Boot Loader

    With the second-stage boot loader in

    memory, the file system is consulted, andthe default kernel image and initrd imageare loaded into memory.

    With the images ready, the stage 2 bootloader invokes the kernel image.

  • 7/27/2019 L0 Embedded Linux

    52/62

    Kernel

    The kernel image is a compressed one.

    Typically this is a zImage (compressedimage, less than 512KB) or a bzImage(big compressed image, greater than

    512KB), that has been previouslycompressed with zlib.

    At the head of this kernel image is aroutine that does some minimal amount

    of hardware setup and then decompressesthe kernel contained within the kernelimage and places it into high memory.

  • 7/27/2019 L0 Embedded Linux

    53/62

    Kernel

    If an initial RAM disk image is present,

    this routine moves it into memory andnotes it for later use.

    The routine then calls the kernel and the

    kernel boot begins.

  • 7/27/2019 L0 Embedded Linux

    54/62

    Kernel

    When the bzImage (for an i386

    image) is invoked, you begin at./arch/i386/boot/head.S i

    n the start assembly routine.

    This routine does some basic

    hardware setup and invokes

    the startup_32 routine in./arch/i386/boot/compressed/head.S.

    This routine sets up a basic

    environment (stack, etc.) and

    clears the Block Started by

    Symbol (BSS). The kernel isthen decompressed through a

    call to a C functioncalled decompress_kernel (l

    ocated in./arch/i386/boot/compre

    ssed/misc.c).

    BSS: A data representation at themachine level, that has initial values

    when a program starts and tellsabout how much space the kernelallocates for the un-initialized data.Kernel initializes it to zero at run-time.

  • 7/27/2019 L0 Embedded Linux

    55/62

    Kernel

    When the kernel is decompressed into

    memory, it is called. This is yet anotherstartup_32 function, but this functionis in ./arch/i386/kernel/head.S .

    In the new startup_32 function (also

    called the swapper or process 0), the

    page tables are initialized and memory

    paging is enabled. The type of CPU isdetected along with any optional

    floating-point unit (FPU) and stored

    away for later use. The start_kernel function is then

    invoked (init/main.c), which takes

    you to the non-architecture specificLinux kernel.

  • 7/27/2019 L0 Embedded Linux

    56/62

    Kernel

    With the call to start_kernel, a long list

    of initialization functions are called to setup interrupts, perform further memoryconfiguration, and load the initial RAMdisk.

    In the end, a call is madeto kernel_thread(in arch/i386/kernel/process.c) to start the init function,

    which is the first user-space process. Finally, the idle task is started and the

    scheduler can now take control (after thecall to cpu_idle).

  • 7/27/2019 L0 Embedded Linux

    57/62

  • 7/27/2019 L0 Embedded Linux

    58/62

    Debugging

    One advantage of using Linux on your

    embedded target platform is you can start to develop and debug your

    application on a host-based Linux system

    such as on a Linux desktop. Once you have completed debugging on

    the host, you can then migrate yourapplication to the target.

  • 7/27/2019 L0 Embedded Linux

    59/62

    Debugging

    If the embedded target system is not high

    performance debug the target from the host platform

    this is known as cross-debugging.

    The debugger runs on the host systemand communicates with the target eithervia a direct connection to the processorvia JTAG/BDM (background debug mode),

    or over a communications port such asEthernet or a serial port.

  • 7/27/2019 L0 Embedded Linux

    60/62

    Kernel Debugging

    If the kernel crashes, you will be

    presented with a kernel oops.This is a detailed traceback of the kernel

    stack;

    provides sufficient information to identify why

    the kernel crashed.

    A core dump file contains a completesnapshot of system memory, and all

    relevant processor state such as registercontents.

    In many embedded systems there is notsufficient local storage to save the core

    dump file.

  • 7/27/2019 L0 Embedded Linux

    61/62

    Kernel Debugging

    Native debugging

    Turn on debugging info of kernel (turn onCONFIG_DEBUG_INFO)

    Use >gdb vmlinux /proc/kcore. The

    symbols for the kernel are loaded from the

    vmlinux fileThe /proc/kcore is a special driver used to

    communicate with the kernel debug agentfrom the user space gdb application.

    Cross-target kernel debuggingUse JTAG/BDM.

    Use a software based target debug agent.

    Remote kernel debugging available now.

    Use emulator such as EMU.

  • 7/27/2019 L0 Embedded Linux

    62/62

    Reference

    Pro Linux Embedded Systems by Gene

    Sally. Modern Embedded Computing by Peter

    Barry and Patrick Cowley.

    http://www.ibm.com/developerworks/linux/library/l-linuxboot/

    http://www.ibm.com/developerworks/linux/library/l-linuxboot/http://www.ibm.com/developerworks/linux/library/l-linuxboot/http://www.ibm.com/developerworks/linux/library/l-linuxboot/http://www.ibm.com/developerworks/linux/library/l-linuxboot/http://www.ibm.com/developerworks/linux/library/l-linuxboot/http://www.ibm.com/developerworks/linux/library/l-linuxboot/