在内核中添加自己的代码:

/**************************************************************************************/
内核源码的查看方法:
    1.删除linux-3.5-for-look/arch/arm/所无关于三星的代码。因为这个内核支持的芯片比较多,我们学得是arm,查询的时候防止查询到其他平台的代码,所以精简一下代码。
    2.ctags -R .
    3.vim -t memcpy
    4.vim 命令行模式下:cstag memcpy
    5.ctrl + ] :进入
    6.ctrl + o :退出

/**************************************************************************************/
在内核中添加自己的代码:
    1.驱动可以直接编译到内核。
    2.或者编译成模块,然后需要的时候在安装。
    3.mkdir linux-3.5/Driver/mydriver
    4.创建一下几个文件:my.c Makefile
    my.c中代码:
          1 #include <linux/init.h>
          2 #include <linux/module.h>
          3 #include <linux/sched.h>
          4 //在内核启动过程中执行,且在内核启动过程中执行的函数类型是固定的:int test_init(void) :返回值和行>>
            参是固定的。
          5 //init 段:这个函数被放到init段,好处是代码执行完成之后,函数体就释放了,节省一部分内存。只能调用>>
            一次。后面不要调用了。
          6 static __init  int test_init(void)
          7 {
          8         int i;
          9         //如果资源申请失败,要饭会相应的错误吗
         10         //      
         11         //return -ENOMEM;内存申请失败
         12         for(i=0;i<20;i++)
         13         {
         14                 printk("hello kernel\n");
         15         }
         16                 //printf("") //>> 1
         17         return 0;
         18
         19 }
         20 //把test_init函数放到内核启动过程中执行
         21 module_init(test_init);
         22 MODULE_LICENSE("GPL"); //这个是代码遵循GPL开源规则,以后谁要是用了我们写的代码,也必须要遵循GPL规则。
         23 //下面三行可有可无
         24 MODULE_AUTHOR("LIUYE");
         25 MODULE_VERSION("V1.0");    
            26 MODULE_DESCRIPTION("test for class");

    Makefile:
        obj-y += my.o
    vim ../Makefile 上一层makefile中添加下层Makefile的信息,makefile是分层编译的结构,上层malefile记录下层的makefile信息,才能正确编译。
        obj-y += mydriver/     这里一定要有“/”,因为这里是一个目录
    之后就可退出到linux-3.5/ make -j4
        [liuye@LiuYe linux-3.5]$>make -j4
          CHK     include/linux/version.h
          CHK     include/generated/utsrelease.h
        make[1]: 'include/generated/mach-types.h' is up to date.
          CALL    scripts/checksyscalls.sh
          CHK     include/generated/compile.h
          CC      drivers/mydriver/my.o
          LD      drivers/mydriver/built-in.o
          LD      drivers/built-in.o
          LINK    vmlinux
          LD      vmlinux.o
          MODPOST vmlinux.o
        WARNING: modpost: Found 9 section mismatch(es).
        To see full details build your kernel with:
        'make CONFIG_DEBUG_SECTION_MISMATCH=y'
          GEN     .version
          CHK     include/generated/compile.h
          UPD     include/generated/compile.h
          CC      init/version.o
          LD      init/built-in.o
          KSYM    .tmp_kallsyms1.o
          KSYM    .tmp_kallsyms2.o
          LD      vmlinux
          SYSMAP  System.map
          OBJCOPY arch/arm/boot/Image
          Kernel: arch/arm/boot/Image is ready
          GZIP    arch/arm/boot/compressed/piggy.gzip
          AS      arch/arm/boot/compressed/piggy.gzip.o
          LD      arch/arm/boot/compressed/vmlinux
          OBJCOPY arch/arm/boot/zImage
          Kernel: arch/arm/boot/zImage is ready
          Building modules, stage 2.
          MODPOST 3 modules
    可以看到我们的代码被编译并且链接了。之后更换zImage到SD卡,刷机,只需要刷zImage就可以了。
        [    3.225000] usbhid: USB HID core driver
        [    3.225000] hello kernel
        [    3.225000] hello kernel
        [    3.225000] hello kernel
        [    3.225000] hello kernel
        [    3.225000] hello kernel
        [    3.225000] hello kernel
        [    3.225000] hello kernel
        [    3.225000] hello kernel
        [    3.225000] hello kernel
        [    3.225000] hello kernel
        [    3.225000] hello kernel
        [    3.225000] hello kernel
        [    3.225000] hello kernel
        [    3.225000] hello kernel
        [    3.225000] hello kernel
        [    3.225000] hello kernel
        [    3.225000] hello kernel
        [    3.225000] hello kernel
        [    3.225000] hello kernel
        [    3.225000] hello kernel
        [    3.225000] Samsung Audio Subsystem Driver, (c) 2011 Samsung Electronics
        [    3.225000] audss_init: RCLK SRC[busclk]
        [    3.225000] Samsung SRP driver, (c)2011 Samsung Electronics
        [    3.310000] mmcblk0boot1: mmc0:0001 M4G1YC partition 2 2.00 MiB
    可以看到我们的代码在内核启动过程中就已经运行了。
/**************************************************************************************/

猜你喜欢

转载自www.cnblogs.com/axjlxy/p/8934160.html