embedded linux quick start guide

44
Embedded Linux Quick Start Guide using Buildroot and BeagleBone Black 2net Ltd v1.4, May 2016 Embedded Linux Quick Start Guide 1 Copyright © 2011-2016, 2net Ltd

Upload: chris-simmonds

Post on 15-Jan-2017

1.832 views

Category:

Software


10 download

TRANSCRIPT

Embedded Linux Quick Start Guideusing Buildroot and BeagleBone Black

2net Ltd

v1.4, May 2016

Embedded Linux Quick Start Guide 1 Copyright © 2011-2016, 2net Ltd

License

These slides are available under a Creative Commons Attribution-ShareAlike 3.0license. You can read the full text of the license herehttp://creativecommons.org/licenses/by-sa/3.0/legalcodeYou are free to

• copy, distribute, display, and perform the work

• make derivative works

• make commercial use of the work

Under the following conditions

• Attribution: you must give the original author credit

• 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.e. include thispage exactly as it is)

• For any reuse or distribution, you must make clear to others the license terms ofthis work

Embedded Linux Quick Start Guide 2 Copyright © 2011-2016, 2net Ltd

About Chris Simmonds• Consultant and trainer• Author of Mastering Embedded Linux

Programming• Working with embedded Linux since 1999• Android since 2009• Speaker at many conferences and

workshops"Looking after the Inner Penguin" blog at http://2net.co.uk/

https://uk.linkedin.com/in/chrisdsimmonds/

https://google.com/+chrissimmonds

Embedded Linux Quick Start Guide 3 Copyright © 2011-2016, 2net Ltd

Overview

• This is a quick introduction to embedded Linux

• It includes hands-on exercises that will take you fromzero to command prompt in half a day

• The target board is a BeagleBone Black

• It uses Buildroot to generate the Linux distro

Embedded Linux Quick Start Guide 4 Copyright © 2011-2016, 2net Ltd

Topics

• Getting started with embedded Linux

• The four elements

• Booting and running the BeagleBone Black

• Hands-on with the BeagleBone Black

Embedded Linux Quick Start Guide 5 Copyright © 2011-2016, 2net Ltd

What is embedded Linux?

• Embedded Linux = Linux running on embeddedhardware

• "Linux" in the broad sense: a Linux kernel plus otheropen source packages needed to make a workingsystem

• Hard to define: applications range from the small(light bulbs) to the large (aircraft AV systems) andbeyond (large network switches)

• ... from the trivial (light bulbs) to the critical (industrialplant; self-driving cars)

• Linux is commonly used in mission criticalapplications, but not (yet) safely critical

Embedded Linux Quick Start Guide 6 Copyright © 2011-2016, 2net Ltd

Driving factors

• Moore’s law: complex hardware requires complexsoftware

• Free: you have the freedom to get and modify thecode, making it easy to adapt and extend

• Functional: supports a (very) wide range of hardware

• Up to date: the kernel has a three month releasecycle

• Free: there is no charge for using the source code

Embedded Linux Quick Start Guide 7 Copyright © 2011-2016, 2net Ltd

Open source ecosystem

The main players are:

• Open source community

• A loose alliance of developers, working on 1000’s ofindividual projects, some funded by companies with acommercial interest

• SoC vendors

• Customise upstream code (e.g. Linux kernel,toolchain) to ensure it works well on their platforms

• SBC and SoM vendors: further customisation

• Commercial embedded Linux vendors: offer supportand services

Embedded Linux Quick Start Guide 8 Copyright © 2011-2016, 2net Ltd

Pain points

• Lack of support for your particular hardware (alwayscheck with the manufacturer before you design acomponent in)

• Lack of knowledge (that’s why I am here)

• Underestimating the task

Embedded Linux Quick Start Guide 9 Copyright © 2011-2016, 2net Ltd

The four elements

Embedded Linux Quick Start Guide 10 Copyright © 2011-2016, 2net Ltd

