搜索
bottom↓
回复: 32

原创:为DE1-SOC搭建linux工具链,可编译xxx.ko

[复制链接]

出0入0汤圆

发表于 2015-4-24 04:46:30 | 显示全部楼层 |阅读模式
terasic 有点坑爹DE1-soc这块基于Cyclone-V soc的开发板除了Altera的 DS-5 AE以外没有放出任何工具链以及内核的源码。而且配套教材全是及其简单的一些应用的开发,涉及到驱动部分都是用/dev/mem在访问物理内存。这你让人怎么玩!!!!
在其他网友的提醒下我用用linaro搭建了de1-soc的工具链,过程异常艰 辛现在分享出来。这下可以好好玩了






版权归amobbs  YFM所有,转载请注明来处
安装ubuntu12.04LTS
这个就不说了在Vmware Player下装就可以

为DE1-SOC制作SD启动盘
从terasic官网上下载Linux BSP (Board Support Package): MicroSD Card Image中的Linux Console

http://www.terasic.com.cn/cgi-bi ... No=870&PartNo=4


安装工具链
http://releases.linaro.org/14.05/components/toolchain/binaries下载交叉编译器gcc-linaro-arm-linux-gnueabihf-4.9-2014.05_linux.tar.xz
以上的编译器是ubuntu下可执行的文件因此不用编译他。
解压xz –d xxxxx.tar.xz
     tar –xvf xxxxx.tar
添加PATH变量export PATH=$PATH:\..\ gcc-linaro-arm-linux-gnueabihf-4.9-2014.05_linux\bin  
\..\为您老解压的这个交叉编译器的目录

重新登录系统 用arm-linux-gnueabihf-gcc  –v验证是否PATH环境变量添加成功。

构建内核树
下载内核 3.12.0 版本
解压内核 xz –b linux-3.12.tar.xz
tar –xvf linux-3.12.tar
进入内核源码的文件夹
gedit 顶层Makefile
修改ARCH ?= $(SUBARCH) 为  ARCH = arm
修改CROSS_COMPILE ?=  为 CROSS_COMPILE = arm-linux-gnueabihf-
make menuconfig发现如下错误:
*** Unable to find the ncurses libraries or the
*** required header files.
*** 'make menuconfig' requires the ncurses libraries.
***
*** Install ncurses (ncurses-devel) and try again.
***
make[1]: *** [scripts/kconfig/dochecklxdialog] Error 1
make: *** [menuconfig] Error 2
原因是缺少 libncurses5-dev这个包
用apt-get install libncurses5-dev 安装这个包。在ubuntu14.40版本下apt-get找不到这个包我就又重新装了一个ubuntu12.04版的ubuntu 然后就可以安装这个libncurses5-dev的包了。

然后make下面这个文件出现错误 出错原因不明
drivers/staging/lustre/lustre/lov/lov_pack.c
这个是lustre的分布式文件系统 直接进menuconfig –> device driver->staging device 直接把这个模块去掉。
然后继续make
        make modules
        make modules_install
顺利完成后在ubuntu 的 /lib/modules/下可以看到3.12.0文件夹这个就是内核树。
传输文件到DE1-SOC并运行
用生成的内核树编译驱动程序hello.c生成 hello.ko
scp hello.ko root@192.168.1.253:/home
将hello.ko送到主机名为root IP地址为192.168.1.253运行在DE1-SOC上的linux系统的/home文件夹下
然后在DE1-SOC终端上insmod hello.ko出现下面的错误:
hello: version magic '3.12.0 SMP mod_unload modversions ARMv7 p2v8 ' should be '3.12.0-00307-g507abb4-dirty SMP mod_unload ARMv7 p2v8 '
Error: could not insert module hello.ko: Invalid module format
错误原因是内核树和DE1-SOC上运行的linux的vervision不一样
3.12.0-00307-g507abb4-dirty SMP mod_unload ARMv7 p2v8 这个为DE1-SOC上kernel的version

3.12.0 SMP mod_unload modversions ARMv7 p2v8 是我内核树的version
内核树的version少了-00307-g507abb4-dirty却多了modversions
少了-00307-g507abb4-dirty的解决方法请您老参见http://linux.die.net/lkmpg/x380.html
现复制原网页内容如下



