Share E-Book

Mastering Embedded Linux Programming Unleash the full potential of Embedded Linux with Linux 4.9 and Yocto Project 2.2 (Morty)… (Chris Simmonds)(Z-Library)

Author

Programming
Language English

Key Features Discover how to build and configure reliable embedded Linux devices This book has been updated to include Linux 4.9 and Yocto Project 2.2 (Morty) This comprehensive guide covers the remote update of devices in the field and power management Book Description Embedded Linux runs many of the devices we use every day, from smart TVs to WiFi routers, test equipment to industrial controllers - all of them have Linux at their heart. Linux is a core technology in the implementation of the inter-connected world of the Internet of Things. The comprehensive guide shows you the technologies and techniques required to build Linux into embedded systems. You will begin by learning about the fundamental elements that underpin all embedded Linux projects: the toolchain, the bootloader, the kernel, and the root filesystem. You'll see how to create each of these elements from scratch, and how to automate the process using Buildroot and the Yocto Project. Moving on, you'll find out how to implement an effective storage strategy for flash memory chips, and how to install updates to the device remotely once it is deployed. You'll also get to know the key aspects of writing code for embedded Linux, such as how to access hardware from applications, the implications of writing multi-threaded code, and techniques to manage memory in an efficient way. The final chapters show you how to debug your code, both in applications and in the Linux kernel, and how to profile the system so that you can look out for performance bottlenecks. By the end of the book, you will have a complete overview of the steps required to create a successful embedded Linux system. What you will learn Evaluate the Board Support Packages offered by most manufacturers of a system on chip or embedded module Use Buildroot and the Yocto Project to create embedded Linux systems quickly and efficiently Update IoT devices in the field without compromising security Reduce the power budget of devices to make batte

Format PDF
Size 5.1 MB
5
Views
(First 20 pages)

Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

