Cross Compile Hello world kernel module - PowerPC

寫了幾年的程式,常常在改人家寫好的kernel module,好像沒自己寫過,今天就來試試看。

1.先寫好hello.c 這段整個是reference copy來的。
#include <linux/module.h>       /* Needed by all modules */
#include <linux/kernel.h>      /* Needed for KERN_INFO */

int init_module(void)
{
        printk(KERN_INFO "Hello world 1.\n");

        /* 
         * A non 0 return means init_module failed; module can't be loaded. 
         */
        return 0;
}

void cleanup_module(void)
{
        printk(KERN_INFO "Goodbye world 1.\n");
}
2. Makefile kernel module的compile方式跟 userspace application 有點不同。 我實驗用的開發板是PowerPC平台,其make file如下。
# Cross compilation Makefile for ARM
KERN_SRC=../../linux-2.6.37_build
obj-m := hello.o
 
all:
        make -C $(KERN_SRC) ARCH=powerpc CROSS_COMPILE=/opt/powerpc/usr/bin/powerpc-linux- M=`pwd` modules
clean:
        make -C $(KERN_SRC) ARCH=powerpc CROSS_COMPILE=/opt/powerpc/usr/bin/powerpc-linux- M=`pwd` clean
3.把hello.ko tftp至機器上測試
[root@localhost /sbin]# insmod hello.ko
Hello world 1.
[root@localhost /sbin]# lsmod
hello 592 0 - Live 0xcc89d000 (P)
[root@localhost /sbin]# rmmod hello
Goodbye world 1.
參考資料:
The Linux Kernel Module Programming Guide
http://linux.die.net/lkmpg/index.html

留言

這個網誌中的熱門文章

Raspberry Pi (ARMv6)上自幹一個微小作業系統

C語言,大數運算,階層筆記

Linux VLAN 筆記