Documentation/Maemo 5 Developer Guide/Kernel and Debugging Guide/Maemo Kernel Guide

Contents

Kernel Guide

This chapter describes how to configure, compile and flash the Linux kernel for the Internet Tablet device. The chapter is targeted at developers wishing to compile their own custom kernels for the device.

Prerequisites

Before starting, set up the Maemo environment. The required GCC toolchain that is used to compile the kernel is included in the Scratchbox by default.

Setting up a separate target for kernel compilation is not mandatory, but this example does it in case the default armel target has been modified in some way.


  • Start Scratchbox.
  • $ scratchbox
    
  • Create a new target called MaemoKernel with qemu-arm CPU transparency. The second command installs the armel rootstraps to the target. The last command installs the C-library, /etc, devkits and fakeroot.
  • [sbox-FREMANTLE_ARMEL: ~] > sb-conf setup MaemoKernel \
    -c cs2007q3-glibc2.5-arm7 \
    -d qemu:perl:svn:apt-https -t /scratchbox/devkits/qemu/bin/qemu-arm-sb
    [sbox-FREMANTLE_ARMEL: ~] > sb-conf select MaemoKernel
    [sbox-MaemoKernel: ~] > sb-conf rs MaemoKernel \
    /home/<username>/maemo-sdk-rootstrap_5.0_armel.tgz
    [sbox-MaemoKernel: ~] > sb-conf in MaemoKernel -edFL
    
Image:Ambox_notice.png
The <username> above refers to your login name in the environment. If you have used the Maemo installer, the rootstraps are under your home directory. If you have performed a manual installation, the rootstrap is under the /scratchbox/packages directory. Keep this in mind when running the sb-conf command above.


  • Verify that the sources.list file inside the Scratchbox environment is correct. If the below lines are not in the /etc/apt/sources.list file, add them there.
  • deb http://repository.maemo.org/ fremantle/sdk free non-free
    deb-src http://repository.maemo.org/ fremantle/sdk free
    
    deb http://repository.maemo.org/ fremantle/tools free non-free
    deb-src http://repository.maemo.org/ fremantle/tools free
    
  • Create a working directory (inside Scratchbox) for the kernel sources.
  • [sbox-MaemoKernel: ~] > mkdir ~/maemo_kernel


The Scratchbox environment is now ready for compiling the kernel.

Getting Kernel Sources

Kernel sources are not included in the rootstrap. Therefore, you need to download them from the repository.

  • Select the kernel compilation target, if not selected already.
  • [sbox-FREMANTLE_ARMEL: ~] > sb-conf select MaemoKernel
    
  • Update the package database. This requires the earlier modifications in the sources.list file.
  • [sbox-MaemoKernel: ~] > fakeroot apt-get update
    
  • Go to the working directory and fetch the sources.
  • [sbox-MaemoKernel: ~] > cd ~/maemo_kernel
    [sbox-MaemoKernel: ~/maemo_kernel ] > apt-get source kernel
    

Kernel sources should now be fetched and ready to be compiled.

Configuring Source Tree and Compiling Kernel

  • A source subdirectory is listed. Enter the directory and create the default configuration.
  • [sbox-MaemoKernel: ~/maemo_kernel ] > cd kernel-2.6.28
    [sbox-MaemoKernel: ~/maemo_kernel/kernel-2.6.28] > make EXTRAVERSION=-omap1 rx51_defconfig
     # lots of output from make program...
    
  • Compile the kernel image and check the image file timestamp to ensure that it is properly created.
  • [sbox-MaemoKernel: ~/maemo_kernel/kernel-2.6.28] > make EXTRAVERSION=-omap1 bzImage
    # compilation output...
    [sbox-MaemoKernel: ~/maemo_kernel/kernel-2.6.28] > ls -l arch/arm/boot/zImage
    -rwxrwxr-x  1 maemo maemo 1717620 Jul 29 18:10 arch/arm/boot/zImage
    


Image:Ambox_notice.png
The kernel is very strict on versions. EXTRAVERSION=-omap1 sets the version to match the default installation. Instead of always supplying it to the make command, as done in this chapter, it is possible to modify the kernel Makefile. Another alternative is to avoid using make directly and use dpkg-buildpackage -rfakeroot -b to compile the kernel.


Now the device can be flashed with the new kernel image using the Flasher tool. This must be performed outside Scratchbox. For short instructions on flashing, see the Flashing Kernel section.

Changing Default Kernel Configuration