Overview

Every embedded Linux project has these elements:

• Toolchain: to compile the other elements

• Bootloader: to initialise the board and load the kernel

• Kernel: to manage system resources

• Root filesystem: to run applications

Embedded Linux Quick Start Guide 11 Copyright © 2011-2016, 2net Ltd

Element 1: Toolchain

• Toolchain = GNU GCC + C library + GNU GDB

• LLVM/Clang is also an option, but not quitemainstream yet

• Native toolchain

• Install and develop on the target

• Cross toolchain

• Build on development system, deploy on target

• Keeps target and development environmentsseparate

• Cross toolchains are the most common

Embedded Linux Quick Start Guide 12 Copyright © 2011-2016, 2net Ltd

Choosing a C library

• The C library is the interface between user space andkernel

• Three options

Library NotesGNU glibc Big, good support for POSIX and extensionsuClibc Small: a good choice for memory-constrained systemsmusl libc Small: an alternative to uClibc. BSD license

Embedded Linux Quick Start Guide 13 Copyright © 2011-2016, 2net Ltd

Getting a toolchain

• You can:

• Build from upstream source, e.g. using CrosstoolNG

• Download from a trusted third party, e.g. Linaro forARM devices

• Use the one provided by your SoC/board vendor(check quality first)

• Use an embedded build system to generate one

• We will be using an embedded build system(Buildroot)

Embedded Linux Quick Start Guide 14 Copyright © 2011-2016, 2net Ltd

Element 2: Bootloader

• Tasks

• Initialise board

• Load kernel

• Maintenance tasks, e.g. flash system images

• Open source bootloaders

• Das U-Boot

• Barebox

• GRUB 2 (for X86 and X86_64)

Embedded Linux Quick Start Guide 15 Copyright © 2011-2016, 2net Ltd

Das U-Boot

www.denx.de/wiki/U-Boot

• Open source, GPL license

• Small run-time binary (50 - 800 KiB)

• Supports many CPU architectures, incl. ARM, MIPS,PowerPC, SH

• ... and many boards (> 1000)

• Reasonably easy to port to a new board

Embedded Linux Quick Start Guide 16 Copyright © 2011-2016, 2net Ltd

U-Boot command-lineLoad a kernel image into memory from...

• NAND flashUboot# nand read 80100000 1000000 200000

• eMMC or SD cardUBoot# mmc rescan 1UBoot# fatload mmc 1:1 80100000 zimage

• TFTP serverUBoot# setenv ipaddr 192.168.1.2UBoot# setenv serverip 192.168.1.1UBoot# tftp 80100000 zImage

• Boot a kernel image (already loaded into memory)UBoot# bootz 80100000

Embedded Linux Quick Start Guide 17 Copyright © 2011-2016, 2net Ltd

U-Boot environment

• Contains parameters, e.g. ipaddr and serverip fromprevious slide

• Parameters can be created or modified at run-timeusing setenv

• Default set is hard-coded in U-Boot

• At boot, additional parameters may be read from:

• Dedicated area of flash memory

• A file named boot.scr

• A file named uEnv.txt

Embedded Linux Quick Start Guide 18 Copyright © 2011-2016, 2net Ltd

Element 3: Kernel

https://www.kernel.org

• Tasks

• Manage system resources: CPU, memory, I/O

• Interface with hardware via device drivers

• Provide a (mostly) hardware-independent API

• Rapid development cycle: new version every 12weeks

Embedded Linux Quick Start Guide 19 Copyright © 2011-2016, 2net Ltd

Kernel versions

Up to May 2011

From July 2011

From April 2015

2.6.39.1

3.0.1

4.0.1

Release number: changesevery 12 weeks or so

Bug fix number: changes everytime a bug is fixed

Embedded Linux Quick Start Guide 20 Copyright © 2011-2016, 2net Ltd

