Golden Notes and Roadmap For Embedded Linux 1702891166
Golden Notes and Roadmap For Embedded Linux 1702891166
1
Copyright 2023 bhstalel@gmail.com
Operating System
User
● Memory (MMU)
● Process
● Devices (Drivers)
● Storage
Operating System ● CPU (Scheduling)
● Networking
● …
2
Copyright 2023 bhstalel@gmail.com
Unix
Multics Unix
1960s
● Unix provides:
○ Processes
○ More utilities
Ken Thompson - Dennis Ritchie
3
Copyright 2023 bhstalel@gmail.com
POSIX
• Provides portability
• Defines:
C Library
• System interfaces and headers
4
Copyright 2023 bhstalel@gmail.com
GNU
1960s 1983
Linux
● Advantages:
○ Community support
6
Copyright 2023 bhstalel@gmail.com
Unix
7
Copyright 2023 bhstalel@gmail.com
Architecture
User Space:
Applications
- Unprivileged mode
Hardware
8
Copyright 2023 bhstalel@gmail.com
Shell
● Running an application does not mean running it’s instructions, it means it has to
inform the Kernel to run it using the exec* system calls
● They all serve the same purpose, they differ in syntax and interpretations
9
Copyright 2023 bhstalel@gmail.com
Shell | Example
$ sleep 5 /usr/bin/sleep
1. check
Shell PATH
2. fork: make a copy from itself 3. exec: Replace the copy with sleep
4. wait
sleep
10
Copyright 2023 bhstalel@gmail.com
What’s next?
● System calls are defined in ABI: Application Binary Interface, read about it
● It is recommended to understand some Assembly because it is arch-specific
○ Understand how the Kernel handles the system call
○ Read about vDSO
○ Try to develop something without using the C Library
● Understand the ELF: Executable & Linkable Format binary format in Linux
○ Understand the sections and headers
○ It has .data, .text, .bss (You probably heard of that before)
○ Learn how the Kernel runs an application using a shebang
● Learn about the Root file system (rootfs) and all folders under it (bin, sbin, etc, var,
boot, usr, …) and pseudo-filesystems (Essentially procfs and sysfs, …)
11
Copyright 2023 bhstalel@gmail.com
What’s next?
● Learn about the known shell utilities, known as Coreutils (ls, cd, mkdir, cp, mv, …)
12
Copyright 2023 bhstalel@gmail.com
Compilation
CFLAGS LDFLAGS
.c .i .s .o Ex
+:
readelf
objdump
ar
…
Compiler Binutils
C Library
Libraries - Static Linking (.o + .o)
Toolchain - Dynamic Linking: (.o + .so:
Utilities
= called sysroot Shared Object)
- Static Library (.a: Archive):
contains .o files
- (.so created with -shared)
SDK: Software Development Kit
- Static linking: -static
13
Copyright 2023 bhstalel@gmail.com
What’s next?
14
Copyright 2023 bhstalel@gmail.com
Types of compilation
= = ~Native
= != ~Cross
BUILD HOST TARGET
!= = ~Cross-Native
!= != ~Canadian
● Target: The architecture of the part that will run the compiled binary
● Example:
○ Building on an intel i7 x86-64 with gcc and runs on the same PC: Native
○ Building on an intel i7 x86-64 with arm-gcc and runs on RPI: Cross
○ Building a gcc on intel i7 x86-64 to run on RPI and build for RPI: Cross-Native
○ Canadian is not really used, or really ?
15
Copyright 2023 bhstalel@gmail.com
Cross Compilation
● To cross compile for a Linux target system, you need to answer 4 questions:
○ What C Library used in the target system?
○ What Architecture?
○ What ABI is used for the target architecture?
○ Is the target CPU has FPU (Floating Point Unit)
● Examples:
○ arm-linux-gnueabihf (ARM, GNU CLib, EABI, HF: Hardware Float)
○ arm-linux-musleabi (ARM, Musl CLib, EABI, No FPU)
● If no toolchain found for your combination, then you need to create one using:
○ crosstool-ng
○ Yocto
○ …
16
Copyright 2023 bhstalel@gmail.com
What’s next?
● Do some cross compilation and examine the generated ELF file with file command
● Examine the Assembly output differences (To master registers and low level CPU stuff)
17
Copyright 2023 bhstalel@gmail.com
Build Systems
● Build systems are frameworks that help you automate the build process.
● How can you generate 1000 .o files from .c and link them manually?
○ Running `gcc -c f1.c` to 1000?
● How can you detect when to recompile a .c file (Always or only on modification?)
● And more and more questions are answered by build systems like: make and cmake
18
Copyright 2023 bhstalel@gmail.com
● make is based on an input file, generally, called Makefile (it can support custom name)
19
Copyright 2023 bhstalel@gmail.com
What’s next?
● Learn about cmake as it is a wrapper over make and other build systems
● Document about other build systems (DO NOT BE AFRAID, THEY SERVE THE SAME PURPOSE)
○ bazel
○ ninja
○ meson
○ conan
○ vcpkg
○ …
20
Copyright 2023 bhstalel@gmail.com
POSIX Programming
● When it gets running it becomes a: Process that has a unique ID: PID
21
Copyright 2023 bhstalel@gmail.com
POSIX Programming
● Multiple threads in one Process share everything in the context except the stack
● To access files in the HW, system calls need to be invoked for the Kernel (open, …)
○ This topic is “File handling” in Linux
22
Copyright 2023 bhstalel@gmail.com
● All so called “microservices” are just processes talking to each other via IPC.
23
Copyright 2023 bhstalel@gmail.com
What’s next?
● Develop a program that runs a Child process and check their PIDs
○ Develop your custom C Shell that takes input and invokes fork, exec* and wait
24
Copyright 2023 bhstalel@gmail.com
Kernel | Kbuild
● Kbuild system is a way to compile and manage Kernel components in a way that make the
Kernel so modular and can be adapted to any need.
○ Example: No need for the Kernel to know WIFI if no WIFI used in the project
25
Copyright 2023 bhstalel@gmail.com
Kernel | Kbuild
linux
Kernel | Kbuild
Assuming .o are not in obj- meaning that they are not disabled.
.o .o .o .o .o .o .o
Kbuild | menuconfig
● menuconfig is one of the Makefile targets that compiles an ncurses application and
runs the root Kconfig file on it and thus you get a menu that handles .config
automatically.
● It makes a backup for .config named .config.old before doing any saving.
○ This helps using diffconfig utility to show the difference between the two
○ That is called: Kernel Configuration Fragment (.cfg)
○ Used to automatically apply a configuration on a preset of .config
● When working with a fresh Linux sources, you need to create a .config before working
with menuconfig
○ This is usually done via <name>_defconfig target that tells Makefile to get a
saved and ready defconfig file and copy it as .config.
○ This is usually saved in: linux/arch/<ARCH>/configs
● Example:
○ # Setup for cross compilation:
○ $ export ARCH=arm
○ $ export CROSS_COMPILE=arm-linux-gnueabihf-
○ $ make defconfig # Prepare the .config file
○ $ make menuconfig # Opens the menu utility
○ $ make modules # Compile only out-of-tree modules
○ $ make # Compile the full Kernel Image
28
Copyright 2023 bhstalel@gmail.com
Kernel | Development
● Each subsystem has its own API and they are all well developed and documented
● Most development in the Kernel is in Device Drivers section, it’s +95% of source code.
● Device Drivers development requires understanding how the core works (Memory, …)
29
Copyright 2023 bhstalel@gmail.com
What’s next?
● Talking about the Kernel internals will take writing a full book.
CPU
ROM SRAM
1st Hardware
Bootloader SPL
Components
3 2
SPL U-boot
U-boot
DTB
DTB 4
Kernel 5
6
Kernel
Rootfs
7
SD/eMMC RAM
31
Copyright 2023 bhstalel@gmail.com
● SPL: Second Program Loader: Initializes the RAM and loads TPL
● TPL: Third Program Loader (Infamous U-boot, or other): Load Kernel and DTB
● At the end, the Kernel loads the first program (init) from the rootfs
○ There are other programs before init, but it is up to you to go that deep.
● The init program starts running other programs (fork+exec) until reaching the shell
● Usually it invokes what is called an Init Manager (systemd, sysvinit, busybox-init, ..)
32
Copyright 2023 bhstalel@gmail.com
● Example:
lcd_backlight: backlight {
compatible = "pwm-backlight"; // What driver to invoke
33
Copyright 2023 bhstalel@gmail.com
What’s next?
● Create a simple SD card with dd and load it with qemu-system after Uboot and load
files from there
● Learn how to do “Network booting” to fetch Kernel and DTB from networking (TFTP, NFS)
34
Copyright 2023 bhstalel@gmail.com
● Build systems are used to build full distributions for your need
○ Far better than working with pre-built distros like Ubuntu, Raspbian, …
● Distro build systems have same idea as Software build systems like make
kernel, dtb
in-house
bootloader
Build System
rootfs
upstream
sdk
Configuration
git, http, svn, ftp, .. ARCH ?
C Library ?
Rootfs ?
…
35
Copyright 2023 bhstalel@gmail.com
What’s next?
● This topic is huge and the more you master the previous topics of general Embedded
Linux the more you understand build systems as:
○ They will fetch, prepare and build your software (Kernel, Uboot, Busybox, …)
○ Prepare the type of compilation and toolchain, …
○ Assembly the final image for you
36
Copyright 2023 bhstalel@gmail.com
Software Development
● POSIX Programming
● Programming languages:
○ Shell (MUST)
○ C (MUST)
○ C++ (90% MUST) BELIEVE IN YOURSELF
○ Python (90% MUST)
○ Rust (50% MUST) YOU CAN DO ALL
● Design Patterns: Singleton, Mediator, … :)
● Inter Process Communication
● Graphics programming: SDL, Qt, …
● Build systems: make, cmake, …
● Debugging: GDB, Binary Ninja, …
● ….
37
Copyright 2023 bhstalel@gmail.com
Going Beyond
● Mastering all previous content will make you capable of working in industry themes:
○ Automotive: Adds some protocols: CAN, SOME/IP, …
○ IoT: Based on all 3 jobs, adds MQTT protocol (TCP), …
○ Robotics: Has ROS (Robot Operating System) based on Linux with C++ and Python, ..
○ Routers: Based on openWRT which is based on Buildroot, just learn Networking
38
Copyright 2023 bhstalel@gmail.com
40