您的位置:首页 > 其它

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

2019-02-15 10:03 399 查看

作为一名 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文件怎么写?

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