基于 i.mx6 开发板编译加载驱动文件过程总结

本文详细介绍了一名Linux初学者如何从零开始编译并加载一个名为hello的驱动模块。文章涵盖了hello驱动的源代码、Makefile配置、编译过程、交叉编译设置以及在开发板上加载和卸载驱动的具体步骤。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

作为一名 Linux 初学者,很多东西都是从无到有、从零到一的过程,现将自己学习过程中遇到的问题及解决方法记录下来,既方便给遇到同样问题的你一些启发,也方便再次遇到同样问题时能回过头来看看曾经走过的路。
如果这其中有什么不明白或者不妥的地方,也欢迎咨询及指正,感谢!

接下来就开始对编译加载驱动文件进行总结,首先我是从生成最简单的驱动文件 hello.ko 开始,hello.ko 的生成是基于 hello.c 及 Makefile 这两个文件编译出来的,具体代码如下:

hello.c

#include <linux/init.h>
#include <linux/module.h>

MODULE_LICENSE("GPL");

static int hello_init(void)
{
	printk(KERN_ALERT "Hello, world\n");
	return 0;
}

static void hello_exit(void)
{
	printk(KERN_ALERT "Goodbye, cruel world\n");
}

module_init(hello_init);
module_exit(hello_exit);

Makefile

obj-m := hello.o
        KERNELBUILD := /opt/yocto/fsl-release-bsp/build-mini/tmp/work/imx6sxsabresd-poky-linux-gnueabi/linux-imx/4.1.15-r0/git
defaule:
        make -C $(KERNELBUILD) M=$(shell pwd) modules ARCH=arm CROSS_COMPILE=arm-fsl-linux-gnueabi-
clean:
        rm -rf *.o *.ko *.mod.c .*.cmd *.markers *.order *.symvers

注:生成的 hello.ko 要能够在开发板上成功加载,需要满足几个条件:
① 既然要在开发板上运行,就需要在编译的时候指明交叉编译所用的编译器,我这里用的是 arm-fsl-linux-gnueabi-gcc
② 然后还需要指明编译时所对应 kernel 的路径,这里所指向的 kernel 需要跟开发板的 kernel 一致,即需要将开发板运行的 kernel 放到虚拟机里面,且需要编译通过后再指向。我这里由于 kernel 不一致导致编译出来的 ko 文件在 insmod 时一直报错 insmod: ERROR: could not insert module hello.ko: Invalid module format,最终是通过重新编译 kernel 再烧录到板子上解决的。

完成 hello.c 及 Makefile 的编写后,在虚拟机中执行 make 开始编译

ipual@ipual-virtual-machine:/opt/ipual/hello_world/ko$ make
make -C /opt/yocto/fsl-release-bsp/build-mini/tmp/work/imx6sxsabresd-poky-linux-gnueabi/linux-imx/4.1.15-r0/git M=/opt/ipual/hello_world/ko modules ARCH=arm CROSS_COMPILE=arm-fsl-linux-gnueabi-
make[1]: 正在进入目录 `/opt/yocto/fsl-release-bsp/build-mini/tmp/work-shared/imx6sxsabresd/kernel-source'
  CC [M]  /opt/ipual/hello_world/ko/hello.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /opt/ipual/hello_world/ko/hello.mod.o
  LD [M]  /opt/ipual/hello_world/ko/hello.ko
make[1]:正在离开目录 `/opt/yocto/fsl-release-bsp/build-mini/tmp/work-shared/imx6sxsabresd/kernel-source'

输入 ls 可以看到编译后所生成的文件

ipual@ipual-virtual-machine:/opt/ipual/hello_world/ko$ ls
hello.c  hello.ko  hello.mod.c  hello.mod.o  hello.o  Makefile  modules.order  Module.symvers

再将 hello.ko 文件复制到虚拟机上的共享目录 /nfsroot
关于 NFS 共享目录可以查看我的另一篇文章 Ubuntu 下搭建 NFS 服务

ipual@ipual-virtual-machine:/opt/ipual/hello_world/ko$ cp hello.ko /nfsroot/

打开开发板命令行界面,进入 /mnt 目录将 hello.ko 文件复制到 /opt ,目录下

root@imx6qpdlsolox:/mnt# cp hello.ko /opt/

进入 /opt 目录执行 insmod

