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

描述

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

 

Linux系统

编译内核

左右滑动查看完整内容

 

~/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文件就是修改的内容了。

 

  审核编辑:汤梓红

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

全部0条评论

快来发表一下你的评论吧 !

×
20
完善资料,
赚取积分