Android Kernel module support. Running a simple hello-world kernel module in Android emulator

I was playing with android code and suddenly I thought lets have a small exercise of learning android kernel compilation and loading modules in it.
The following are the steps which I did in order to recompile android kernel for my emulator, write a simple hello world module, compile it, push it to emulator and load it.

Read for some fun? Read on.

==============================
Download and compile GingerBread code
==============================

Go to http://source.android.com/source/download.html and download the source code.
I downloaded the gingerbread branch
repo init -u git://android.git.kernel.org/platform/manifest.git -b gingerbread
repo sync
|
|
|
and the usual commands to get your platform code…

Note: These days the android kernel source is removed from platform code.
So I need to download the kernel source too.
I went to http://android.git.kernel.org/?p=kernel/qemu.git;a=shortlog;h=refs/heads/android-goldfish-2.6.29 and downloaded the kernel for emulator.

Now, I compiled the platform code. (source /build/envsetup.sh && lunch 1 && make -j2)
Once the platform is built, I started building the linux kernel.

===========================================
Compile Android kernel with module support
===========================================

Android kernel, by default do not support loadable modules. So I needed to edit some files.
Here is what I did:

1. make ARCH=arm goldfish_defconfig (This will create our .config)
2. Edit the .config file and searched for the line “CONFIG_MODULES”
It was unset for me and the line looked liked this
# CONFIG_MODULES is not set
I changed the line to CONFIG_MODULES=y, saved and closed the file.
3. Now, I fired the command to build the kernel
make ARCH=arm CROSS_COMPILE=~/timepass/mydroid/prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/arm-eabi-

Important: Replace “~/timepass/mydroid” with your KERNEL SOURCE TREE
Note: This command might ask for the following
======================================