2.8. Building modules for a precompiled kernel
Obviously, we strongly suggest you to recompile your kernel, so that you can enable a number of useful debugging features, such as forced module unloading (MODULE_FORCE_UNLOAD): when this option is enabled, you can force the kernel to unload a module even when it believes it is unsafe, via a rmmod -f module command. This option can save you a lot of time and a number of reboots during the development of a module.
Nevertheless, there is a number of cases in which you may want to load your module into a precompiled running kernel, such as the ones shipped with common Linux distributions, or a kernel you have compiled in the past. In certain circumstances you could require to compile and insert a module into a running kernel which you are not allowed to recompile, or on a machine that you prefer not to reboot. If you can't think of a case that will force you to use modules for a precompiled kernel you might want to skip this and treat the rest of this chapter as a big footnote.
Now, if you just install a kernel source tree, use it to compile your kernel module and you try to insert your module into the kernel, in most cases you would obtain an error as follows:
insmod: error inserting 'poet_atkm.ko': -1 Invalid module format
       
Less cryptical information are logged to /var/log/messages:
Jun  4 22:07:54 localhost kernel: poet_atkm: version magic '2.6.5-1.358custom 686
REGPARM 4KSTACKS gcc-3.3' should be '2.6.5-1.358 686 REGPARM 4KSTACKS gcc-3.3'
       
In other words, your kernel refuses to accept your module because version strings (more precisely, version magics) do not match. Incidentally, version magics are stored in the module object in the form of a static string, starting with vermagic:. Version data are inserted in your module when it is linked against theinit/vermagic.o file. To inspect version magics and other strings stored in a given module, issue the modinfo module.ko command:
[root@pcsenonsrv 02-HelloWorld]# modinfo hello-4.ko
license:        GPL
author:         Peter Jay Salzman <p@dirac.org>
description:    A sample driver
vermagic:       2.6.5-1.358 686 REGPARM 4KSTACKS gcc-3.3
depends:        
       
To overcome this problem we could resort to the --force-vermagic option, but this solution is potentially unsafe, and unquestionably inacceptable in production modules. Consequently, we want to compile our module in an environment which was identical to the one in which our precompiled kernel was built. How to do this, is the subject of the remainder of this chapter.
First of all, make sure that a kernel source tree is available, having exactly the same version as your current kernel. Then, find the configuration file which was used to compile your precompiled kernel. Usually, this is available in your current /boot directory, under a name like config-2.6.x. You may just want to copy it to your kernel source tree: cp /boot/config-`uname -r` /usr/src/linux-`uname -r`/.config.
Let's focus again on the previous error message: a closer look at the version magic strings suggests that, even with two configuration files which are exactly the same, a slight difference in the version magic could be possible, and it is sufficient to prevent insertion of the module into the kernel. That slight difference, namely the custom string which appears in the module's version magic and not in the kernel's one, is due to a modification with respect to the original, in the makefile that some distribution include. Then, examine your /usr/src/linux/Makefile, and make sure that the specified version information matches exactly the one used for your current kernel. For example, you makefile could start as follows:
VERSION = 2
PATCHLEVEL = 6
SUBLEVEL = 5
EXTRAVERSION = -1.358custom
...
       