Mainline, stable and longterm• Mainline: Linus Torvald’s development tree

• Stable: bug fixes to the previous mainline release

• Longterm: selected versions that are supported for>= 2 years

• 4.4

• 4.1

• 3.18

• 3.14

• 3.12

• 3.10

• 3.4

Embedded Linux Quick Start Guide 21 Copyright © 2011-2016, 2net Ltd

Vendor kernels

• Mainline Linux has good support for x86/x86_64

• SoC vendors take mainline Linux and customise tosupport their chips

• Vendor kernels lag behind mainline

• Vendors don’t take every mainline release: typicallyonly one per year

• Most vendors are not very good at pushing theirchanges into mainline

• Most of the time you will be not be working withmainline Linux

Embedded Linux Quick Start Guide 22 Copyright © 2011-2016, 2net Ltd

Digression: device trees

• The kernel needs to know details about hardware

• To decide which drivers to initialise

• To configure device parameters such as registeraddresses and IRQ

• Sources of information:

• Firmware ACPI tables (x86 and ARM server)

• Bus enumeration, e.g. PCI

• Hard coded structures

• Device tree (ARM, PPC, MIPS, some x86)

Embedded Linux Quick Start Guide 23 Copyright © 2011-2016, 2net Ltd

Device Tree

• Open Firmware specification, IEEE-1275-1994

• Description of hardware (contains no code)

• Tree of nodes, containing attributes, for example:/dts-v1/;/{

model = "TI AM335x BeagleBone";compatible = "ti,am33xx";#address-cells = <1>;#size-cells = <1>;memory@0x80000000 {

device_type = "memory";reg = <0x80000000 0x20000000>; /* 512 MB */

};[...]

Embedded Linux Quick Start Guide 24 Copyright © 2011-2016, 2net Ltd

Compiling the device tree

• .dts source files are in arch/<ARCH>/boot/dts

• compiled to .dtb, using dtc

• dtb file is loaded into memory by the bootloader

• The U-boot bootz command takes three arguments

bootz <kernel> <ramdisk> <dt binary>

• If there is no ramdisk (which is common)

bootz <kernel> - <dt binary>

Embedded Linux Quick Start Guide 25 Copyright © 2011-2016, 2net Ltd

Element 4: Root filesystem

• The kernel contains no applications: these are in theroot filesystem

• The last stage of kernel boot is to launch the firstprogram: /sbin/init

• init runs a number of scripts that start up the rest ofthe system

Embedded Linux Quick Start Guide 26 Copyright © 2011-2016, 2net Ltd

Root filesystem layout

• Linux file layout is defined in Filesystem HierarchyStandard (FHS)http://refspecs.linuxfoundation.org/fhs.shtml

Directory Notes/bin Programs/dev Device nodes/etc Configuration/lib Dynamically-loaded libraries/sbin Binaries for superuser/tmp Temporary files/usr Additional binaries and libraries/var Non-volatile data

Embedded Linux Quick Start Guide 27 Copyright © 2011-2016, 2net Ltd

Embedded build systems

• Building the four elements by hand is time consuming

• Embedded build systems make it easy

Tool Notesbuildroot Small, menu-drivenOpenWrt A variant of Buildroot for network devicesOpenEmbedded General purposeTizen Good for mobile and automotiveYocto Project General purpose, wide industry support, complex

Embedded Linux Quick Start Guide 28 Copyright © 2011-2016, 2net Ltd

Buildroot

www.buildroot.org

• Simple and fast: ideal for small embedded projects

• Has configurations for c. 100 dev boards

• Has c. 1000 packages

• incl. network tools, databases, Qt5 graphics

Embedded Linux Quick Start Guide 29 Copyright © 2011-2016, 2net Ltd

Buildroot

Embedded Linux Quick Start Guide 30 Copyright © 2011-2016, 2net Ltd

Directories

• Upstream source -> dl/

• Build artifacts -> output/

• output/ contains