scripts/kconfig/conf -s arch/arm/Kconfig
*
* Restart config...
*
*
* General setup
*
Prompt for development and/or incomplete code/drivers (EXPERIMENTAL) [Y/n/?] y
Local version - append to kernel release (LOCALVERSION) []
Automatically append version information to the version string (LOCALVERSION_AUTO) [Y/n/?] y
Support for paging of anonymous memory (swap) (SWAP) [Y/n/?] y
System V IPC (SYSVIPC) [Y/n/?] y
POSIX Message Queues (POSIX_MQUEUE) [N/y/?] n
BSD Process Accounting (BSD_PROCESS_ACCT) [N/y/?] n
Export task/process statistics through netlink (EXPERIMENTAL) (TASKSTATS) [N/y/?] n
Auditing support (AUDIT) [N/y/?] n
Kernel .config support (IKCONFIG) [Y/n/m/?] y
Enable access to .config through /proc/config.gz (IKCONFIG_PROC) [Y/n/?] y
Kernel log buffer size (16 => 64KB, 17 => 128KB) (LOG_BUF_SHIFT) [16] 16
Group CPU scheduler (GROUP_SCHED) [Y/n/?] y
Group scheduling for SCHED_OTHER (FAIR_GROUP_SCHED) [Y/n] y
Group scheduling for SCHED_RR/FIFO (RT_GROUP_SCHED) [Y/n/?] y
Basis for grouping tasks
1. user id (USER_SCHED)
> 2. Control groups (CGROUP_SCHED)
choice[1-2]: 2
Create deprecated sysfs layout for older userspace tools (SYSFS_DEPRECATED_V2) [N/y/?] n
Kernel->user space relay support (formerly relayfs) (RELAY) [N/y/?] n
Namespaces support (NAMESPACES) [Y/?] y
UTS namespace (UTS_NS) [N/y/?] n
IPC namespace (IPC_NS) [N/y/?] n
User namespace (EXPERIMENTAL) (USER_NS) [N/y/?] n
PID Namespaces (EXPERIMENTAL) (PID_NS) [N/y/?] n
Network namespace (NET_NS) [N/y/?] n
Initial RAM filesystem and RAM disk (initramfs/initrd) support (BLK_DEV_INITRD) [Y/n/?] y
Initramfs source file(s) (INITRAMFS_SOURCE) []
Optimize for size (CC_OPTIMIZE_FOR_SIZE) [Y/n/?] y
Default panic timeout (PANIC_TIMEOUT) [0] 0
Enable the Anonymous Shared Memory Subsystem (ASHMEM) [Y/n/?] y
Disable heap randomization (COMPAT_BRK) [Y/n/?] y
Choose SLAB allocator
> 1. SLAB (SLAB)
2. SLUB (Unqueued Allocator) (SLUB)
choice[1-2?]: 1
Profiling support (EXPERIMENTAL) (PROFILING) [N/y/?] n
Kprobes (KPROBES) [N/y/?] (NEW) ---------- I said N here
*
* Enable loadable module support
*
Enable loadable module support (MODULES) [Y/n/?] y
Forced module loading (MODULE_FORCE_LOAD) [N/y/?] (NEW) y
Forced module unloading (MODULE_FORCE_UNLOAD) [N/y/?] (NEW) y
Module versioning support (MODVERSIONS) [N/y/?] (NEW) y
Source checksum for all modules (MODULE_SRCVERSION_ALL) [N/y/?] (NEW) y
*
* Memory Technology Device (MTD) support
*
Memory Technology Device (MTD) support (MTD) [Y/n/m/?] y
Debugging (MTD_DEBUG) [N/y/?] n
MTD concatenating support (MTD_CONCAT) [N/m/y/?] n
MTD partitioning support (MTD_PARTITIONS) [N/y/?] n
MTD tests support (MTD_TESTS) [N/m/?] (NEW) n
*
* User Modules And Translation Layers
*
Direct char device access to MTD devices (MTD_CHAR) [Y/n/m/?] y
Common interface to block layer for MTD 'translation layers' (MTD_BLKDEVS) [Y] y
Caching block device access to MTD devices (MTD_BLOCK) [Y/n/m/?] y
FTL (Flash Translation Layer) support (FTL) [N/m/y/?] n
NFTL (NAND Flash Translation Layer) support (NFTL) [N/m/y/?] n
INFTL (Inverse NAND Flash Translation Layer) support (INFTL) [N/m/y/?] n
Resident Flash Disk (Flash Translation Layer) support (RFD_FTL) [N/m/y/?] n
NAND SSFDC (SmartMedia) read only translation layer (SSFDC) [N/m/y/?] n
Log panic/oops to an MTD buffer (MTD_OOPS) [N/m/y/?] n
*
* Meilhaus support
*
Meilhaus support (MEILHAUS) [N/m/?] (NEW) n
*
* Staging drivers
*
Staging drivers (STAGING) [Y/n/?] y
Exclude Staging drivers from being built (STAGING_EXCLUDE_BUILD) [N/y/?] n
Line Echo Canceller support (ECHO) [N/m/y/?] n
Data Acquision support (comedi) (COMEDI) [N/m/?] (NEW) n
*
* Cryptographic API
*
Cryptographic API (CRYPTO) [Y/?] y
*
* Crypto core or helper
*
FIPS 200 compliance (CRYPTO_FIPS) [N/y/?] n
Cryptographic algorithm manager (CRYPTO_MANAGER) [Y/?] y
GF(2^128) multiplication functions (EXPERIMENTAL) (CRYPTO_GF128MUL) [N/m/y/?] n
Null algorithms (CRYPTO_NULL) [N/m/y/?] n
Software async crypto daemon (CRYPTO_CRYPTD) [N/m/y/?] n
Authenc support (CRYPTO_AUTHENC) [Y/?] y
Testing module (CRYPTO_TEST) [N/m/?] (NEW) n
*
* Authenticated Encryption with Associated Data
*
CCM support (CRYPTO_CCM) [N/m/y/?] n
GCM/GMAC support (CRYPTO_GCM) [N/m/y/?] n
Sequence Number IV Generator (CRYPTO_SEQIV) [N/m/y/?] n

================================================
Once I answered, the compilation continued and now I had a zImage of kernel with loadble module support.

===========================================
Write and compile a simple hello-world module
===========================================

At this point I have kernel with module support and compiled platform. So without much ado I fired up the emulator.


export ANDROID_PRODUCT_OUT=~/mydroid/out/
$ ./emulator -system ~/mydroid/out/target/product/generic/system.img -kernel ~/goldfish_kernel/arch/arm/boot/zImage -data ~/mydroid/out/target/product/generic/userdata.img -ramdisk ~/mydroid/out/target/product/generic/ramdisk.img -skindir ~/mydroid/sdk/emulator/skins/ -skin HVGA -verbose -show-kernel