In this case, you need to restore the value of symbol EXTRAVERSION to -1.358. We suggest to keep a backup copy of the makefile used to compile your kernel available in/lib/modules/2.6.5-1.358/build. A simple cp /lib/modules/`uname -r`/build/Makefile /usr/src/linux-`uname -r` should suffice. Additionally, if you already started a kernel build with the previous (wrong) Makefile, you should also rerun make, or directly modify symbol UTS_RELEASE in file /usr/src/linux-2.6.x/include/linux/version.haccording to contents of file /lib/modules/2.6.x/build/include/linux/version.h, or overwrite the latter with the first.
Now, please run make to update configuration and version headers and objects:
[root@pcsenonsrv linux-2.6.x]# make
CHK     include/linux/version.h
UPD     include/linux/version.h
SYMLINK include/asm -> include/asm-i386
SPLIT   include/linux/autoconf.h -> include/config/*
HOSTCC  scripts/basic/fixdep
HOSTCC  scripts/basic/split-include
HOSTCC  scripts/basic/docproc
HOSTCC  scripts/conmakehash
HOSTCC  scripts/kallsyms
CC      scripts/empty.o
...
       
If you do not desire to actually compile the kernel, you can interrupt the build process (CTRL-C) just after the SPLIT line, because at that time, the files you need will be are ready. Now you can turn back to the directory of your module and compile it: It will be built exactly according your current kernel settings, and it will load into it without any errors.



对于以上这个我是这样做的:
在建立内核树的源代码下修改顶层Makefile。在顶层Makefile的开始有如下内容
VERSION = 3
PATCHLEVEL = 12
SUBLEVEL = 0
EXTRAVERSION =
EXTRAVERSION为空这就是-00307-g507abb4-dirty不存在的原因。EXTRAVERSION=后面加上-00307-g507abb4-dirty。

多了modversions的解决方法为在建立内核树的源代码下make menuconfig 然后关闭
Enable loadable module support --->Module versioning support 这个选项。

最后重新make
        make modules
        make modules_install
最后您老会发现在lib/modules下多了一个3.12.0-00307-g507abb4-dirty的文件夹,这个就是新生成的内核树。

最后的最后编译ko模块传到DE1-SOC。insmod成功加载。


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x

出0入0汤圆

 楼主| 发表于 2015-4-24 05:17:08 | 显示全部楼层
最后还要在menuconfig下进入选中 System Type--->Altera SOCFPGA family

出0入0汤圆

发表于 2015-4-24 08:08:26 来自手机 | 显示全部楼层
kernel没试过,uboot轻松编译运行,gcc也是一样的

出0入0汤圆

发表于 2015-4-25 06:07:29 来自手机 | 显示全部楼层
两个建议:1.编译内核时make使用"O=目标目录",将编译输出到指定目录,方便查看编译结果和具体用了哪些代码,自动生成了哪些文件,而且不会影响源码目录,便于多个不同板子共用同一目录。2.安装目module的时候,用module install prefix指定安装目录(具体环境变量名称用make help查一下),否则编译的代码要是与主机版本相同,就悲剧了。

出0入0汤圆

 楼主| 发表于 2015-4-25 15:47:02 来自手机 | 显示全部楼层
hiberhe 发表于 2015-4-25 06:07
两个建议:1.编译内核时make使用"O=目标目录",将编译输出到指定目录,方便查看编译结果和具体用了哪些代码 ...

第二个里面主机版本,这个主机指的是ARM上运行的linux还是交叉工具链所在的pc上运行的linux?

出0入0汤圆

发表于 2015-4-25 17:10:12 来自手机 | 显示全部楼层
我要裸跑这个 官方有例子吗 或者fae那有吗

出0入0汤圆

 楼主| 发表于 2015-4-25 18:18:44 | 显示全部楼层
这两天用这个工具链编译内核hello模块发现.ko加载进去没有打印出来 hello的字符
原因如下:
      是因为内核树构建的时候源码配置和目标板上的不一致
解决办法:
      内核树构建应用如下方法:
             将de1-soc linux中的/proc/config.gz文件拷贝出来,这个文件里含有目标板上kernel编译的时候的.config文件
             将config.gz文件压缩的.config文件拷贝到内核源码目录里覆盖原先的.config
             然后make oldconfig
             最后重新编译内核make
                       make modules
                       make modules_install
幸好这样做了不然之后编译其他驱动程序并运行的时候肯定会出很多问题。

出0入0汤圆

 楼主| 发表于 2015-4-25 18:24:51 | 显示全部楼层
lyxer 发表于 2015-4-25 17:10
我要裸跑这个 官方有例子吗 或者fae那有吗

裸跑没试过,要用DS-5 AE吧。我这个就不准备裸跑,裸跑光芯片启动就很烦。不过你这也没必要裸跑啊,都上这么贵的芯片了成本肯定都不怎么敏感了。加个Flash或SD卡直接上系统,代码运行效率和裸跑没什么区别啊。

出0入0汤圆

发表于 2015-4-25 18:30:22 | 显示全部楼层

出0入0汤圆

发表于 2015-4-28 09:07:33 | 显示全部楼层
刚好最近在这块,参考一下

出0入0汤圆

发表于 2015-5-9 17:31:49 | 显示全部楼层
也有一块DE1 SOC,有时间试一试。感谢楼主分享

出0入0汤圆

发表于 2015-5-28 18:50:54 | 显示全部楼层
本帖最后由 Liyicheng 于 2015-5-28 19:00 编辑

用DE1玩过一段时间,然后老师给了DE1SoC让我升级玩,把官方给的手册全部玩过一遍,官方给的controlPanel那个示例给出了工具链的安装方式,而且给了用这块板开发QT界面的一套流程,看似给了很高自由度,其实没有,最近想要在板子上安上USB摄像头用,然后发现相关内核完全没有,insmod又因为设备号的问题总是不成功,正好楼主给出的东西解决了我的一点忧虑,但是我下不到3.12.0的内核楼主能说一下在哪找到的么?


本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x

出0入0汤圆

发表于 2015-5-28 23:28:59 来自手机 | 显示全部楼层
Rocketboards.org上有这个板子吗

出0入0汤圆

发表于 2015-5-29 09:22:14 来自手机 | 显示全部楼层
有的,而且有一些相关资料

出0入0汤圆

发表于 2015-5-29 20:38:16 | 显示全部楼层
YFM 发表于 2015-4-25 18:18
这两天用这个工具链编译内核hello模块发现.ko加载进去没有打印出来 hello的字符
原因如下:
      是因为内 ...

关于那个覆盖config的问题,如果直接用板子上的config文件那么之前所做的那些配置岂不是没有效果?

出0入0汤圆

 楼主| 发表于 2015-5-30 01:01:14 | 显示全部楼层
Liyicheng 发表于 2015-5-29 20:38
关于那个覆盖config的问题,如果直接用板子上的config文件那么之前所做的那些配置岂不是没有效果? ...

3.12.0版本在3.12系列的最后。
直接用板子上的config覆盖之前的配置没有用。

出0入0汤圆

发表于 2015-5-30 15:29:57 | 显示全部楼层
YFM 发表于 2015-5-30 01:01
3.12.0版本在3.12系列的最后。
直接用板子上的config覆盖之前的配置没有用。 ...

哎,真折腾,原来在后面,不过我直接下了个别的版本的然后直接修改subversion至0
我现在就是在DE1-SoC上装UVC,然后完成arch等的修改,并menuconfig 加上了UVC等模块,最后将make好的模块(make了快1个半小时)传输到板子上
版本问题解决了,就是那个should be...
但是模块还是不能加载,估计问题出在模块内部编译时用到的库的问题
我要加载的那个模块是没有depends 的
在板上加载时 显示 could not insert module media.ko: unknown symbol in module
那两个symbol 是: __stack_chk_guard   __stack_chk_fail
网上查过仍然是版本的问题,方法还是在makefile
哎。。估计又要折腾一天,如果整完了必将第一时间通知各位

出0入0汤圆

发表于 2015-5-30 16:09:29 | 显示全部楼层
YFM 发表于 2015-5-30 01:01
3.12.0版本在3.12系列的最后。
直接用板子上的config覆盖之前的配置没有用。 ...

这...我用板子上的覆盖后make oldconfig了一下,现在的kernel和开发板上的一样了。。。。那之前menuconfig添加所做的添加模块无效了

出0入0汤圆

 楼主| 发表于 2015-5-31 02:38:35 | 显示全部楼层
Liyicheng 发表于 2015-5-30 16:09
这...我用板子上的覆盖后make oldconfig了一下,现在的kernel和开发板上的一样了。。。。那之前menuconfi ...

那就编译成ko模块然后系统启动后insmod进去

出0入0汤圆

发表于 2015-5-31 13:57:48 | 显示全部楼层
YFM 发表于 2015-5-31 02:38
那就编译成ko模块然后系统启动后insmod进去

没错,我就是这么做的
成功安装了几个模块,但还有一些即使依赖装好了也装不进去,昨天一天在愁这事
截一下成功安装的几个模块,证明楼主的伟大

顺便截失败的:说一下 我先把生成的ko都在media 文件夹里 复制于板上
然后成功加载了几个模块(当时那个兴奋啊)
然后根据modinfo 把安装某些模块要有的依赖都装上
可以看到 vmalloc 那个模块要先装上memops
但可以看到memops已经装上,vmalloc模块加载失败
根据错误我找到可能要装一个dma的模块
那个模块我在不用板上的config文件配置时生成过
不过现在用板上的配置模块不会生成该文件

接下来就搞笑了,那个dma文件说要装memops
不是装好了么
哎,还在想办法中
兄弟,我QQ:2389181535 一起讨论技术啊



本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x

出0入0汤圆

发表于 2015-5-31 19:34:08 | 显示全部楼层
翻看以前的帖子顿时感觉楼主和我是不是校友

出0入0汤圆

发表于 2015-7-16 10:56:51 | 显示全部楼层
在和网友 (QQ昵称:依然半兽人) 的讨论下,解决了问题 我会尽快出说明 无需再QQ联系我

出0入0汤圆

发表于 2016-4-11 20:11:08 | 显示全部楼层
Liyicheng 发表于 2015-5-28 18:50
用DE1玩过一段时间,然后老师给了DE1SoC让我升级玩,把官方给的手册全部玩过一遍,官方给的controlPanel那 ...

您好!请问目前你USB摄像头驱动是否已经加载,我现在也碰见同样的问题,请多多指教!谢谢!

出0入0汤圆

发表于 2016-4-11 21:51:05 | 显示全部楼层
YFM 发表于 2015-4-25 18:18
这两天用这个工具链编译内核hello模块发现.ko加载进去没有打印出来 hello的字符
原因如下:
      是因为内 ...

可我的/proc 目录下没有任何文件?不知是什么原因?请指教,谢谢!

出0入0汤圆

 楼主| 发表于 2016-4-11 22:36:56 | 显示全部楼层
14htdeng 发表于 2016-4-11 20:11
您好!请问目前你USB摄像头驱动是否已经加载,我现在也碰见同样的问题,请多多指教!谢谢! ...

我可以在/dev下面看到video0设备文件但深入的没有测试过。你摄像头是V4L的吗? 在官方给的系统的linux系统的配置下,你再进menuconfig使能V4L等摄像头相关的内核选项然后make 结束之后会提示你生成了哪些模块把这些.ko模块拷贝到开发板文件系统然后insmod这些模块。注意insmod的顺序。

出0入0汤圆

发表于 2016-4-12 11:07:48 | 显示全部楼层
YFM 发表于 2016-4-11 22:36
我可以在/dev下面看到video0设备文件但深入的没有测试过。你摄像头是V4L的吗? 在官方给的系统的linux系 ...

您好!我用的是V4L摄像头!liunux内核是linux-socfpga-socfpga-3.12,开发板上内核是DE1-SoC官方自带的内核,我按照你的方法修改,可是编译成功加载后还是不成功, version magic '3.12.0-00307-g507abb4-dirty mod_unload ARMv5 ' should be '3.12.0-00307-g507abb4-dirty SMP mod_unload ARMv7 p2v8 '
Error: could not insert module gpio-altera.ko: Invalid module format,不知道到底出错在哪里?再次麻烦你指导下,我的QQ是1181150422,谢谢了!

出0入0汤圆

发表于 2016-4-12 13:59:21 | 显示全部楼层
14htdeng 发表于 2016-4-12 11:07
您好!我用的是V4L摄像头!liunux内核是linux-socfpga-socfpga-3.12,开发板上内核是DE1-SoC官方自带的内 ...

在加载模块时,现在出现uvcvideo: Unknown symbol vb2_queue_init (err 0)   uvcvideo: Unknown symbol v4l2_fh_exit (err 0)  ?该怎么解决,谢谢!

出0入0汤圆

发表于 2016-4-12 16:30:44 | 显示全部楼层
您好!我按照你的步骤做了 ,在make、make modules时没有出错, 可是在执行make modules_install, 出现arm-linux-gnueabihf-gcc: Command not found,但在/lib文件夹生成了可加载模块,将模块移植到开发板上时,通过insmod加载时,又出错误了:uvcvideo: Unknown symbol vb2_queue_init (err 0)
         uvcvideo: Unknown symbol v4l2_fh_exit (err 0)  请问群主这到底是什么原因造成?麻烦群主指导,非常感谢!
祝好
邓海涛

出0入0汤圆

发表于 2016-4-25 15:46:07 | 显示全部楼层
Liyicheng 发表于 2015-5-31 13:57
没错,我就是这么做的
成功安装了几个模块,但还有一些即使依赖装好了也装不进去,昨天一天在愁这事
截一 ...

朋友,您好!在加载uvcvideo驱动时,也碰见和你一样的问题,请教下该怎样解决?谢谢

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?注册

x

出0入0汤圆

 楼主| 发表于 2016-4-26 14:02:22 来自手机 | 显示全部楼层
14htdeng 发表于 2016-4-25 15:46
朋友,您好!在加载uvcvideo驱动时,也碰见和你一样的问题,请教下该怎样解决?谢谢 ...

注意模块的加载依赖次序

出0入0汤圆

发表于 2016-4-26 14:50:32 | 显示全部楼层
YFM 发表于 2016-4-26 14:02
注意模块的加载依赖次序

我用命令modinfo各模块的依赖关系,并进行相应的添加,可是在添加模块insmod videobuf2-core.ko和模块insmod videobuf2-vmalloc.ko时,一直说dma-buf-vmap和dma-buf-get等有错误,可编译时,并没有这几个模块可添加啊?都整了好久,

出0入0汤圆

发表于 2016-5-26 21:47:54 | 显示全部楼层
请教一下楼主,Cyclone-V soc除了支持Linux外,还支持其他操作系统吗?需要实时操作系统

出0入0汤圆

发表于 2016-6-26 10:02:46 | 显示全部楼层
mark一下,有时间回来研究一下
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-4-20 08:02

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表