Directory Notesbuild Working directory for compiling sourcehost Tools that run on the host, incl. toolchainimages bootloader, kernel and root filesystem for the targetstaging Link to the sysroot of the toolchaintarget Staging area for target root filesystem

Embedded Linux Quick Start Guide 31 Copyright © 2011-2016, 2net Ltd

The BeagleBone Black

Embedded Linux Quick Start Guide 32 Copyright © 2011-2016, 2net Ltd

The BeagleBone Black

• Open source hardware design fromhttp://beagleboard.org

• Low cost ($55)• Extensible via stackable daughter

boards, called capes

Embedded Linux Quick Start Guide 33 Copyright © 2011-2016, 2net Ltd

Features

• TI AM335x 1GHz Cortex-A8 SoC

• Imagination Tech. PowerVR SGX530 GPU

• 512 MiB DDR3 RAM

• 2 or 4 GiB 8-bit eMMC on-board flash storage

• MicroSD card slot for external storage

• Mini USB OTG port, also provides power

• Full size USB 2.0 host

• 10/100 Ethernet

• Mini HDMI connector

Embedded Linux Quick Start Guide 34 Copyright © 2011-2016, 2net Ltd

Memory map

0x0000_0000

1 GiB memory-mappedperipherals

512 MiBDDR3 RAM

0x8000_0000

0xA000_0000

0xFFFF_FFFF

0x4000_0000

Embedded Linux Quick Start Guide 35 Copyright © 2011-2016, 2net Ltd

Storage

• MMC0: External microSD (4-bit)

• MMC1: 2/4 GiB internal eMMC (8-bit)

Embedded Linux Quick Start Guide 36 Copyright © 2011-2016, 2net Ltd

Switches

Power

Reset

Boot

Embedded Linux Quick Start Guide 37 Copyright © 2011-2016, 2net Ltd

Boot sequence

• Default:

• boot from internal eMMC

• If boot switch is pressed while power is applied:

• Try to boot from microSD card

• If no SD card present, try to load an image via USBport, followed by serial port

Embedded Linux Quick Start Guide 38 Copyright © 2011-2016, 2net Ltd

Boot filesWhen booting from MMC (eMMC or SD):

• The first partition is mounted

• Must be FAT32 (vfat) format

• Must have boot flag set

• Load and execute 1st stage boot in MLO

• Load and execute 2nd stage boot in u-boot.img

• U-Boot loads

• Linux kernel: zImage

• Device tree binary: am335x-boneblack.dtb

• U-Boot starts Linux

Embedded Linux Quick Start Guide 39 Copyright © 2011-2016, 2net Ltd

LEDs

usr 0

usr 1

usr 2

usr 3

Embedded Linux Quick Start Guide 40 Copyright © 2011-2016, 2net Ltd

Conclusion

Embedded Linux Quick Start Guide 41 Copyright © 2011-2016, 2net Ltd

The story so far

• We have investigated the four elements of embeddedLinux:

• Toolchain

• Bootloder

• Kernel

• Root filesystem

• We used Buildroot to create these elements

• We tried it out using a BeagleBone Black target

Embedded Linux Quick Start Guide 42 Copyright © 2011-2016, 2net Ltd

Delving deeper

• This is an excerpt from my Fast track to embeddedLinux class

• If you would like to discover more about the power ofembedded Linux, visithttp://www.2net.co.uk/training.html and enquireabout training classes for your company

• 2net training is available world-wide

• Also, my book, Mastering Embedded LinuxProgramming, covers the topics discused here inmuch greater detail

Embedded Linux Quick Start Guide 43 Copyright © 2011-2016, 2net Ltd

Lab

Time to do the lab!

The instructions are in the workbookhttp://www.2net.co.uk/downloads/

embedded-linux-quick-start-workbook.pdf

Embedded Linux Quick Start Guide 44 Copyright © 2011-2016, 2net Ltd