Once emulator is up, I wrote the simple “Hello-world” module: Source is as follows:


1 #include"linux/module.h"
2 #include"linux/kernel.h"
3//replace the "" with angular brackets
4 int init_module(void)
5 {
6 printk(KERN_INFO "Hello android kernel...\n");
7 return 0;
8 }
9
10 void cleanup_module(void)
11 {
12 printk(KERN_INFO "Goodbye android kernel...\n");
13 }
14

I saved the file as android_module.c

Kernel modules as we know have to be compiled slightly differently than traditional programs.
My makefile for this simple module is as follows:


1 obj-m += android_module.o
2
3 all:
4 make -C /home/user/android_goldfish_kernel/goldfish-2.6.29/ M=$(PWD) modules
5
6 clean:
7
8 make -C /home/user/android_goldfish_kernel/goldfish-2.6.29/ M=$(PWD) clean

Now we are ready to compile our android kernel module


CROSS_COMPILE=/home/user/mydroid/prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/arm-eabi- ARCH=arm make

Once compilation is done, I got android_module.ko

===========================================
Push the module inside emulator and execute it
===========================================

I pushed the newly build module inside emulator by:


adb push android_module.ko /data/local

Now that my module is inside the emulator which was already running kernel with module support, I opened a shell by:

adb shell

Inside this emulator shell prompt, I navigated to /data/local and executed
insmod android_module.ko
rmmod android_module
dmesg

The result is

android_module: module license 'unspecified' taints kernel.
Hello android kernel...
Goodbye android kernel...

If you have any doubt, please feel free to drop a comment here ๐Ÿ™‚

Advertisements