Page 1
(This page has no text content)
Page 2
Mastering Embedded Linux Programming Second Edition Unleash the full potential of Embedded Linux Chris Simmonds BIRMINGHAM - MUMBAI
Page 3
Mastering Embedded Linux Programming Second Edition Copyright © 2017 Packt Publishing All rights reserved. No part of this book may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, without the prior written permission of the publisher, except in the case of brief quotations embedded in critical articles or reviews. Every effort has been made in the preparation of this book to ensure the accuracy of the information presented. However, the information contained in this book is sold without warranty, either express or implied. Neither the author, nor Packt Publishing, and its dealers and distributors will be held liable for any damages caused or alleged to be caused directly or indirectly by this book. Packt Publishing has endeavored to provide trademark information about all of the companies and products mentioned in this book by the appropriate use of capitals. However, Packt Publishing cannot guarantee the accuracy of this information. First published: December 2015 Second edition: June 2017 Production reference: 1280617 Published by Packt Publishing Ltd. Livery Place 35 Livery Street Birmingham B3 2PB, UK. ISBN 978-1-78728-328-2 www.packtpub.com
Page 4
Credits Author Chris Simmonds Copy Editors Madhusudan Uchil Stuti Shrivastava Reviewers Daiane Angolini Otavio Salvador Alex Tereschenko Project Coordinator Virginia Dias Commissioning Editor Kartikey Pandey Proofreader Safis Editing Acquisition Editor Prateek Bharadwaj Indexer Rekha Nair Content Development Editor Sharon Raj Graphics Kirk D'Penha Technical Editor Vishal Kamal Mewada Production Coordinator Melwyn Dsa
Page 5
About the Author Chris Simmonds is a software consultant and trainer living in southern England. He has almost two decades of experience in designing and building open-source embedded systems. He is the founder and chief consultant at 2net Ltd, which provides professional training and mentoring services in embedded Linux, Linux device drivers, and Android platform development. He has trained engineers at many of the biggest companies in the embedded world, including ARM, Qualcomm, Intel, Ericsson, and General Dynamics. He is a frequent presenter at open source and embedded conferences, including the Embedded Linux Conference and Embedded World. You can see some of his work on the Inner Penguin blog at www.2net.co.uk. I would like to thank Shirley Simmonds for being so supportive during the long hours that I was shut in my home office researching and writing this book. I would also like to thank all the people who have helped me with the research of the technical aspects of this book, whether they realized that is what they were doing or not. In particular, I would like to mention Klaas van Gend, Thomas Petazzoni, and Ralph Nguyen for their help and advice. Lastly, I would like to thank Sharon Raj, Vishal Mewada, and the team at Packt Publishing for keeping me on track and bringing the book to fruition.
Page 6
About the Reviewers Daiane Angolini has been working with embedded Linux since 2008. She has been working as an application engineer at NXP, acting on internal development, porting custom applications from Android, and on-customer support for i.MX architectures in areas such as Linux kernel, u-boot, Android, Yocto Project, and user-space applications. However, it was on the Yocto Project that she found her place. She has coauthored the books Embedded Linux Development with Yocto Project and Heading for the Yocto Project, and learned a lot in the process. Otavio Salvador loves technology and started his free software activities in 1999. In 2002, he founded O.S. Systems, a company focused on embedded system development services and consultancy worldwide, creating and maintaining customized BSPs, and helping companies with their product's development challenges. This resulted in him joining the OpenEmbedded community in 2008, when he became an active contributor to the OpenEmbedded project. He has coauthored the books Embedded Linux Development with Yocto Project and Heading for the Yocto Project. Alex Tereschenko is an embedded systems engineer by day, and an avid maker by night, who is convinced that computers can do a lot of good for people when they are interfaced with real-world objects, as opposed to just crunching data in a dusty corner. That's what's driving him in his projects, and this is why embedded systems and the Internet of Things are the topics he enjoys the most.
Page 7
www.PacktPub.com For support files and downloads related to your book, please visit www.PacktPub.com. Did you know that Packt offers eBook versions of every book published, with PDF and ePub files available? You can upgrade to the eBook version at www.PacktPub.comand as a print book customer, you are entitled to a discount on the eBook copy. Get in touch with us at service@packtpub.com for more details. At www.PacktPub.com, you can also read a collection of free technical articles, sign up for a range of free newsletters and receive exclusive discounts and offers on Packt books and eBooks. h t t p s ://w w w . p a c k t p u b . c o m /m a p t Get the most in-demand software skills with Mapt. Mapt gives you full access to all Packt books and video courses, as well as industry-leading tools to help you plan your personal development and advance your career. Why subscribe? Fully searchable across every book published by Packt Copy and paste, print, and bookmark content On demand and accessible via a web browser
Page 8
Customer Feedback Thanks for purchasing this Packt book. At Packt, quality is at the heart of our editorial process. To help us improve, please leave us an honest review on this book's Amazon page at h t t p s ://w w w . a m a z o n . c o m /d p /1787283283. If you'd like to join our team of regular reviewers, you can e-mail us at customerreviews@packtpub.com. We award our regular reviewers with free eBooks and videos in exchange for their valuable feedback. Help us be relentless in improving our products!
Page 9
Table of Contents Preface 1 Chapter 1: Starting Out 8 Selecting the right operating system 9 The players 10 Project life cycle 12 The four elements of embedded Linux 12 Open source 13 Licenses 13 Hardware for embedded Linux 15 Hardware used in this book 16 The BeagleBone Black 16 QEMU 17 Software used in this book 18 Summary 19 Chapter 2: Learning About Toolchains 20 Introducing toolchains 21 Types of toolchains 22 CPU architectures 23 Choosing the C library 25 Finding a toolchain 26 Building a toolchain using crosstool-NG 28 Installing crosstool-NG 28 Building a toolchain for BeagleBone Black 28 Building a toolchain for QEMU 30 Anatomy of a toolchain 31 Finding out about your cross compiler 31 The sysroot, library, and header files 34 Other tools in the toolchain 34 Looking at the components of the C library 35 Linking with libraries – static and dynamic linking 36 Static libraries 37 Shared libraries 38 Understanding shared library version numbers 39 The art of cross compiling 40
Page 10
[ ii ] Simple makefiles 40 Autotools 41 An example: SQLite 43 Package configuration 44 Problems with cross compiling 45 Summary 46 Chapter 3: All About Bootloaders 47 What does a bootloader do? 48 The boot sequence 48 Phase 1 – ROM code 49 Phase 2 – secondary program loader 50 Phase 3 – TPL 51 Booting with UEFI firmware 52 Moving from bootloader to kernel 53 Introducing device trees 54 Device tree basics 55 The reg property 56 Labels and interrupts 57 Device tree include files 58 Compiling a device tree 60 Choosing a bootloader 60 U-Boot 61 Building U-Boot 61 Installing U-Boot 63 Using U-Boot 65 Environment variables 66 Boot image format 66 Loading images 67 Booting Linux 69 Automating the boot with U-Boot scripts 69 Porting U-Boot to a new board 70 Board-specific files 71 Configuring header files 73 Building and testing 73 Falcon mode 75 Barebox 75 Getting barebox 75 Building barebox 76 Using barebox 77 Summary 78
Page 11
[ iii ] Chapter 4: Configuring and Building the Kernel 79 What does the kernel do? 80 Choosing a kernel 82 Kernel development cycle 82 Stable and long term support releases 83 Vendor support 84 Licensing 84 Building the kernel 85 Getting the source 85 Understanding kernel configuration – Kconfig 86 Using LOCALVERSION to identify your kernel 90 Kernel modules 91 Compiling – Kbuild 92 Finding out which kernel target to build 92 Build artifacts 93 Compiling device trees 95 Compiling modules 95 Cleaning kernel sources 96 Building a kernel for the BeagleBone Black 96 Building a kernel for QEMU 96 Booting the kernel 97 Booting the BeagleBone Black 97 Booting QEMU 98 Kernel panic 98 Early user space 98 Kernel messages 99 Kernel command line 100 Porting Linux to a new board 101 A new device tree 102 Setting the board compatible property 103 Additional reading 105 Summary 105 Chapter 5: Building a Root Filesystem 107 What should be in the root filesystem? 108 The directory layout 109 The staging directory 110 POSIX file access permissions 111 File ownership permissions in the staging directory 112
Page 12
[ iv ] Programs for the root filesystem 113 The init program 113 Shell 113 Utilities 114 BusyBox to the rescue! 114 Building BusyBox 115 ToyBox – an alternative to BusyBox 116 Libraries for the root filesystem 116 Reducing the size by stripping 118 Device nodes 119 The proc and sysfs filesystems 120 Mounting filesystems 121 Kernel modules 122 Transferring the root filesystem to the target 122 Creating a boot initramfs 123 Standalone initramfs 123 Booting the initramfs 124 Booting with QEMU 124 Booting the BeagleBone Black 124 Mounting proc 125 Building an initramfs into the kernel image 125 Building an initramfs using a device table 126 The old initrd format 127 The init program 127 Starting a daemon process 128 Configuring user accounts 129 Adding user accounts to the root filesystem 131 A better way of managing device nodes 131 An example using devtmpfs 131 An example using mdev 132 Are static device nodes so bad after all? 133 Configuring the network 133 Network components for glibc 134 Creating filesystem images with device tables 135 Booting the BeagleBone Black 137 Mounting the root filesystem using NFS 137 Testing with QEMU 139 Testing with the BeagleBone Black 140 Problems with file permissions 140 Using TFTP to load the kernel 140 Additional reading 141
Page 13
[ v ] Summary 142 Chapter 6: Selecting a Build System 143 Build systems 143 Package formats and package managers 145 Buildroot 146 Background 146 Stable releases and long-term support 147 Installing 147 Configuring 147 Running 148 Creating a custom BSP 150 U-Boot 150 Linux 152 Build 153 Adding your own code 156 Overlays 156 Adding a package 157 License compliance 158 The Yocto Project 159 Background 159 Stable releases and supports 161 Installing the Yocto Project 161 Configuring 162 Building 163 Running the QEMU target 164 Layers 165 BitBake and recipes 167 Customizing images via local.conf 170 Writing an image recipe 171 Creating an SDK 172 The license audit 174 Further reading 174 Summary 175 Chapter 7: Creating a Storage Strategy 176 Storage options 177 NOR flash 177 NAND flash 178 Managed flash 180 MultiMediaCard and Secure Digital cards 180 eMMC 181
Page 14
[ vi ] Other types of managed flash 181 Accessing flash memory from the bootloader 182 U-Boot and NOR flash 182 U-Boot and NAND flash 183 U-Boot and MMC, SD, and eMMC 183 Accessing flash memory from Linux 183 Memory technology devices 183 MTD partitions 184 MTD device drivers 187 The MTD character device, mtd 187 The MTD block device, mtdblock 189 Logging kernel oops to MTD 189 Simulating NAND memory 189 The MMC block driver 190 Filesystems for flash memory 190 Flash translation layers 190 Filesystems for NOR and NAND flash memory 191 JFFS2 192 Summary nodes 192 Clean markers 193 Creating a JFFS2 filesystem 193 YAFFS2 194 Creating a YAFFS2 filesystem 195 UBI and UBIFS 196 UBI 196 UBIFS 199 Filesystems for managed flash 201 Flashbench 201 Discard and TRIM 202 Ext4 203 F2FS 204 FAT16/32 205 Read-only compressed filesystems 205 squashfs 206 Temporary filesystems 207 Making the root filesystem read-only 208 Filesystem choices 209 Further reading 209 Summary 210 Chapter 8: Updating Software in the Field 211 What to update? 212
Page 15
[ vii ] Bootloader 213 Kernel 213 Root filesystem 213 System applications 214 Device-specific data 214 Components that need to be updated 214 The basics of software update 214 Making updates robust 215 Making updates fail-safe 216 Making updates secure 218 Types of update mechanism 219 Symmetric image update 219 Asymmetric image update 220 Atomic file updates 221 OTA updates 223 Using Mender for local updates 224 Building the Mender client 224 Installing an update 226 Using Mender for OTA updates 229 Summary 233 Chapter 9: Interfacing with Device Drivers 235 The role of device drivers 236 Character devices 237 Block devices 239 Network devices 240 Finding out about drivers at runtime 241 Getting information from sysfs 244 The devices: /sys/devices 244 The drivers: /sys/class 245 The block drivers: /sys/block 246 Finding the right device driver 246 Device drivers in user space 247 GPIO 248 Handling interrupts from GPIO 249 LEDs 251 I2C 252 Serial Peripheral Interface (SPI) 254 Writing a kernel device driver 254 Designing a character driver interface 255
Page 16
[ viii ] The anatomy of a device driver 256 Compiling kernel modules 259 Loading kernel modules 260 Discovering the hardware configuration 261 Device trees 261 The platform data 262 Linking hardware with device drivers 263 Additional reading 265 Summary 265 Chapter 10: Starting Up – The init Program 266 After the kernel has booted 266 Introducing the init programs 267 BusyBox init 268 Buildroot init scripts 269 System V init 270 inittab 272 The init.d scripts 274 Adding a new daemon 275 Starting and stopping services 276 systemd 277 Building systemd with the Yocto Project and Buildroot 277 Introducing targets, services, and units 278 Units 278 Services 280 Targets 280 How systemd boots the system 281 Adding your own service 281 Adding a watchdog 283 Implications for embedded Linux 284 Further reading 284 Summary 285 Chapter 11: Managing Power 286 Measuring power usage 287 Scaling the clock frequency 289 The CPUFreq driver 290 Using CPUFreq 291 Selecting the best idle state 294 The CPUIdle driver 295 Tickless operation 298
Page 17
[ ix ] Powering down peripherals 298 Putting the system to sleep 300 Power states 301 Wakeup events 302 Timed wakeups from the real-time clock 303 Further reading 303 Summary 304 Chapter 12: Learning About Processes and Threads 305 Process or thread? 306 Processes 308 Creating a new process 308 Terminating a process 309 Running a different program 310 Daemons 313 Inter-process communication 313 Message-based IPC 314 Unix (or local) sockets 315 FIFOs and named pipes 315 POSIX message queues 315 Summary of message-based IPC 316 Shared memory-based IPC 316 POSIX shared memory 316 Threads 320 Creating a new thread 321 Terminating a thread 322 Compiling a program with threads 322 Inter-thread communication 323 Mutual exclusion 324 Changing conditions 324 Partitioning the problem 326 Scheduling 327 Fairness versus determinism 328 Time-shared policies 328 Niceness 329 Real-time policies 330 Choosing a policy 331 Choosing a real-time priority 332 Further reading 332 Summary 333 Chapter 13: Managing Memory 334
Page 18
[ x ] Virtual memory basics 335 Kernel space memory layout 336 How much memory does the kernel use? 337 User space memory layout 339 The process memory map 341 Swapping 342 Swapping to compressed memory (zram) 343 Mapping memory with mmap 343 Using mmap to allocate private memory 344 Using mmap to share memory 344 Using mmap to access device memory 344 How much memory does my application use? 345 Per-process memory usage 346 Using top and ps 347 Using smem 348 Other tools to consider 350 Identifying memory leaks 350 mtrace 350 Valgrind 351 Running out of memory 353 Further reading 355 Summary 355 Chapter 14: Debugging with GDB 356 The GNU debugger 356 Preparing to debug 357 Debugging applications 358 Remote debugging using gdbserver 358 Setting up the Yocto Project for remote debugging 359 Setting up Buildroot for remote debugging 360 Starting to debug 361 Connecting GDB and gdbserver 361 Setting the sysroot 362 GDB command files 364 Overview of GDB commands 365 Breakpoints 365 Running and stepping 365 Getting information 366 Running to a breakpoint 366 Native debugging 367 The Yocto Project 367 Buildroot 368
Page 19
[ xi ] Just-in-time debugging 368 Debugging forks and threads 369 Core files 369 Using GDB to look at core files 371 GDB user interfaces 372 Terminal user interface 372 Data display debugger 373 Eclipse 374 Debugging kernel code 374 Debugging kernel code with kgdb 375 A sample debug session 376 Debugging early code 377 Debugging modules 378 Debugging kernel code with kdb 379 Looking at an Oops 380 Preserving the Oops 383 Further reading 384 Summary 385 Chapter 15: Profiling and Tracing 386 The observer effect 387 Symbol tables and compile flags 387 Beginning to profile 388 Profiling with top 389 Poor man's profiler 390 Introducing perf 391 Configuring the kernel for perf 391 Building perf with the Yocto Project 392 Building perf with Buildroot 392 Profiling with perf 393 Call graphs 395 perf annotate 396 Other profilers – OProfile and gprof 397 Tracing events 399 Introducing Ftrace 400 Preparing to use Ftrace 400 Using Ftrace 401 Dynamic Ftrace and trace filters 403 Trace events 404 Using LTTng 406
Page 20
[ xii ] LTTng and the Yocto Project 406 LTTng and Buildroot 406 Using LTTng for kernel tracing 407 Using Valgrind 410 Callgrind 410 Helgrind 411 Using strace 411 Summary 414 Chapter 16: Real-Time Programming 415 What is real time? 416 Identifying sources of non-determinism 418 Understanding scheduling latency 419 Kernel preemption 420 The real-time Linux kernel (PREEMPT_RT) 421 Threaded interrupt handlers 422 Preemptible kernel locks 424 Getting the PREEMPT_RT patches 424 The Yocto Project and PREEMPT_RT 426 High-resolution timers 426 Avoiding page faults 427 Interrupt shielding 428 Measuring scheduling latencies 428 cyclictest 428 Using Ftrace 432 Combining cyclictest and Ftrace 434 Further reading 435 Summary 435 Index 437
The above is a preview of the first 20 pages. Register to read the complete e-book.

Recommended for You

Loading recommended books...
Failed to load, please try again later

Tip the Site

Scan the WeChat Pay or Alipay code to tip. No login required.

WeChat Pay
Alipay
Back to List