Professional Documents
Culture Documents
Embedded Linux Kernel and Driver Development Training Lab Book
Embedded Linux Kernel and Driver Development Training Lab Book
Free Electrons
http://free-electrons.com
Training setup
The training labs are done with the vanilla version of Kubuntu
(http://www.kubuntu.org), a derivative of Ubuntu that uses the KDE
desktop environment. Most of the needed packages are readily
available from Kubuntu official repositories, but Free Electrons
provides an additional repository for other packages.
General guidelines
Useful during all the labs:
You should only use the root user for operations that require
super-user privileges, such as: mounting a file system, loading a
kernel module, changing file ownership, configuring the network.
Most regular tasks (such as downloading, extracting sources,
compiling...) can be done as a regular user.
If you ran commands from a root shell by mistake, your regular
user may no longer be able to handle the corresponding
generated files. In this case, use the chown R command to give
back the new files to your regular user.
Example: chown R myuser:myuser /mnt/labs/
In Debian, Ubuntu, Kubuntu and other derivatives, don't be
surprised if you cannot run graphical applications as root. You
could set the DISPLAY variable to the same setting as for your
regular user, but again, it's unnecessary and unsafe to run
graphical applications as root.
In Kubuntu running in LiveCD mode, do not store data or
generate files in /home/ubuntu. Otherwise, your system will
quickly freeze. /home is in RAM. The bigger /home gets, the less
RAM is left for your applications.
Installation Kubuntu
If you are allowed to install GNU/Linux on your PC, you may choose
to install Kubuntu on your hard disk.
Real life experience from training life has shown that:
cdrom access is slow, making the system very slow to respond to
4
© 2004-2008 Free Electrons, http://free-electrons.com Creative Commons License
Embedded Linux kernel
and driver development
Training lab book
Before
Used Free
Windows Windows
system partition data partition
After
6
© 2004-2008 Free Electrons, http://free-electrons.com Creative Commons License
Embedded Linux kernel
and driver development
Training lab book
You will have to do these steps (adding the persistent boot option
and mounting the labs.img file) at each boot. That's why we
recommend the Kubuntu installation option, which is more
convenient.
More guidelines
Can be useful throughout any of the labs
Read instructions and tips carefully. Lots of people make mistakes
or waste time because they missed an explanation or a guideline.
Always read error messages carefully, in particular the first one
which is issued. Some people stumble on very simple errors just
because they specified a wrong file path and didn't pay enough
attention to the corresponding error message.
If your workstation freezes, tell your instructor. In many cases,
the workstation is still alive and can be fixed without rebooting.
Never stay stuck with a strange problem more than 5 minutes.
Show your problem to your colleagues or to the instructor.
Setup
Go to the /mnt/labs/linux/lab1 directory.
To practice the patch command later, download the full 2.6.22 wget <url>
sources. Unpack the archive, which creates a linux2.6.22 wget <url>.sign
directory. wget can continue interrupted
downloads
Apply patches
Install the patch command, either through the graphical Adept
package manager, or using the following command line :
sudo aptitude install patch
Download the 2 patch files corresponding to the latest 2.6.23 stable
release. Check their authenticity too.
Without uncompressing them (!), apply the 2 patches to the
linux2.6.22 directory.
Did you know it? gvim can open
View one of the 2 patch files with vi or gvim (if you prefer a graphic compressed files on the fly!
editor), to understand the information carried by such a file. How are Vim supports supports syntax
described added or removed files? highlighting for patch files
8
© 2004-2008 Free Electrons, http://free-electrons.com Creative Commons License
Embedded Linux kernel
and driver development
Training lab book
function.
Setup
Stay in the /mnt/labs/linux/lab1 directory from the previous lab.
Objectives
The goal of this lab is to configure, build and boot a kernel for a
minimalistic, virtual PC, emulated by the qemu emulator
(http://qemu.org)
Makefile tweaks
Add the usage of ccache during compiling, to speed up recompiling,
in case you make multiple configuration changes. You will need to
install the ccache package.
Kernel configuration
Run make xconfig to start the kernel configuration interface. The
kernel configuration interface is provided as source code in the
kernel, make xconfig compiles it automatically, but requires libraries
and headers. You will need to install the libqt3mtdev package,
which contains the Qt development files, and the g++ package, the C
++ compiler.
In the interface, toggle the Option > Show Name option. This is Also try with make menuconfig.
useful sometimes, when the parameter name is more explicit than its Though it is not graphical, some people
description, or when you're are following guidelines which give the prefer this interface. As the
menuconfig interface is based on the
parameter name itself. Ncurses library, you will have to install
Also try the Option > Show All Options and Option > Show the libncursesdev package to use it.
Debug Info options. They let you see all the parameters which
wouldn't appear otherwise, because they depend on the values of
other parameters. By clicking on the description of such a
parameter, you will see its preconditions and understand why it is
not selectable.
Specify a version suffix, so that you can identify your kernel in the
running system by running uname r
or cat /proc/version.
Configure your kernel for a minimalistic PC:
We advise you to unselect all options at
Pentium-Pro processor (i686) once, and add only the ones that you
need.
IDE hard disk (not only ATA / IDE support!)
ext2 filesystem
Support for elf binaries
10
© 2004-2008 Free Electrons, http://free-electrons.com Creative Commons License
Embedded Linux kernel
and driver development
Training lab book
qemu is a fast tool which can emulate several processors (x86, ppc,
arm, sparc, mips...) or even entire systems.
By using an emulator, you won't have to reboot your workstation
over and over again to test your new kernel.
To install qemu on your system, simply install the qemu package.
mount o loop data/linux_i386.img fs/
Still logged as root, create the dev/console device that is missing. Whatever the architecture Linux runs
You can check the /dev/console device file on your training on, major and minor device numbers
are always the same.
workstation to find out the file type as well as the major and minor
device numbers. If you don't umount a filesystem or do
not use special mount options, you
Once this is done, unmount the root filesystem: can't be sure that your changes have
umount fs already been committed on the
physical media (the .img file in this
Rerun your qemu command. Now, you should reach a command line case).
shell.
To unmount a filesystem, remember
Run a few commands in the virtual PC shell. that you must be outside of the
directory where the filesystem is
mounted. Otherwise umount will fail
Kernel version with Device or resource busy.
Query the version of the running kernel and make sure you find the
version suffix that you specified at configuration time.
Conclusion
Well done! Now you know how to configure, compile and boot a
kernel on a minimalistic PC.
12
© 2004-2008 Free Electrons, http://free-electrons.com Creative Commons License
Embedded Linux kernel
and driver development
Training lab book
Lab 3 – Cross-compiling
Objective: Learn how to cross-compile a kernel for another
target platform.
Setup
Go to the /mnt/labs/linux/lab3 directory.
Target system
We are going to cross-compile and boot a Linux kernel for the ARM q3mu rul3z!
Versatile PB development board, emulated by qemu.
Makefile setup
Modify the toplevel Makefile file to cross-compile for the arm
platform using the above toolchain.
Cross compiling
Try to compile your kernel. You will soon see whether there are
issues in your setup or not.
If the run was successful, where was the compressed Linux kernel
image created?
14
© 2004-2008 Free Electrons, http://free-electrons.com Creative Commons License
Embedded Linux kernel
and driver development
Training lab book
After this lab, you will be able to NFS root filesystems are particularly
useful to compile modules on your
Boot an i386 kernel using an NFS root filesystem, which is host, and make them directly visible on
somewhere on your development workstation. the target. You longer have to update
the root filesystem by hand and
Write a kernel module with several capabilities, including module transfer it to the target (requiring a
parameters and a /proc output interface. shutdown and reboot).
Lab implementation
You know that code from Linux kernel modules is treated just as any
kernel code, and executed without any control. In particular, any
mistake in module code can corrupt or crash the running kernel.
This is particularly true with modules under development. If you
used your PC workstation to test your new modules, you could face
several critical failures, and waste a significant amount of time
rebooting your system and restoring your development environment
(all the more if you use the live CD which forgets all your settings).
To make your first practice with Linux kernel modules easier, we are
going to use a virtual Linux system again. When your test system
crashes, you will just have to restart the emulator.
There is a practical problem though: how to update the compiled
module files (.ko) on the virtual system, every time they are
recompiled? Copying them each time from the development PC to
the target root filesystem would be tedious: halting the virtual
system, mounting the target root filesystem image, updating the
module file, unmounting and eventually restarting the virtual system.
Fortunately, it is possible to set up networking between the
development workstation and the virtual target. Then, workstation
files can be accessed through the network by the target, using NFS.
Development PC
/work/modules directory
test.ko
Virtual PC (qemu)
NFS
e xpo /mnt/nfs directory
rt
test.ko
Virtual
network
Setup
Go to the /mnt/labs/linux/lab4 directory. The way you do it is up to you. You can
reuse sources from the previous labs,
Get the sources from the latest 2.6.20.x release and place them in download fresh sources again or
directly use ketchup. Just make sure
the current directory.
the architecture is set to i386.
Kernel configuration
Configure this kernel with the configuration file available in the
data/ subdirectory.
Now add the configuration options that enable booting on a root Other TCP/IP networking configuration
filesystem which sits on an NFS remote directory. settings are already enabled in the
provided configuration file.
Compile your kernel.
Host-target networking
Of course, we will need networking between the host (workstation)
and the target (qemu emulated PC).
To do this, we will add the net tap net nic options to the qemu
command line.
When qemu is run with the above options, it creates a userspace
(software) IP tunnel between the virtual machine and the host. On
the host, this corresponds to the tun0 network interface. qemu
automatically sets its IP address to 172.20.0.1.
In the virtual machine, this corresponds to a regular Ethernet
interface (eth0 if only one interface is created). We will assign the
172.20.0.2 address to it.
NFS booting
Install the NFS server by installing the nfs-kernel-server package.
Once installed, edit the /etc/exports file as root to add the
following lines :
/mnt/labs/linux/lab4/nfsroot 172.20.0.2(rw,no_root_squash,no_subtree_check)
/mnt/labs/linux/lab5/nfsroot 172.20.0.2(rw,no_root_squash,no_subtree_check)
/mnt/labs/linux/lab6/nfsroot 172.20.0.2(rw,no_root_squash,no_subtree_check)
Writing a module
Go to the nfsroot/src directory. All the files you generate there will
also be visible from the target. That's great to load modules!
Create a hello_version.c file implementing a module which
displays this kind of message when loaded:
16
© 2004-2008 Free Electrons, http://free-electrons.com Creative Commons License
Embedded Linux kernel
and driver development
Training lab book
18
© 2004-2008 Free Electrons, http://free-electrons.com Creative Commons License
Embedded Linux kernel
and driver development
Training lab book
Setup
Go to the /mnt/labs/linux/lab5 directory.
Reuse the run_qemu script from lab4, but adjust paths to continue to
As in the previous lab, look at the lab
use the kernel you built in lab4. directory for files that you can reuse.
Now go to the nfsroot/src directory.
Just like in the previous lab, you have a ready-made Makefile to
compile your module, and a template echo.c module which you will
fill with your own code.
20
© 2004-2008 Free Electrons, http://free-electrons.com Creative Commons License
Embedded Linux kernel
and driver development
Training lab book
Lab 6 - Interrupts
Objective: learn how to register and implement a simple
interrupt handler.
Setup
Go to the /mnt/labs/linux/lab6 directory.
As in lab4, we will use a virtual PC booted from NFS.
Reuse the run_qemu script from lab5!
Go to the nfsroot/src directory.
/proc interface
Implement and register a /proc/intr_monitor interface for your Don't hesitate to reuse the /proc
module. At the beginning, just make it display a global variable that interface code you wrote in Lab 4!
will count the number of interrupts intercepted by your driver.
Note that each time an interrupt is
received, the handler could just printk
Interrupt handler the updated counter value. However, a
/proc interface is a much better
Implement an interrupt handler that just increases the interrupt solution, as the number of interrupts
counter variable. can be huge. Using printk would just
keep the kernel log too busy.
Make sure it returns a correct value though.
Handler registration
In your module init function, register your handler on a shared
interrupt line, which is defined by a irq module parameter.
Make sure that you test that the handler registration was successful!
Otherwise, just make the module init function return a negative
value (suggestion: EBUSY), causing module loading to fail.
Going further
You could improve your driver by:
Making it compute the number of interrupts per second.
22
© 2004-2008 Free Electrons, http://free-electrons.com Creative Commons License