The following steps describe how to change the default kernel configuration.

  • Restore the original default configuration, just in case it has been changed.
  • [sbox-MaemoKernel: ~/maemo_kernel/kernel-2.6.28] > make EXTRAVERSION=-omap1 \
    rx51_defconfig
    
  • Edit the configuration file with your chosen editor. Editing the file is OK even though the system warns against it.
  • [sbox-MaemoKernel: ~/maemo_kernel/kernel-2.6.28] > vi .config
    
  • Include your changes for compilation.
  • [sbox-MaemoKernel: ~/maemo_kernel/kernel-2.6.28] > make EXTRAVERSION=-omap1 oldconfig
    


Now a kernel image with modified configuration can be recompiled.

Compiling External Kernel Modules

In this section, a simple external, or out-of-tree, kernel module is built against the kernel-headers. This is the recommended way of building new modules. A real external kernel module can be a driver for an uncommon or new device or some special service for a system, but this example code only prints some helloworlds to the kernel ring buffer.

  1. As before, start in the work directory.
    [sbox-MaemoKernel: ~/maemo_kernel] >
  2. Install the kernel-headers package.
    [sbox-MaemoKernel: ~/maemo_kernel] > fakeroot apt-get install kernel-headers
  1. Download the example files from the Maemo svn.
    [sbox-MaemoKernel: ~/maemo_kernel] > svn export https://garage.maemo.org/svn/\
    maemoexamples/trunk/hello-module/
    
    [sbox-MaemoKernel: ~/maemo_kernel] > cd hello-module
    [sbox-MaemoKernel: ~/maemo_kernel/hello-module] > ls -l
    total 32
    -rw-rw-r--  1 maemo maemo 17989 Aug 28 13:59 COPYING
    -rw-rw-r--  1 maemo maemo   380 Aug 28 13:59 Makefile
    -rw-rw-r--  1 maemo maemo   480 Aug 28 14:23 README
    -rw-rw-r--  1 maemo maemo   640 Aug 28 13:59 hello.c
    

    The source code for the module is very simple:

    [sbox-MaemoKernel: ~/maemo_kernel/hello-module] > cat hello.c
    /* hello-module.c, an example kernel module */
    
    #include <linux/module.h> /* needed by all kernel modules */
    #include <linux/init.h>   /* needed for custom init/exit functions */
    #include <linux/kernel.h> /* needed for KERN_ALERT macro */
    
    /* Special macro to indicate license (to avoid tainting the kernel) */
    MODULE_LICENSE("GPLv2");
    
    static int hello_init(void) {
      printk(KERN_ALERT "Hello, world\n"); /* top priority message */
      return 0;
    }
    
    static void hello_exit(void)
    {
    	printk(KERN_INFO "Goodbye, world\n");
    }
    
    /* macros to mark modules init/exit funcs (run on insmod/rmmod) */
    module_init(hello_init);
    module_exit(hello_exit);
    

    Compiling kernel modules differs from compiling normal programs. That is reflected in the Makefile:

    [sbox-MaemoKernel: ~/maemo_kernel/hello-module] > cat Makefile
    # Makefile for building the hello kernel module outside the kernel tree
    
    KERNELDIR := /usr/src/kernel-headers
    
    obj-m := hello.o
    
    # default build target (uses kernel build (kbuild) system)
    all:
    	$(MAKE) -C $(KERNELDIR) M=`pwd` EXTRAVERSION=-maemo2 modules
    
    # target for cleaning up
    clean:
    	$(RM) *.o .depend .*.cmd *.ko *.mod.c Module.symvers modules.order
    	$(RM) -R .tmp_versions
    

    The all: target compiles the module, not using this Makefile, but a Makefile in the kernel headers folder set in variable KERNELDIR. The clean: target is a regular target handled in this Makefile.

  2. Compile the module.
    [sbox-MaemoKernel: ~/maemo_kernel/hello-module] > make
    make -C /usr/src/kernel-headers M=`pwd` modules
    make[1]: Entering directory `/targets/MaemoKernel/usr/src/kernel-headers'
      CC [M]  /home/maemo/maemo_kernel/hello-module/hello.o
      Building modules, stage 2.
      MODPOST 1 modules
      CC      /home/maemo/maemo_kernel/hello-module/hello.mod.o
      LD [M]  /home/maemo/maemo_kernel/hello-module/hello.ko
    make[1]: Leaving directory `/targets/MaemoKernel/usr/src/kernel-headers'
    [sbox-MaemoKernel: ~/maemo_kernel/hello-module] >
    
  3. Check the results.
    [sbox-MaemoKernel: ~/maemo_kernel/hello-module] > ls -l
    total 148
    -rw-rw-r--  1 maemo maemo   223 Aug  6 14:51 Makefile
    -rw-rw-r--  1 maemo maemo     0 Aug  6 14:51 Module.symvers
    -rw-rw-r--  1 maemo maemo   692 Aug  6 14:50 hello.c
    -rw-rw-r--  1 maemo maemo 64037 Aug  6 14:51 hello.ko
    -rw-rw-r--  1 maemo maemo   663 Aug  6 14:51 hello.mod.c
    -rw-rw-r--  1 maemo maemo 32656 Aug  6 14:51 hello.mod.o
    -rw-rw-r--  1 maemo maemo 32400 Aug  6 14:51 hello.o
    -rw-rw-r--  1 maemo maemo    57 Aug  6 14:51 modules.order
    


File hello.ko is the new module. Install it by copying it to the device and running it (as root)

Nokia-NXX-31-10:/home/user# insmod hello.ko

and remove it by calling

Nokia-NXX-31-10:/home/user# rmmod hello.ko


Check that the module worked properly by examining the kernel ring buffer:

Nokia-NXX-31-10:/home/user# dmesg | tail
...
[11921.578338] Hello, world
[11928.357696] Goodbye, world

If there is a slight version mismatch between SDK's kernel headers and the running kernel on device, insmod refuses to install the module. In this case, install the module by stripping version info from it before installation:

[sbox-FREMANTLE_ARMEL: ~/maemo_kernel/hello-module] > objcopy --strip-debug \ 
-R .modinfo -R __versions hello.ko

For more information on kernel modules, see the following:

Compiling Internal Kernel Modules

This section explains how to configure and compile additional kernel modules included in the kernel source tree for the Internet Tablet in the Maemo environment. The NFSD module is used as an example. In this section, the modules are compiled against the full kernel source tree, not the kernel headers package. This example compiles all configured modules at once.


  • Go to your working directory with kernel sources.
  • [sbox-MaemoKernel:] > cd  ~/maemo_kernel/kernel-2.6.28
    
  • Edit the configuration file and define that the NFSD is a kernel module.
  • [sbox-MaemoKernel: ~/maemo_kernel/kernel-2.6.28] > vi .config
    
  • Find the location of the NFSD configuration.
  • # CONFIG_NFSD is not set
    
  • Enable NFSD support as a module (m for module, Y for built-in).
  • CONFIG_NFSD=m
    
  • As described in the previous sections, refresh the configuration and build new modules.
  • [sbox-MaemoKernel: ~/maemo_kernel/kernel-2.6.28] > make EXTRAVERSION=-maemo2 oldconfig
    # lots of output here. Answer Y to NFSD_V3, default to others.
    [sbox-MaemoKernel: ~/maemo_kernel/kernel-2.6.28] > make EXTRAVERSION=-maemo2 modules
    
  • Check with the find command to see which kernel object files (extension .ko) now exist.
  • [sbox-MaemoKernel: ~/maemo_kernel/kernel-2.6.28] > find . -name "*.ko" -ls
     655362  804 -rw-rw-r--   1 maemo maemo   817889 Aug  4 16:57 ./fs/ext2/ext2.ko
     630786  108 -rw-rw-r--   1 maemo maemo   106187 Aug  4 16:57 ./fs/exportfs/exp
    ortfs.ko
    1335340  144 -rw-rw-r--   1 maemo maemo   141168 Aug  4 16:57 ./fs/fat/vfat.ko
    ...
    4595743 1328 -rw-rw-r--   1 maemo maemo  1354124 Aug  4 16:57 ./fs/nfsd/nfsd.ko
    ...
    4074612  104 -rw-rw-r--   1 maemo maemo    98930 Aug  4 16:57 ./arch/arm/mach-o
    map2/dspbridge.ko
    

The list of .ko files is quite long, depending on whether or not some additional changes were made to the .config file. Module nfsd.ko is the one just added.

The kernel modules are kept under /lib/modules/. However, the insmod command can be used to load the modules into the running kernel from any directory they have been saved in.

Using make menuconfig

'make menuconfig' fails to autodetect the ncurses library available in scratchbox and bombs out with an error. If you want to use 'make menuconfig', first edit scripts/kconfig/lxdialog/check-lxdialog.sh and change

# What library to link
ldflags()
{
	for ext in so a dylib ; do

to

# What library to link
ldflags()
{
	# XXX autodetection fails in scratchbox
	echo -lncurses
	exit
	for ext in so a dylib ; do

Flashing Kernel

You can flash the custom kernel from outside the Scratchbox with the flasher tool using USB.

# flasher-3.5 -f -k /scratchbox/users/<username>/home/<username>/\
maemo_kernel/kernel-2.6.28/arch/arm/boot/zImage