root@imx6qpdlsolox:/mnt# cd /opt/
root@imx6qpdlsolox:/opt# insmod hello.ko 
Hello, world

输入 rmmod 即可卸载已挂载的驱动文件

root@imx6qpdlsolox:/opt# rmmod hello.ko 
Goodbye, cruel world

如果这里没有打印出 Hello, world ,可以通过如下指令查看内核 log

root@imx6qpdlsolox:/opt# tail /var/log/kern.log
Feb 15 00:06:06 imx6qpdlsolox kernel: nfs: server 10.86.141.169 not responding, timed out
Feb 15 00:08:06 imx6qpdlsolox kernel: nfs: server 10.86.141.169 not responding, timed out
Feb 15 00:13:14 imx6qpdlsolox kernel: nfs: server 10.86.141.169 not responding, timed out
Feb 15 00:20:21 imx6qpdlsolox kernel: nfs: server 10.86.141.169 not responding, timed out
Feb 15 00:22:20 imx6qpdlsolox kernel: nfs: server 10.86.141.169 not responding, timed out
Feb 15 00:27:28 imx6qpdlsolox kernel: nfs: server 10.86.141.169 not responding, timed out
Feb 15 00:34:36 imx6qpdlsolox kernel: nfs: server 10.86.141.169 not responding, timed out
Feb 15 00:36:35 imx6qpdlsolox kernel: nfs: server 10.86.141.169 not responding, timed out
Feb 15 01:46:32 imx6qpdlsolox kernel: Hello, world
Feb 15 01:49:13 imx6qpdlsolox kernel: Goodbye, cruel world

还以通过 file 命令查看 hello.ko 文件属性

root@imx6qpdlsolox:/opt# file hello.ko 
hello.ko: ELF 32-bit LSB relocatable, ARM, EABI5 version 1 (SYSV), BuildID[sha1]=9f9cab4703aa62c4d100586677b431e6c022b571, not stripped

到这里就完成了 helloworld 驱动的编译及加载。

以下为我整理的相关文章:
安装 i.MX6 交叉编译器
单独编译 i.mx6 kernel
Linux kernel 编译报错:ignoring invalid character `#’ in expression

参考链接:
linux下 驱动模块编译步骤
如何用arm-linux-gcc编译驱动程序,Makefile文件怎么写?

$(function(){ $.fn.extend({ SimpleTree:function(options){ //初始化参数 var option = $.extend({ click:function(a){ } },options); option.tree=this; /* 在参数对象中添加对当前菜单树的引用,以便在对象中使用该菜单树 */ option._init=function(){ /* * 初始化菜单展开状态,以及分叉节点的样式 */ this.tree.find("ul ul").hide(); /* 隐藏所有子级菜单 */ this.tree.find("ul ul").prev("li").removeClass("open"); /* 移除所有子级菜单父节点的 open 样式 */ this.tree.find("ul ul[show='true']").show(); /* 显示 show 属性为 true 的子级菜单 */ this.tree.find("ul ul[show='true']").prev("li").addClass("open"); /* 添加 show 属性为 true 的子级菜单父节点的 open 样式 */ }/* option._init() End */ /* 设置所有超链接不响应单击事件 */ this.find("a").click(function(){ $(this).parent("li").click(); return false; }); /* 菜单项 接受单击 */ this.find("li").click(function(){ /* * 当单击菜单项 * 1.触发用户自定义的单击事件,将该 标签中的第一个超链接做为参数传递过去 * 2.修改当前菜单项所属的子菜单的显示状态(如果等于 true 将其设置为 false,否则将其设置为 true) * 3.重新初始化菜单 */ option.click($(this).find("a")[0]); /* 触发单击 */ /* * 如果当前节点下面包含子菜单,并且其 show 属性的值为 true,则修改其 show 属性为 false * 否则修改其 show 属性为 true */ /* if($(this).next("ul").attr("show")=="true"){ $(this).next("ul").attr("show","false"); }else{ $(this).next("ul").attr("show","true"); }*/ /* 初始化菜单 */ option._init(); }); /* 设置所有父节点样式 */ this.find("ul").prev("li").addClass("folder"); /* 设置节点“是否包含子节点”属性 */ this.find("li").find("a").attr("hasChild",false); this.find("ul").prev("li").find("a").attr("hasChild",true); /* 初始化菜单 */ option._init(); }/* SimpleTree Function End */ }); });
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值