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

(get hello-module from garage svn)
Line 3: Line 3:
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.
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 =
+
== Prerequisites ==
Before starting, the maemo environment should be set up. The required GCC toolchain that is used to compile the kernel is included in the Scratchbox by default.
Before starting, the maemo environment should be set up. The required GCC toolchain that is used to compile the kernel is included in the Scratchbox by default.
Line 9: Line 9:
It is not mandatory to set up a separate target for kernel compilation, but this example does it in case the default armel target has been modified in some special way.
It is not mandatory to set up a separate target for kernel compilation, but this example does it in case the default armel target has been modified in some special way.
-
* Start Scratchbox. <br /><br />
+
* Start Scratchbox.
   
   
  $ 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. <br /><br />
+
* 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: ~] &gt; sb-conf setup MaemoKernel \
+
  [sbox-FREMANTLE_ARMEL: ~] > sb-conf setup MaemoKernel \
  -c cs2007q3-glibc2.5-arm7 \
  -c cs2007q3-glibc2.5-arm7 \
  -d qemu:perl:svn:apt-https -t /scratchbox/devkits/qemu/bin/qemu-arm-sb
  -d qemu:perl:svn:apt-https -t /scratchbox/devkits/qemu/bin/qemu-arm-sb
-
  [sbox-FREMANTLE_ARMEL: ~] &gt; sb-conf select MaemoKernel
+
  [sbox-FREMANTLE_ARMEL: ~] > sb-conf select MaemoKernel
-
  [sbox-MaemoKernel: ~] &gt; sb-conf rs MaemoKernel \
+
  [sbox-MaemoKernel: ~] > sb-conf rs MaemoKernel \
  /home/&lt;username&gt;/maemo-sdk-rootstrap_5.0beta_armel.tgz
  /home/&lt;username&gt;/maemo-sdk-rootstrap_5.0beta_armel.tgz
-
  [sbox-MaemoKernel: ~] &gt; sb-conf in MaemoKernel -edFL
+
  [sbox-MaemoKernel: ~] > sb-conf in MaemoKernel -edFL
-
<br /><br /><div style="border: 2px solid rgb(255, 215, 0); background-color: rgb(252, 233, 79); margin-left: 25px; margin-right: 25px; padding: 2px"> N.B. <br /><br /> The &lt;username&gt; 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 /scratchbox/packages directory. This should be paid attention to, when running the sb-conf command above. </div>
+
 
-
* 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. <br /><br />
+
 
 +
<div style="border: 2px solid rgb(255, 215, 0); background-color: rgb(252, 233, 79); margin-left: 25px; margin-right: 25px; padding: 2px"> N.B. <br /><br /> The &lt;username&gt; 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 /scratchbox/packages directory. This should be paid attention to, when running the sb-conf command above. </div>
 +
 
 +
 
 +
* 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 http://repository.maemo.org/ fremantle/sdk free non-free
Line 31: Line 35:
  deb-src http://repository.maemo.org/ fremantle/tools free
  deb-src http://repository.maemo.org/ fremantle/tools free
-
* Create a working directory (inside Scratchbox) for the kernel sources. <br /><br />
+
* Create a working directory (inside Scratchbox) for the kernel sources.
   
   
  [sbox-MaemoKernel: ~] &gt; mkdir ~/maemo_kernel
  [sbox-MaemoKernel: ~] &gt; mkdir ~/maemo_kernel
Line 38: Line 42:
The Scratchbox environment is now ready for compiling the kernel.
The Scratchbox environment is now ready for compiling the kernel.
-
= Getting Kernel Sources =
+
== Getting Kernel Sources ==
Kernel sources are not included in the rootstrap, and therefore need to be downloaded from the repository.
Kernel sources are not included in the rootstrap, and therefore need to be downloaded from the repository.
-
* Select the kernel compilation target, if not selected already. <br /><br />
+
* Select the kernel compilation target, if not selected already.
   
   
-
  [sbox-FREMANTLE_ARMEL: ~] &gt; sb-conf select MaemoKernel
+
  [sbox-FREMANTLE_ARMEL: ~] > sb-conf select MaemoKernel
-
* Update the package database. This requires the earlier modifications in the sources.list file. <br /><br />
+
* Update the package database. This requires the earlier modifications in the sources.list file.
   
   
-
  [sbox-MaemoKernel: ~] &gt; fakeroot apt-get update
+
  [sbox-MaemoKernel: ~] > fakeroot apt-get update
-
* Go to the working directory and fetch the sources. <br /><br />
+
* Go to the working directory and fetch the sources.
   
   
-
  [sbox-MaemoKernel: ~] &gt; cd ~/maemo_kernel
+
  [sbox-MaemoKernel: ~] > cd ~/maemo_kernel
-
  [sbox-MaemoKernel: ~/maemo_kernel ] &gt; apt-get source kernel
+
  [sbox-MaemoKernel: ~/maemo_kernel ] > apt-get source kernel
Kernel sources should now be fetched and ready to be compiled.
Kernel sources should now be fetched and ready to be compiled.
-
= Configuring Source Tree and Compiling Kernel =
+
== Configuring Source Tree and Compiling Kernel ==
-
* There is a source subdirectory. Enter the directory and create the default configuration. <br /><br />
+
* There is a source subdirectory. Enter the directory and create the default configuration.
-
<nowiki>
+
 
-
  [sbox-MaemoKernel: ~/maemo_kernel ] &gt; cd kernel-2.6.28
+
 
-
  [sbox-MaemoKernel: ~/maemo_kernel/kernel-2.6.28] &gt; make EXTRAVERSION=-maemo2 rx51_defconfig
+
  [sbox-MaemoKernel: ~/maemo_kernel ] > cd kernel-2.6.28
 +
  [sbox-MaemoKernel: ~/maemo_kernel/kernel-2.6.28] > make EXTRAVERSION=-maemo2 rx51_defconfig
  # lots of output from make program...
  # lots of output from make program...
-
</nowiki>
 
-
* Compile the kernel image, and check the image file timestamp to ensure that it is properly created. <br /><br />
+
 
-
<nowiki>
+
* 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] &gt; make EXTRAVERSION=-maemo2 bzImage
  [sbox-MaemoKernel: ~/maemo_kernel/kernel-2.6.28] &gt; make EXTRAVERSION=-maemo2 bzImage
  # compilation output...
  # compilation output...
  [sbox-MaemoKernel: ~/maemo_kernel/kernel-2.6.28] &gt; ls -l arch/arm/boot/zImage
  [sbox-MaemoKernel: ~/maemo_kernel/kernel-2.6.28] &gt; ls -l arch/arm/boot/zImage
  -rwxrwxr-x  1 maemo maemo 1717620 Jul 29 18:10 arch/arm/boot/zImage
  -rwxrwxr-x  1 maemo maemo 1717620 Jul 29 18:10 arch/arm/boot/zImage
-
</nowiki>
 