41 Responses to “Android Kernel module support. Running a simple hello-world kernel module in Android emulator”

  1. Hi, great tutorial but I have some questions:
    1. I can’t find no way to get the android kernel – both the git and the link for download are down.
    2. Where do you save your module.c and custom makefile?
    3. From which directory do you execute the CROSS_COMPILE make?
    4. Can I compile a module if I have the arm toolchain, but don’t have the kernel?

  2. Hello Stoyan,

    1. Today it seems both android.git.kernel.org and main linux kernel repository (kernel.org) is down. Please wait a while they will be up soon.

    2. You can write your own module.c and Makefile in any directory you prefer. There are no restrictions.

    3. You have to execute the command “make CROSS_COMPILE….”
    from the top level source directory of your kernel tree.

    4. You will at-least need the “sanitized kernel headers” to compile the module. And they should match against the version you want to deploy and test your module.
    Check by running:
    cd /usr/src/linux-headers-`uname -r`

  3. 10x for the quick reply. I hope they will bring the kernel back, but a couple of days ago there was a message in the git in the spirit of “you’re hacking us to much, we’re taking it down”.
    I’ve downloaded the android-2.3.4_r1 ROM sources and managed to cook my own ROM, that even works on my Nexus S. In ~/android-2.3.4_r1/external/kernel-headers/original/linux I’ve a lot of headers. Are they the โ€œsanitized kernel headersโ€? Can you please give me a hint or even better some steps how to compile a module against them? ๐Ÿ™‚

  4. hamsterksu Says:

    how to create patch with own module?

  5. Hamsterksu: I will post a tutorial about it soon, subscribe and you will receive updates as I post.

  6. Kanishka Says:

    Hi, Nice post. I have tried this on emulator have you tried this for a real device? I want a module I have written to be pushed into a Galaxy S. Will pulling the current configuration from the phone and enabling the MODULE support to y will work?

    • Hello Kanishka,

      To do this in Galaxy S you will need to “root” the phone, i.e. get root-access and then only you will be able to insert a kernel module. Yes, you can pull current config from the phone.

  7. Ramiz Raja Says:

    Great tutorial. I read it and successfully inserted module in emulator. Now i want to insert module on my Nexus S. I have already rooted it but when i insert module it automatically reboots after 2-3 seconds without showing anything. Can you help ?

    • Thank you ๐Ÿ™‚
      Check the dmesg, make a little background service that captures dmesg every 0.5 seconds and appends to a file. When the phone reboots, check the contents of that file. From there we can understand what went wrong, if you can modify kernel command-line parameters, run with loglevel=7

  8. Ramiz Raja Says:

    OK. One thing i want to tell you, i have not flashed my custom kernel in Nexus S instead i have just rooted it and trying to insert my module. Secondly I have download Samsung kernel used for Nexus S from source.android.com and compiled it with Android 2.2 and then compiled my LKM with it but my device has Android 4.1.2 . This can be the reason ? should i need to compile Samsung-kernel with Android 4.1.2 ?

  9. Undeniably believe that which you stated. Your favorite reason seemed to be on the
    net the simplest thing to be aware of. I say to you, I certainly
    get annoyed while people think about worries that they plainly don’t know about. You managed to hit the nail upon the top and also defined out the whole thing without having side effect , people could take a signal. Will probably be back to get more. Thanks

  10. sathyamoorthy Says:

    Hii v followed ua steps 4 developing android kernel module support bt v r nt able to get .ko file so vl u plz tell us how to get it

    • Are you getting any error message, if yes, please paste it here.

      • Sathyamoorthy Says:

        Now I am able to get .ko file. But when i execute the insmod command, then i got this error : “insmod: init_module ‘/data/local/android_module.ko’ failed (Exec format error)”

      • Sathya, can you post the dmesg logs as well? I presume you’re carrying out the experiment on Goldfish (emulator) kernel.

      • sathyamoorthy Says:

        When execute the ‘dmesg’, I got this message:
        Initializing cgroup subsys cpu
        Linux version 2.6.290.10I_Love_My_Karnataka-ge3d684d-dirty (root@localhost.localdomain) (gcc version 4.4.3 (GCC) ) #1 Wed Jul 3 09:48:40 IST 2013
        CPU: ARMv7 Processor [410fc080] revision 0 (ARMv7), cr=10c5387f
        CPU: VIPT nonaliasing data cache, VIPT nonaliasing instruction cache
        Machine: Goldfish
        Memory policy: ECC disabled, Data cache writeback
        On node 0 totalpages: 131072
        free_area_init_node: node 0, pgdat c03690c4, node_mem_map c082d000
        Normal zone: 1024 pages used for memmap
        Normal zone: 0 pages reserved
        Normal zone: 130048 pages, LIFO batch:31
        Built 1 zonelists in Zone order, mobility grouping on. Total pages: 130048
        Kernel command line: qemu.gles=0 qemu=1 console=ttyS0 android.qemud=ttyS1 android.checkjni=1 ndns=4
        Unknown boot option `qemu.gles=0′: ignoring
        Unknown boot option `android.qemud=ttyS1′: ignoring
        Unknown boot option `android.checkjni=1′: ignoring
        PID hash table entries: 2048 (order: 11, 8192 bytes)
        Console: colour dummy device 80×30
        Dentry cache hash table entries: 65536 (order: 6, 262144 bytes)
        Inode-cache hash table entries: 32768 (order: 5, 131072 bytes)
        Memory: 512MB = 512MB total
        Memory: 515072KB available (3284K code, 771K data, 128K init)
        Calibrating delay loop… 576.71 BogoMIPS (lpj=2883584)
        Mount-cache hash table entries: 512
        Initializing cgroup subsys debug
        Initializing cgroup subsys cpuacct
        Initializing cgroup subsys freezer
        CPU: Testing write buffer coherency: ok
        net_namespace: 936 bytes
        NET: Registered protocol family 16
        bio: create slab at 0
        NET: Registered protocol family 2
        IP route cache hash table entries: 16384 (order: 4, 65536 bytes)
        Switched to high resolution mode on CPU 0
        TCP established hash table entries: 65536 (order: 7, 524288 bytes)
        TCP bind hash table entries: 65536 (order: 6, 262144 bytes)
        TCP: Hash tables configured (established 65536 bind 65536)
        TCP reno registered
        NET: Registered protocol family 1
        checking if image is initramfs… it is
        Freeing initrd memory: 176K
        goldfish_new_pdev goldfish_interrupt_controller at ff000000 irq -1
        goldfish_new_pdev goldfish_device_bus at ff001000 irq 1
        goldfish_new_pdev goldfish_timer at ff003000 irq 3
        goldfish_new_pdev goldfish_rtc at ff010000 irq 10
        goldfish_new_pdev goldfish_tty at ff002000 irq 4
        goldfish_new_pdev goldfish_tty at ff011000 irq 11
        goldfish_new_pdev goldfish_tty at ff012000 irq 12
        goldfish_new_pdev smc91x at ff013000 irq 13
        goldfish_new_pdev goldfish_fb at ff014000 irq 14
        goldfish_new_pdev goldfish_audio at ff004000 irq 15
        goldfish_new_pdev goldfish_memlog at ff006000 irq -1
        goldfish_new_pdev goldfish-battery at ff015000 irq 16
        goldfish_new_pdev goldfish_events at ff016000 irq 17
        goldfish_new_pdev goldfish_nand at ff017000 irq -1
        goldfish_new_pdev qemu_pipe at ff018000 irq 18
        goldfish_new_pdev goldfish-switch at ff01a000 irq 19
        goldfish_new_pdev goldfish-switch at ff01b000 irq 20
        goldfish_pdev_worker registered goldfish_interrupt_controller
        goldfish_pdev_worker registered goldfish_device_bus
        goldfish_pdev_worker registered goldfish_timer
        goldfish_pdev_worker registered goldfish_rtc
        goldfish_pdev_worker registered goldfish_tty
        goldfish_pdev_worker registered goldfish_tty
        goldfish_pdev_worker registered goldfish_tty
        goldfish_pdev_worker registered smc91x
        goldfish_pdev_worker registered goldfish_fb
        goldfish_pdev_worker registered goldfish_audio
        goldfish_pdev_worker registered goldfish_memlog
        goldfish_pdev_worker registered goldfish-battery
        goldfish_pdev_worker registered goldfish_events
        goldfish_pdev_worker registered goldfish_nand
        goldfish_pdev_worker registered qemu_pipe
        goldfish_pdev_worker registered goldfish-switch
        goldfish_pdev_worker registered goldfish-switch
        ashmem: initialized
        Installing knfsd (copyright (C) 1996 okir@monad.swb.de).
        fuse init (API version 7.11)
        yaffs Jul 3 2013 09:46:48 Installing.
        msgmni has been set to 1006
        alg: No test for stdrng (krng)
        io scheduler noop registered
        io scheduler anticipatory registered (default)
        io scheduler deadline registered
        io scheduler cfq registered
        allocating frame buffer 480 * 800, got ffa00000
        console [ttyS0] enabled
        brd: module loaded
        loop: module loaded
        nbd: registered device at major 43
        goldfish_audio_probe
        tun: Universal TUN/TAP device driver, 1.6
        tun: (C) 1999-2004 Max Krasnyansky
        smc91x.c: v1.1, sep 22 2004 by Nicolas Pitre
        eth0 (smc91x): not using net_device_ops yet
        eth0: SMC91C11xFD (rev 1) at e080c000 IRQ 13 [nowait]
        eth0: Ethernet addr: 52:54:00:12:34:56
        eth0: No PHY found
        goldfish nand dev0: size c5e0000, page 2048, extra 64, erase 131072
        goldfish nand dev1: size c200000, page 2048, extra 64, erase 131072
        goldfish nand dev2: size 4000000, page 2048, extra 64, erase 131072
        mice: PS/2 mouse device common for all mice
        *** events probe ***
        events_probe() addr=0xe0814000 irq=17
        events_probe() keymap=qwerty2
        input: qwerty2 as /devices/virtual/input/input0
        goldfish_rtc goldfish_rtc: rtc core: registered goldfish_rtc as rtc0
        device-mapper: uevent: version 1.0.3
        device-mapper: ioctl: 4.14.0-ioctl (2008-04-23) initialised: dm-devel@redhat.com
        logger: created 64K log ‘log_main’
        logger: created 256K log ‘log_events’
        logger: created 64K log ‘log_radio’
        Netfilter messages via NETLINK v0.30.
        nf_conntrack version 0.5.0 (8192 buckets, 32768 max)
        CONFIG_NF_CT_ACCT is deprecated and will be removed soon. Please use
        nf_conntrack.acct=1 kernel paramater, acct=1 nf_conntrack module option or
        sysctl net.netfilter.nf_conntrack_acct=1 to enable it.
        ctnetlink v0.93: registering with nfnetlink.
        NF_TPROXY: Transparent proxy support initialized, version 4.1.0
        NF_TPROXY: Copyright (c) 2006-2007 BalaBit IT Ltd.
        xt_time: kernel timezone is -0000
        ip_tables: (C) 2000-2006 Netfilter Core Team
        arp_tables: (C) 2002 David S. Miller
        TCP cubic registered
        NET: Registered protocol family 10
        ip6_tables: (C) 2000-2006 Netfilter Core Team
        IPv6 over IPv4 tunneling driver
        NET: Registered protocol family 17
        NET: Registered protocol family 15
        RPC: Registered udp transport module.
        RPC: Registered tcp transport module.
        802.1Q VLAN Support v1.8 Ben Greear
        All bugs added by David S. Miller
        VFP support v0.3: implementor 41 architecture 3 part 30 variant c rev 0
        goldfish_rtc goldfish_rtc: setting system clock to 2013-07-03 04:19:13 UTC (1372825153)
        Freeing init memory: 128K
        init: cannot open ‘/initlogo.rle’
        yaffs: dev is 32505856 name is “mtdblock0”
        yaffs: passed flags “”
        yaffs: Attempting MTD mount on 31.0, “mtdblock0”
        yaffs_read_super: isCheckpointed 0
        save exit: isCheckpointed 1
        yaffs: dev is 32505857 name is “mtdblock1”
        yaffs: passed flags “”
        yaffs: Attempting MTD mount on 31.1, “mtdblock1”
        yaffs_read_super: isCheckpointed 0
        yaffs: dev is 32505858 name is “mtdblock2”
        yaffs: passed flags “”
        yaffs: Attempting MTD mount on 31.2, “mtdblock2”
        yaffs_read_super: isCheckpointed 0
        init: cannot find ‘/system/etc/install-recovery.sh’, disabling ‘flash_recovery’
        eth0: link up
        warning: `rild’ uses 32-bit capabilities (legacy support in use)
        eth0: no IPv6 routers present
        request_suspend_state: wakeup (3->0) at 44215238700 (2013-07-03 04:19:57.180215505 UTC)
        init: sys_prop: permission denied uid:1003 name:service.bootanim.exit
        android_module: unknown relocation: 27

        So what was the mistake here?

      • Sathya,

        You have pasted the dmesg of your custom kernel bootup (as evident from I_Love_My_Karnataka string from linux_banner). ๐Ÿ™‚
        But what I need you to do is:
        1. Bootup your emulator
        2. Try to insmod your module
        3. dmesg > a_file
        4. Paste the file contents here.

      • sathyamoorthy Says:

        I did samething what you told, but i didn’t get what is ‘a_file’? and which the file contents have to paste here.

  11. KRISHNA Says:

    I need to write a module which will call wifi driver on my preference ….. Please help me with it

    • Hello Krishna,
      What exactly you’re trying to achieve here? Please explain in details.

      • KRISHNA Says:

        I am working on Android. I want to modify wifi driver. So from where to start. I have downloaded source code for android kernel and Wireless directory contains hell lot of files. Now which one to modify.It even doesn’t contain documentation so that I can understand the code. please help me with it. I don’t know from where to start.

      • Firstly, do not say that documentation is not available. It _is_ present. Check out the documentation folder, you will get loads of it.
        Now, coming down to your problem, you need to _idenitify_ your wifi card. Then you need to understand on which bus it is connected, could be a sdio or a i2c, the depends.
        Then you need to have the datasheet of your card, based on which you’ll write your driver.

        If you have never coded for Linux kernel before, I suggest you go through the Linux Device Drivers book written by Greg, Jonathan and Rubini.
        Here is a link: http://lwn.net/Kernel/LDD3/
        After that try writing a simple module and understand how various kernel subsystems work. Then try writing your own driver.

        Personally, I will suggest you to go through kernel mailing list and participate in the discussion on current bugs, understand and try to fix the problem, post a patch and see what happens next. If you’re flamed, don’t be sad, try and understand the reason why they didn’t like your patch and work on it. In case of a doubt, let me know.

  12. KRISHNA Says:

    I tried to flash custom kernel on android. It showed Install complete from sdcard but when I reboot it the kernel version which I changed in Makefile is not displayed in kernel version.It shows the previous version.

  13. Pavan @(pavanh)

    Hi very nice tutorial , very detailed and explanatory and helpful for beginners

  14. Nice tutorial. I am looking at getting into module development to do some interesting stuff that I think a lot of people will like… Your tutorial has given me the push I need to know where to go for info and what I need to do. It is Android 4.3 tweaks that I will be playing with so I will need a different version than you got and I may have a bit of fun trying to find the source to the kernel I want to play with, but if things go well, I’ll have some nice tweaks… hopefully…
    Just a question: what linux things do you all need installed for this to work? I see you need GCC and git (obviously), but not sure what other linux tools you need?

  15. I’ve compiled a basic hello world Android kernel module

    #include /* Needed by all modules */
    #include /* Needed for KERN_INFO */
    #include /* Needed for the macros */

    static int hello3_data __initdata = 3;

    static int __init hello_3_init(void)
    {
    printk(KERN_INFO “Hello, world %d\n”, hello3_data);
    return 0;
    }

    static void __exit hello_3_exit(void)
    {
    printk(KERN_INFO “Goodbye, world 3\n”);
    }

    module_init(hello_3_init);
    module_exit(hello_3_exit);

    and written the following in the Makefile :

    VERSION = 2
    PATCHLEVEL = 6
    SUBLEVEL = 29
    EXTRAVERSION =
    obj-m += hello_3.o
    KDIR=/home/aniket/beproject/htconev/android/goldfish
    PWD := $(shell pwd)
    all:
    make -C $(KDIR) ARCH=arm CROSS_COMPILE=/home/aniket/beproject/android/android-ndk-r9c/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86_64/bin/arm-linux-androideabi- SUBDIRS=$(PWD) modules

    clean:
    make -C $(KDIR) ARCH=arm CROSS_COMPILE=/home/aniket/beproject/android/android-ndk-r9c/toolchains/arm-linux-androideabi-4.6/prebuilt/linux-x86_64/bin/arm-linux-androideabi- SUBDIRS=$(PWD) clean

    However after running make and pushing it to the Android device abd running insmod it gives me an error :

    insmod: init_module _.ko failed (Exec format error)

    I got rid of that error using

    make CFLAGS_MODULE=-fno-pic

    However after installing module using insmod, dmesg says:

    hello_3: module license ‘unspecified’ taints kernel.
    hello_3: unknown relocation: 27
    hello_3: no version for “magic” found: kernel tainted.

    The module has been loaded and is live after checking /proc/modules

    Can you please tell me where I’ve gone wrong ?

    • Hello Alex,

      You will need to add the following in your module code.
      MODULE_LICENSE(“GPL”);

      Without this, kernel assumes your code is not GPL’ed and any non-GPL code will run with a “xxx taints kernel” warning message.

      PS: Sorry for my late reply!

  16. Ruhi Sharma Says:

    hey very nice………. ๐Ÿ™‚

  17. Ruhi Sharma Says:

    Hey I am facing a problem while building the kernel …… please help

    1. I have downloaded and complied the source tree first(Say code source tree is in WORKING_DIRECTORY in my HOME folder).
    2. Now I have downloaded the Kernal source too and it is in goldfish folder inside the WORKING_DIRECTORY
    3. I am facing a problem while building the kernal

    make ARCH=arm CROSS_COMPILE=~/WORKING_DIRECTORY/prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/arm-eabi-

    Above command is giving me the following error :
    make: /home/ruhi/WORKING_DIRECTORY/prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/arm-eabi-gcc: Command not found
    CHK include/linux/version.h
    make[1]: `include/asm-arm/mach-types.h’ is up to date.
    CHK include/linux/utsrelease.h
    SYMLINK include/asm -> include/asm-arm
    CC kernel/bounds.s
    /bin/sh: 1: /home/ruhi/WORKING_DIRECTORY/prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/arm-eabi-gcc: not found
    make[1]: *** [kernel/bounds.s] Error 127
    make: *** [prepare0] Error 2

    even I have also tried command(though I know it doesn’t make sense)
    make ARCH=arm CROSS_COMPILE=~/WORKING_DIRECTORY/goldfish/prebuilt/linux-x86/toolchain/arm-eabi-4.4.3/bin/arm-eabi-
    This is also giving me the same error

    Please help…..
    Thanks in Advance ๐Ÿ™‚

  18. Ruhi Sharma Says:

    I have a .apk file and I want to log all the API calls made by this application while running in the Emulator. I want to log the API calls into a file while this application is running in the emulator. Note that I can’t access the code of the application. Is writing some kernel module in emulator kernel(goldfish) is of any use? I have complied the goldfish kernel and wrote a simple “Hello World” module in it.

    If anybody knows the way to start it please help me.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: