基于 i.mx6 开发板编译加载驱动文件过程总结
作为一名 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文件怎么写?
- 在linux中把汇编或c程序交叉编译成二进制文件烧录开发板过程详解
- java文件编译过程总结
- Android编译过程总结及android中各种img文件的作用以及系统启动过程
- web.xml被文件加载过程,各节点加载顺序总结
- java web.xml被文件加载过程,各节点加载顺序总结
- 关于java从编译成class文件到加载再到初始化过程解析
- web.xml被文件加载过程,各节点加载顺序总结
- micro2440开发板解决version magic问题,并在内核树中编译驱动的过程总结
- web.xml被文件加载过程,各节点加载顺序总结
- Android编译过程总结及android中各种img文件的作用以及系统启动过程
- MyBatis学习总结(一)---简单配置使用(基于XML方式实现CRUD以及加载配置文件的三种方式)
- Android编译过程总结及android中各种img文件的作用以及系统启动过程
- Hadoop-2.8.0集群搭建、hadoop源码编译和安装、host配置、ssh免密登录、hadoop配置文件中的参数配置参数总结、hadoop集群测试,安装过程中的常见错误
- 基于三星S3C2410的开发板的u-boot编译过程理解
- linux 驱动 编译加载 , 程序搬移到目标板,驱动源码和头文件位置
- <2012 12 06> FL2440开发板的U-boot-2010.09版本移植(十一)U-boot引导内核设置、编译linux内核、编译文件系统、加载...
- Android编译过程总结及android中各种img文件的作用以及系统启动过程
- U-boot、Kernel、文件系统的几种下载方式总结(基于TQ2440开发板)
- linux 内核驱动加载过程中 向文件系统中的文件进行读写操作
- 嵌入式内核及驱动开发-02驱动模块开发(编写驱动ko,内核编译,设备树文件的编译,网卡移植,简单驱动代码的编写,驱动Makefile编写,insmod加载ko模块,模块ko参数传递)