-
<br /><br /><div style="border: 2px solid rgb(255, 215, 0); background-color: rgb(252, 233, 79); margin-left: 25px; margin-right: 25px; padding: 2px"> N.B. <br /><br /> The kernel is very strict on versions. <code>EXTRAVERSION=-maemo2</code> sets the version to match the default installation. Instead of always supplying it to the <code>make</code> command, as done in this chapter, it would be possible to modify the kernel <code>Makefile</code>. Another alternative would be to avoid using <code>make</code> directly and use <code>dpkg-buildpackage -rfakeroot -b</code> to compile the kernel. </div>
+
<div style="border: 2px solid rgb(255, 215, 0); background-color: rgb(252, 233, 79); margin-left: 25px; margin-right: 25px; padding: 2px"> N.B. <br /><br /> The kernel is very strict on versions. <code>EXTRAVERSION=-maemo2</code> sets the version to match the default installation. Instead of always supplying it to the <code>make</code> command, as done in this chapter, it would be possible to modify the kernel <code>Makefile</code>. Another alternative would be to avoid using <code>make</code> directly and use <code>dpkg-buildpackage -rfakeroot -b</code> to compile the kernel. </div>
-
Now the device can be flashed with the new kernel image using the Flasher tool. This should be performed outside Scratchbox. See section [[#Flashing Kernel]] for short flashing instructions.
 
-
= Changing Default Kernel Configuration =
+
Now the device can be flashed with the new kernel image using the Flasher tool. This should be performed outside Scratchbox. See section [[Documentation/Maemo 5 Developer Guide/Development Environment/Maemo Flasher-3.5|Flashing Kernel]] for short flashing instructions.
 +
 
 +
== Changing Default Kernel Configuration ==
The following steps describe how to change the 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. <br /><br />
+
* Restore the original default configuration, just in case it has been changed.  
   
   
-
  [sbox-MaemoKernel: ~/maemo_kernel/kernel-2.6.28] &gt; make EXTRAVERSION=-maemo2 \
+
  [sbox-MaemoKernel: ~/maemo_kernel/kernel-2.6.28] > make EXTRAVERSION=-maemo2 \
  rx51_defconfig
  rx51_defconfig
-
* Edit the configuration file with your editor of choice. It is all right to edit the file, even though there is a warning against changing it included. <br /><br />
+
* Edit the configuration file with your editor of choice. It is all right to edit the file, even though there is a warning against changing it included.  
   
   
-
  [sbox-MaemoKernel: ~/maemo_kernel/kernel-2.6.28] &gt; vi .config
+
  [sbox-MaemoKernel: ~/maemo_kernel/kernel-2.6.28] > vi .config
-
* Include your changes for compilation. <br /><br />
+
* Include your changes for compilation.
   
   
-
  [sbox-MaemoKernel: ~/maemo_kernel/kernel-2.6.28] &gt; make EXTRAVERSION=-maemo2 oldconfig
+
  [sbox-MaemoKernel: ~/maemo_kernel/kernel-2.6.28] > make EXTRAVERSION=-maemo2 oldconfig
Now a kernel image with modified configuration can be recompiled, as described in the previous section.
Now a kernel image with modified configuration can be recompiled, as described in the previous section.
-
= Compiling External Kernel Modules =
+
== 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 could 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.
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 could 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.
-
# As before, start in the work directory. <pre>[sbox-MaemoKernel: ~/maemo_kernel] &gt;</pre>
+
# As before, start in the work directory. <pre>[sbox-MaemoKernel: ~/maemo_kernel] ></pre>
# Install the kernel-headers package. <pre>[sbox-MaemoKernel: ~/maemo_kernel] &gt; fakeroot apt-get install kernel-headers</pre>
# Install the kernel-headers package. <pre>[sbox-MaemoKernel: ~/maemo_kernel] &gt; fakeroot apt-get install kernel-headers</pre>
Line 199: Line 204:
</pre></li>
</pre></li>
</ol>
</ol>
-
<br /><br /> File <code>hello.ko</code> is the new module. It can be installed by copying it to the device and running (as root) <br />
+
 
 +
 
 +
File <code>hello.ko</code> is the new module. It can be installed by copying it to the device and running (as root)
  Nokia-NXX-31-10:/home/user# insmod hello.ko
  Nokia-NXX-31-10:/home/user# insmod hello.ko
and removed by calling  
and removed by calling  
  Nokia-NXX-31-10:/home/user# rmmod hello.ko
  Nokia-NXX-31-10:/home/user# rmmod hello.ko
-
<br />You can check that the module worked properly by examining the kernel ring buffer:
+
 
 +
You can check that the module worked properly by examining the kernel ring buffer:
   
   
  Nokia-NXX-31-10:/home/user# dmesg | tail
  Nokia-NXX-31-10:/home/user# dmesg | tail
Line 211: Line 219:
  [11928.357696] Goodbye, world
  [11928.357696] Goodbye, world
-
<br />If there is a slight version mismatch between SDK's kernel headers and the running kernel on device, <code>insmod</code> refuses to install the module. In this case, you can install the module by stripping version info from it before installation:
+
If there is a slight version mismatch between SDK's kernel headers and the running kernel on device, <code>insmod</code> refuses to install the module. In this case, you can install the module by stripping version info from it before installation:
   
   
-
  [sbox-FREMANTLE_ARMEL: ~/maemo_kernel/hello-module] &gt; objcopy --strip-debug \
+
  [sbox-FREMANTLE_ARMEL: ~/maemo_kernel/hello-module] &gt; objcopy --strip-debug \  
  -R .modinfo -R __versions hello.ko
  -R .modinfo -R __versions hello.ko
-
<br /><br /> For additional reading on kernel modules, see, e.g.:
+
For additional reading on kernel modules, see, e.g.:
* [http://www.tldp.org/LDP/lkmpg/2.6/html/index.html The Linux Kernel Module Programming Guide]
* [http://www.tldp.org/LDP/lkmpg/2.6/html/index.html The Linux Kernel Module Programming Guide]
* <nowiki>/scratchbox/users/&lt;username&gt;/home/&lt;username&gt;/maemo_kernel/kernel-2.6.28/Documentation/kbuild/modules.txt</nowiki>
* <nowiki>/scratchbox/users/&lt;username&gt;/home/&lt;username&gt;/maemo_kernel/kernel-2.6.28/Documentation/kbuild/modules.txt</nowiki>
-
= Compiling Internal Kernel Modules =
+
== 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. 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.
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. 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. <br /><br />
+
* Go to your working directory with kernel sources.
   
   
  [sbox-MaemoKernel:]&gt; cd  ~/maemo_kernel/kernel-2.6.28
  [sbox-MaemoKernel:]&gt; cd  ~/maemo_kernel/kernel-2.6.28
-
* Edit the configuration file and define that the NFSD will be a kernel module. <br /><br />
+
* Edit the configuration file and define that the NFSD will be a kernel module.  
   
   
  [sbox-MaemoKernel: ~/maemo_kernel/kernel-2.6.28] &gt; vi .config
  [sbox-MaemoKernel: ~/maemo_kernel/kernel-2.6.28] &gt; vi .config
-
<br /><br /> Find the location of the NFSD configuration. <br /><br />
+
 
-
<nowiki>
+
* Find the location of the NFSD configuration.
 +
 
  # CONFIG_NFSD is not set
  # CONFIG_NFSD is not set
-
</nowiki>
+
 
-
<br /><br /> Enable NFSD support as a module (m for module, Y for built-in). <br /><br />
+
* Enable NFSD support as a module (m for module, Y for built-in).  
   
   
  CONFIG_NFSD=m
  CONFIG_NFSD=m
-
* As described in previous sections, refresh the configuration and build new modules. <br /><br />
+
* As described in previous sections, refresh the configuration and build new modules.
-
<nowiki>
+
 
 +
 
  [sbox-MaemoKernel: ~/maemo_kernel/kernel-2.6.28] &gt; make EXTRAVERSION=-maemo2 oldconfig
  [sbox-MaemoKernel: ~/maemo_kernel/kernel-2.6.28] &gt; make EXTRAVERSION=-maemo2 oldconfig
  # lots of output here. Answer Y to NFSD_V3, default to others.
  # lots of output here. Answer Y to NFSD_V3, default to others.
  [sbox-MaemoKernel: ~/maemo_kernel/kernel-2.6.28] &gt; make EXTRAVERSION=-maemo2 modules
  [sbox-MaemoKernel: ~/maemo_kernel/kernel-2.6.28] &gt; make EXTRAVERSION=-maemo2 modules
-
</nowiki>
 
-
* Check with the find command to see what kernel object files (extension .ko) there now are. <br /><br />
+
 
 +
* Check with the find command to see what kernel object files (extension .ko) there now are.
   
   
  [sbox-MaemoKernel: ~/maemo_kernel/kernel-2.6.28] &gt; find . -name "*.ko" -ls
  [sbox-MaemoKernel: ~/maemo_kernel/kernel-2.6.28] &gt; find . -name "*.ko" -ls
Line 259: Line 269:
  4074612  104 -rw-rw-r--  1 maemo maemo    98930 Aug  4 16:57 ./arch/arm/mach-o
  4074612  104 -rw-rw-r--  1 maemo maemo    98930 Aug  4 16:57 ./arch/arm/mach-o
  map2/dspbridge.ko
  map2/dspbridge.ko
-
<br /><br /> The list of .ko files is will be quite long, depending on whether some additional changes were made to the .config file. Module nfsd.ko is the one just added.
 
-
The kernel modules are kept under <code>/lib/modules/</code>. However, the <code>insmod</code> command can be used to load the modules into the running kernel from any directory where they have been saved in.
 
-
= Flashing Kernel =
+
The list of .ko files is will be quite long, depending on whether some additional changes were made to the .config file. Module nfsd.ko is the one just added.
 +
 
 +
The kernel modules are kept under <code>/lib/modules/</code>. However, the <code>insmod</code> command can be used to load the modules into the running kernel from any directory where they have been saved in.
-
The custom kernel can be flashed from outside the Scratchbox with the flasher tool via USB. <br />
+
== Flashing Kernel ==
-
<br />
+
The custom kernel can be flashed from outside the Scratchbox with the flasher tool via USB.
-
<nowiki>
 
  # flasher -f -k /scratchbox/users/&lt;username&gt;/home/&lt;username&gt;/\
  # flasher -f -k /scratchbox/users/&lt;username&gt;/home/&lt;username&gt;/\
  maemo_kernel/kernel-2.6.28/arch/arm/boot/zImage
  maemo_kernel/kernel-2.6.28/arch/arm/boot/zImage
-
</nowiki>
 

Revision as of 11:25, 29 August 2009

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, the maemo environment should be set up. The required GCC toolchain that is used to compile the kernel is included in the Scratchbox by default.

It is not mandatory to set up a separate target for kernel compilation, but this example does it in case the default armel target has been modified in some special 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.0beta_armel.tgz
[sbox-MaemoKernel: ~] > sb-conf in MaemoKernel -edFL


N.B.

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 /scratchbox/packages directory. This should be paid attention to, 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, and therefore need to be downloaded 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

  • There is a source subdirectory. 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=-maemo2 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=-maemo2 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


N.B.

The kernel is very strict on versions. EXTRAVERSION=-maemo2 sets the version to match the default installation. Instead of always supplying it to the make command, as done in this chapter, it would be possible to modify the kernel Makefile. Another alternative would be 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 should be performed outside Scratchbox. See section Flashing Kernel for short flashing instructions.

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=-maemo2 \
rx51_defconfig
  • Edit the configuration file with your editor of choice. It is all right to edit the file, even though there is a warning against changing it included.
[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=-maemo2 oldconfig


Now a kernel image with modified configuration can be recompiled, as described in the previous section.

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 could 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 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. It can be installed by copying it to the device and running (as root)

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

and removed by calling

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


You can 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, you can 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 additional reading on kernel modules, see, e.g.:

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. 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 will be 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 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 what kernel object files (extension .ko) there now are.
[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 will be quite long, depending on whether 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 where they have been saved in.

Flashing Kernel

The custom kernel can be flashed from outside the Scratchbox with the flasher tool via USB.

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