Sunday 6 July 2014

[Kernel Programming] Cross compile a module for ARM platform

My initial Kernel Programming posts in this blog contains the following info

  • how to write a module,
  • how to compile a module using a Makefile,
  • how to insert/remove a module into linux platform machine
Please check my previous posts for this information below

Now, I will share the required data for writing a simple module for ARM platform in this post. Please follow below steps for the same. 

[Note: You should have working linux kernel source code, and able to flash this code on any android device for testing this module.]

Step-1: Goto Linux Kernel source directory and set $TOP variable with this path
Example: If kernel source code is present on home directory then
export TOP = $(/home/your-machine-username/kernel_source_dir)

Step-2: Goto kernel folder from $TOP and create a temporary directory there.
Example: 1. cd $TOP/kernel
                2. mkdir test

Step-3: Now, add simple module file in the test directory.
Example: simple.c

#include<linux/module.h> /* Needed by all modules *MUST* */

int init_module(void) {
         printk(KERN_ALERT "1st module program: Module Inserted\n");
         return 0;
 }

 void cleanup_module() {
         printk(KERN_ALERT "1st module program: Module Removed\n");
 }

Step-4: Add Makefile for this module.
Example: Makefile
obj-m += simple.o

Only above one line is enough here.

Step-5: Now, we have two files in the test directory. They are simple.c and Makefile. Goto kernel folder (cd $TOP/kernel)
Step-6: Compile our test module using make command

make -j4 ARCH=arm CROSS_COMPILE=$TOP/prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.7/bin/arm-linux-androideabi- O=$TOP/out/target/product/<product-name>/obj/KERNEL M=$TOP/kernel/test

Options description:

-j4: Describes how many threads GNU compiler will make use of, to compile the module. Normally, the number(just after 'j') should be twice the number of CPU cores available for your pc.

ARCH: Describes the destination platform architecture. The name of the architecture can be found in the $TOP/kernel/arch/ directory.
Example: arm platform: $TOP/kernel/arch/arm
                arm64 platform: $TOP/kernel/arch/arm64

CROSS_COMPILE: Specifies the correct tools path to be used for generating object files and binary files for our test module.

O: Specifies the path to where the generated object files have to go.

M: Path to input module directory to compile the required module.

Step-7: After successful compilation of our test module, we can see simple.ko file get generated in $TOP/kernel/test directory.

Step-8: Now, insert this module into destination platform using insmod command.
If the destination platform is accessed through adb shell then do the following.
  1. sudo adb push $TOP/kernel/test/simple.ko /data
  2. sudo adb shell
  3. su -
  4. cd /data
  5. Insert our test module (simple.ko) using below command
    insmod simple.ko
  6. We can see the output of the insmod using below command
    dmesg
    And observe, "1st module program: Module Inserted" got printed in the log
  7. Use below command for removing our module
    rmmod simple
I hope this data is useful to compile any module for different platform.

References:
https://www.kernel.org/doc/Documentation/kbuild/kbuild.txt


You might also like

Related Posts Plugin for WordPress, Blogger...