0
  • 聊天消息
  • 系统消息
  • 评论与回复
登录后你可以
  • 下载海量资料
  • 学习在线课程
  • 观看技术视频
  • 写文章/发帖/加入社区
创作中心

完善资料让更多小伙伴认识你,还能领取20积分哦,立即完善>

3天内不再提示

RZ/G2L Linux系统如何添加新的内核模块

瑞萨MCU小百科 来源:瑞萨MCU小百科 2024-01-04 12:19 次阅读

RZ/G2L Linux系统的镜像基于yocto构建,本篇介绍如何添加新的内核模块。

方式1:内核源码外添加

方式2:内核源码中添加

需要提前已按照文档构建好开发环境和安装SDK,本篇依据G2L VLP3.0.3

方式1示例

A. bitbake编译模块方式

目录和内容参考:

左右滑动查看完整内容

rzg2l_vlp_v3.0.3$ tree meta-renesas/meta-rz-common/recipes-kernel/kernel-module-helloworld/
meta-renesas/meta-rz-common/recipes-kernel/kernel-module-helloworld/
├── files
│  ├── helloworld.c
│  └── Makefile
└── kernel-module-helloworld.bb

helloworld.c

左右滑动查看完整内容

#include 
static int hello_world_init(void)
{
  printk("Hello world
");
  return 0;
}
static void hello_world_exit(void)
{
  printk("Bye world
");
}
module_init(hello_world_init);
module_exit(hello_world_exit);
MODULE_LICENSE("GPL v2");

Makefile

左右滑动查看完整内容

obj-m := helloworld.o
SRC := $(shell pwd)
all:
    make -C $(KERNELSRC) M=$(SRC) modules
install:
    make -C $(KERNELSRC) M=$(SRC) modules_install
clean:
    rm -f *.o *~ core .depend .*.cmd *.ko *.mod.c
    rm -f Module.markers Module.symvers modules.order
    rm -rf .tmp_versions Modules.symvers

kernel-module-helloworld.bb

左右滑动查看完整内容

SRC_URI = " 
  file://helloworld.c 
  file://Makefile 
"
S = "${WORKDIR}"
EXTRA_OEMAKE = "KERNELDIR=${STAGING_KERNEL_BUILDDIR}"
EXTRA_OEMAKE += "CROSS_COMPILE=${CROSS_COMPILE}"


KERNEL_MODULE_PACKAGE_SUFFIX = ""
do_install() {
  # Create destination directory
  install -d ${D}/lib/modules/${KERNEL_VERSION}/extra/


  # Install kernel module
  install -m 644 ${S}/helloworld.ko ${D}/lib/modules/${KERNEL_VERSION}/extra/


  # Install module symbol file
  install -m 644 ${S}/Module.symvers ${STAGING_KERNEL_BUILDDIR}/helloworld.symvers
}
PACKAGES = " 
  ${PN} 
"
FILES_${PN} = " 
  /lib/modules/${KERNEL_VERSION}/extra/helloworld.ko 

编译模块:

左右滑动查看完整内容

~/rzg2l_vlp_v3.0.3$ source poky/oe-init-build-env
### Shell environment set up for builds. ###


You can now run 'bitbake '


Targets are:
  core-image-minimal
  core-image-bsp
  core-image-weston
  core-image-qt
hank@rz:~/rzg2l_vlp_v3.0.3/build$ MACHINE=smarc-rzg2l bitbake -s | grep hello
go-helloworld                     :0.1-r0
kernel-module-helloworld               :1.0-r0
lib32-go-helloworld                  :0.1-r0
hank@rz:~/rzg2l_vlp_v3.0.3/build$ MACHINE=smarc-rzg2l bitbake kernel-module-helloworld
WARNING: Layer qt5-layer should set LAYERSERIES_COMPAT_qt5-layer in its conf/layer.conf file to list the core layer names it is compatible with.
WARNING: Layer qt5-layer should set LAYERSERIES_COMPAT_qt5-layer in its conf/layer.conf file to list the core layer names it is compatible with.
Loading cache: 100% 
…
NOTE: Tasks Summary: Attempted 650 tasks of which 635 didn't need to be rerun and all succeeded.

查看结果:

左右滑动查看完整内容

build/tmp/work/smarc_rzg2l-poky-linux/kernel-module-helloworld/1.0-r0/helloworld.ko

如果想编译到rootfs,请修改conf/local.conf追加内容并重新编译rootfs

左右滑动查看完整内容

MACHINE_EXTRA_RRECOMMENDS = " kernel-module-helloworld"

库文件包已含在rootfs内

左右滑动查看完整内容

build$ find ./tmp/work/smarc_rzg2l-poky-linux/ -name helloworld.ko
./tmp/work/smarc_rzg2l-poky-linux/core-image-qt/1.0-r0/rootfs/lib/modules/5.10.158-cip22-yocto-standard/extra/helloworld.ko
./tmp/work/smarc_rzg2l-poky-linux/kernel-module-helloworld/1.0-r0/helloworld.ko

B.在源码目录直接编译方式

左右滑动查看完整内容

cd /opt/poky_vlp3.0.3/sysroots/aarch64-poky-linux/lib/modules/5.10.158-cip22-yocto-standard/build
source /opt/poky_vlp3.0.3/environment-setup-aarch64-poky-linux
sudo chown -R $USER .
make scripts
make prepare
mkdir hello //并拷贝上边的源码文件
/opt/poky_vlp3.0.3/sysroots/aarch64-poky-linux/lib/modules/5.10.158-cip22-yocto-standard/build/hello$ ls
helloworld.c  Makefile
/opt/poky_vlp3.0.3/sysroots/aarch64-poky-linux/lib/modules/5.10.158-cip22-yocto-standard/build/hello$ make
make -C /opt/poky_vlp3.0.3/sysroots/aarch64-poky-linux/usr/src/kernel M=/opt/poky_vlp3.0.3/sysroots/aarch64-poky-linux/lib/modules/5.10.158-cip22-yocto-standard/build/hello modules
make[1]: Entering directory '/opt/poky_vlp3.0.3/sysroots/aarch64-poky-linux/lib/modules/5.10.158-cip22-yocto-standard/build'
 CC [M] /opt/poky_vlp3.0.3/sysroots/aarch64-poky-linux/lib/modules/5.10.158-cip22-yocto-standard/build/hello/helloworld.o
 MODPOST /opt/poky_vlp3.0.3/sysroots/aarch64-poky-linux/lib/modules/5.10.158-cip22-yocto-standard/build/hello/Module.symvers
 CC [M] /opt/poky_vlp3.0.3/sysroots/aarch64-poky-linux/lib/modules/5.10.158-cip22-yocto-standard/build/hello/helloworld.mod.o
 LD [M] /opt/poky_vlp3.0.3/sysroots/aarch64-poky-linux/lib/modules/5.10.158-cip22-yocto-standard/build/hello/helloworld.ko
make[1]: Leaving directory '/opt/poky_vlp3.0.3/sysroots/aarch64-poky-linux/lib/modules/5.10.158-cip22-yocto-standard/build'
hank@rz:/opt/poky_vlp3.0.3/sysroots/aarch64-poky-linux/lib/modules/5.10.158-cip22-yocto-standard/build/hello$ ls
helloworld.c helloworld.ko helloworld.mod helloworld.mod.c helloworld.mod.o helloworld.o Makefile modules.order Module.symvers

方式2示例

首先提取内核源码:

左右滑动查看完整内容

~/rzg2l_vlp_v3.0.3$ source poky/oe-init-build-env
~/rzg2l_vlp_v3.0.3/build$ MACHINE=smarc-rzg2l devtool modify linux-renesas
NOTE: Starting bitbake server...
……
INFO: Adding local source files to srctree...
INFO: Copying kernel config to srctree
INFO: Source tree extracted to /home/xxx/rzg2l_vlp_v3.0.3/build/workspace/sources/linux-renesas
WARNING: SRC_URI is conditionally overridden in this recipe, thus several devtool-override-* branches have been created, one for each override that makes changes to SRC_URI. It is recommended that you make changes to the devtool branch first, then checkout and rebase each devtool-override-* branch and update any unique patches there (duplicates on those branches will be ignored by devtool finish/update-recipe)
INFO: Recipe linux-renesas now set up to build from /home/xxx/rzg2l_vlp_v3.0.3/build/workspace/sources/linux-renesas

上面最后一行就是Linux内核源码提取后所在目录,即/home/xxx/rzg2l_vlp_v3.0.3/build/workspace/sources/linux-renesas, 然后去这个目录修改代码即可。

进入Linux源码目录下找个目录增加模块代码,这里使用build/workspace/sources/ linux-renesas /drivers/char目录,在该目录下执行mkdir hello创建目录,然后在hello目录下创建以下文件。

左右滑动查看完整内容

~/rzg2l_vlp_v3.0.3/build/workspace/sources/linux-renesas/drivers/char$ tree hello/
hello/
├── hello.c
├── Kconfig
└── Makefile

hello.c

左右滑动查看完整内容

#include 
static int hello_world_init(void)
{
  printk("Hello world
");
  return 0;
}
static void hello_world_exit(void)
{
  printk("Bye world
");
}
module_init(hello_world_init);
module_exit(hello_world_exit);
MODULE_LICENSE("GPL v2");

Kconfig

左右滑动查看完整内容

config HELLO
    tristate 'Create a hello module'
    default n
    help
        This is a module to print Hello World!

Makefile

obj-$(CONFIG_HELLO) += hello.o

修改build/workspace/sources/ linux-renesas /drivers/char目录的Kconfig和Makefile:

左右滑动查看完整内容

diff --git a/drivers/char/Kconfig b/drivers/char/Kconfig
index b4e65d1ed..2b96630ab 100644
--- a/drivers/char/Kconfig
+++ b/drivers/char/Kconfig
@@ -508,4 +508,6 @@ config RANDOM_TRUST_BOOTLOADER
     believe its RNG facilities may be faulty. This may also be configured
     at boot time with "random.trust_bootloader=on/off".


+source "drivers/char/hello/Kconfig"
+
 endmenu
diff --git a/drivers/char/Makefile b/drivers/char/Makefile
index ffce287ef..3056303ff 100644
--- a/drivers/char/Makefile
+++ b/drivers/char/Makefile
@@ -47,3 +47,5 @@ obj-$(CONFIG_PS3_FLASH)        += ps3flash.o
 obj-$(CONFIG_XILLYBUS)     += xillybus/
 obj-$(CONFIG_POWERNV_OP_PANEL) += powernv-op-panel.o
 obj-$(CONFIG_ADI)       += adi.o
+
+obj-$(CONFIG_HELLO) += hello/

返回yocto顶层目录,选择内核配置

左右滑动查看完整内容

~/rzg2l_vlp_v3.0.3/build$ source poky/oe-init-build-env
~/rzg2l_vlp_v3.0.3/build$ MACHINE=smarc-rzg2l bitbake virtual/kernel -c menuconfig

b0390fec-aab6-11ee-8b88-92fbcf53809c.png

编译内核

左右滑动查看完整内容

~/rzg2l_vlp_v3.0.3/build$ MACHINE=smarc-rzg2l devtool build linux-renesas
NOTE: Starting bitbake server...
……
NOTE: Tasks Summary: Attempted 607 tasks of which 594 didn't need to be rerun and all succeeded.

生成结果

左右滑动查看完整内容

~/rzg2l_vlp_v3.0.3/build$ ls tmp/work/smarc_rzg2l-poky-linux/linux-renesas/5.10.158-cip22+git999-r1/linux-renesas-5.10.158-cip22+git999/drivers/char/hello/
built-in.a hello.o modules.order

最后制作补丁,创建自己的layer保存补丁。

左右滑动查看完整内容

~/rzg2l_vlp_v3.0.3$ source poky/oe-init-build-env
~/rzg2l_vlp_v3.0.3/build$ bitbake-layers create-layer ../meta-mylayer
~/rzg2l_vlp_v3.0.3/build$ bitbake-layers add-layer ../meta-mylayer
hank@rz:~/rzg2l_vlp_v3.0.3/build$ tree ../meta-mylayer/
../meta-mylayer/
├── conf
│  └── layer.conf
├── COPYING.MIT
├── README
└── recipes-example
  └── example
    └── example_0.1.bb


3 directories, 4 files

再次进入源码目录,提交修改,生成补丁

左右滑动查看完整内容

~/rzg2l_vlp_v3.0.3/build/workspace/sources/linux-renesas$ git status .
Refresh index: 100% (70807/70807), done.
On branch devtool
Changes not staged for commit:
 (use "git add ..." to update what will be committed)
 (use "git restore ..." to discard changes in working directory)
    modified:  drivers/char/Kconfig
    modified:  drivers/char/Makefile


Untracked files:
 (use "git add ..." to include in what will be committed)
    drivers/char/hello/


no changes added to commit (use "git add" and/or "git commit -a")
~/rzg2l_vlp_v3.0.3/build/workspace/sources/linux-renesas$ git add ./*
The following paths are ignored by one of your .gitignore files:
oe-logs
oe-workdir
Use -f if you really want to add them.
~/rzg2l_vlp_v3.0.3/build/workspace/sources/linux-renesas$ git commit -m "add the hello module"
[devtool 6dc52bd44] add the hello module
 5 files changed, 25 insertions(+)
 create mode 100644 drivers/char/hello/Kconfig
 create mode 100644 drivers/char/hello/Makefile
 create mode 100644 drivers/char/hello/hello.c

切到build目录执行

左右滑动查看完整内容

~/rzg2l_vlp_v3.0.3/build$ MACHINE=smarc-rzg2l devtool finish linux-renesas ../meta-mylayer/
NOTE: Starting bitbake server...
……
Parsing of 2151 .bb files complete (0 cached, 2151 parsed). 5440 targets, 887 skipped, 3 masked, 0 errors.
……
INFO: Leaving source tree /home/xxx/rzg2l_vlp_v3.0.3/build/workspace/sources/linux-renesas as-is; if you no longer need it then please delete it manually
~/rzg2l_vlp_v3.0.3/build$ tree ../meta-mylayer/
../meta-mylayer/
├── conf
│  └── layer.conf
├── COPYING.MIT
├── README
├── recipes-example
│  └── example
│    └── example_0.1.bb
└── recipes-kernel
  └── linux
    ├── linux-renesas
    │  ├── 0001-add-the-hello-module.patch
    │  └── devtool-fragment.cfg
    └── linux-renesas_%.bbappend


6 directories, 7 files

上边patch文件就是修改的内容了。

审核编辑:汤梓红

声明:本文内容及配图由入驻作者撰写或者入驻合作网站授权转载。文章观点仅代表作者本人,不代表电子发烧友网立场。文章及其配图仅供工程师学习之用,如有内容侵权或者其他违规问题,请联系本站处理。 举报投诉
  • 内核
    +关注

    关注

    3

    文章

    1309

    浏览量

    39850
  • Linux
    +关注

    关注

    87

    文章

    10990

    浏览量

    206738
  • Linux系统
    +关注

    关注

    4

    文章

    567

    浏览量

    26923
  • 源码
    +关注

    关注

    8

    文章

    574

    浏览量

    28589
  • SDK
    SDK
    +关注

    关注

    3

    文章

    966

    浏览量

    44709

原文标题:RZ/G2L添加新的内核模块

文章出处:【微信号:瑞萨MCU小百科,微信公众号:瑞萨MCU小百科】欢迎添加关注!文章转载请注明出处。

收藏 人收藏

    评论

    相关推荐

    G2L系列 核心板 -RZ/G2L 处理器简介|框架图|功耗|原理图及硬件设计指南

    用户便捷开发,轻松选型。 三、RZ/G2L系列 Linux系统整机功耗表很多小伙伴对FET-G2LD-C核心板和OK-
    发表于 06-21 14:45

    【飞凌RZ/G2L开发板试用体验】开箱帖子,飞凌质量YYDS

    ,品质指的信赖。飞凌RZ/G2L开发板基于瑞萨RZ/G2L,在智能工控领域的一款高性能、超高效处理器。RZ/
    发表于 08-15 18:46

    【飞凌RZ/G2L开发板试用体验】+003+烧录程序

    【飞凌RZ/G2L开发板试用体验】+03+开发环境与烧录程序 本篇测试的目的是熟悉开发环境,完成烧录,1. 开发环境  安装ubuntu,打开开发资料
    发表于 08-22 22:24

    【飞凌RZ/G2L开发板试用体验】+01.开箱(zmj)

    ,主要应用于各种具有视频输出的工控行业。2.硬件讲解硬件讲解:从外观结构上看,飞凌RZ/G2L开发板使用模块和扩展板的方式组成,重点是飞凌扩展板原理图/PCB图免费提供,扩展板可以更换
    发表于 08-28 19:13

    【飞凌RZ/G2L开发板试用体验】+02.开发环境体验(zmj)

    ;#036; sudo apt-get install vim //安装编辑器3.RZ/G2L开发环境编译测试使用飞凌提供的RZ/G2L开发环境可以事半功倍,大大提升开发效率。
    发表于 08-28 20:22

    【飞凌RZ/G2L开发板】飞凌RZ/G2L开发板试用测评报告

    飞凌RZ/G2L开发板试用测评报告大信(QQ:8125036)在电子发烧友论坛上看到飞凌RZ/G2L的开发板介绍,其优秀的高性能低能耗引起我的兴趣,在结合其强大的音视频能力,感觉该开发
    发表于 08-29 02:01

    【飞凌RZ/G2L开发板试用体验】+08.RZ/V2L开发板的QT测试(zmj)

    【飞凌RZ/G2L开发板试用体验】+08.RZ/V2L开发板的QT测试(zmj)Qt-Creator 是一个跨平台的 QT 集成开发环境(IDE),包括了高级 C++代码编辑器、项目和
    发表于 09-06 20:17

    【米尔瑞萨RZ/G2L开发板-试用体验】开箱

    感谢 感谢电子发烧友论坛、感谢米尔电子,把米尔瑞萨RZ/G2L开发板试用话动的机会给了我。虽然周五就收到了开发板,但是由于复阳了,为了能及时的完成试用活动,所以今天努力的爬起来完成开箱报告。 开箱
    发表于 05-14 19:41

    【米尔瑞萨RZ/G2L开发板-试用体验】米尔瑞萨RZ/G2L开发板开箱视频

    今天刚刚收到米尔瑞萨RZ/G2L开发板,拆开包裹后给人的感觉是惊艳,板卡设计真的很棒,来看看视频做个简单了解吧。 更多板卡可以登录官网了解哦。https://www.myir.cn/
    发表于 05-22 21:58

    【米尔瑞萨RZ/G2L开发板-试用体验】米尔瑞萨RZ/G2L开发板使用SSH登录

    收到的米尔瑞萨RZ/G2L开发板上电测试一下SSH登录方式和其它测试! SSH登录 在使用之前,需要事先连接网络,笔者这里使用的是以太网,事先需要使用串口的登录,然后输入以下命令查看IP地址
    发表于 06-11 21:47

    【米尔瑞萨RZ/G2L开发板-试用体验】认识一下米尔瑞萨RZ/G2L开发板的核心板

    收到米尔瑞萨RZ/G2L开发板后一直对米尔旗下开发板的做工感到非常精致,同时也有着很强大的功能,也一直很喜欢米尔系列开发板。 引领工业市场从32位MPU向64位演进 基于瑞萨高性价比RZ/G2
    发表于 07-29 00:21

    Linux设备驱动开发详解》第4章、Linux内核模块

    Linux设备驱动开发详解》第4章、Linux内核模块
    发表于 10-27 14:15 0次下载
    《<b class='flag-5'>Linux</b>设备驱动开发详解》第4章、<b class='flag-5'>Linux</b><b class='flag-5'>内核模块</b>

    什么是 Linux 内核模块?

    lsmod 命令能够告诉你当前系统上加载了哪些内核模块,以及关于使用它们的一些有趣的细节。
    的头像 发表于 08-09 17:01 2987次阅读

    嵌入式LINUX系统内核内核模块调试教程

    本文档的主要内容详细介绍的是嵌入式LINUX系统内核内核模块调试教程。
    发表于 11-06 17:32 21次下载
    嵌入式<b class='flag-5'>LINUX</b><b class='flag-5'>系统</b><b class='flag-5'>内核</b>和<b class='flag-5'>内核模块</b>调试教程

    嵌入式LINUX系统内核内核模块调试

    嵌入式LINUX系统内核内核模块调试(嵌入式开发和硬件开发)-嵌入式LINUX系统
    发表于 07-30 13:55 9次下载
    嵌入式<b class='flag-5'>LINUX</b><b class='flag-5'>系统</b><b class='flag-5'>内核</b>和<b class='flag-5'>内核模块</b>调试