基于ipk安装包形式移植helloworld驱动模块到openwrt系统

在根据网上相关教程对openwrt系统进行helloworld驱动模块的文件进行添加的过程中,由于网上相关问题所使用的openwrt系统的版本较早,不具备较强的参考性,因此记录解决办法。

一、环境

openwrt开发环境为基于x86架构PC的windows系统上VMware Workstation 17 Player内运行的ubuntu18.04虚拟机。

openwrt系统为基于x86架构PC的windows系统上VMware Workstation 17 Player内运行的虚拟机,镜像默认名称为openwrt-x86-generic-generic-ext4-combined.vmdk,镜像为作者事先使用以上openwrt开发环境制作,并未使用官网提供的镜像,在之后的内容中会提到两种方式,而两种方式所使用的镜像有一个配置上的区别,后面会提到。

openwrt源码为GitHub - openwrt/openwrt: This repository is a mirror of https://git.openwrt.org/openwrt/openwrt.git It is for reference only and is not active for check-ins. We will continue to accept Pull Requests here. They will be merged via staging trees then into openwrt.git.上作者此时的master分支版本:

作者也不知道这个具体的版本号叫什么,在哪查看,对GitHub的概念确实不太熟,有知晓的麻烦告知一下,感激不尽。

二、步骤

1、编写源码

首先在开发环境的openwrt源码根目录中openwrt/package/kernel/路径下创建一个目录helloworld,编写一个简单的helloworld驱动模块进行测试,该驱动只在模块安装(insmod)和模块卸载(rmmod)时候printk打印出一段信息证明程序运行正常。

helloworld目录结构如下:

顶层Makefile脚本内容如下:


#
# Copyright (C) 2008-2012 OpenWrt.org
#
# This is free software, licensed under the GNU General Public License v2.
# See /LICENSE for more information.
#

include $(TOPDIR)/rules.mk
include $(INCLUDE_DIR)/kernel.mk

PKG_NAME:=helloworld
PKG_RELEASE:=1
PKG_LICENSE:=GPL-2.0

include $(INCLUDE_DIR)/package.mk

define KernelPackage/helloworld
  SUBMENU:=Other modules
  TITLE:=helloworld driver
  FILES:=$(PKG_BUILD_DIR)/helloworld.ko
  AUTOLOAD:=$(call AutoLoad,80,helloworld,1)
  KCONFIG:=
endef

define KernelPackage/helloworld/description
 helloworld
endef

define Build/Compile
        $(KERNEL_MAKE) M="$(PKG_BUILD_DIR)" modules
endef

$(eval $(call KernelPackage,helloworld))

子目录src的Makefile脚本内容如下:


obj-m += helloworld.o

子目录src的驱动源码文件helloworld.c内容如下:


/*
 *  GPIO Button Hotplug driver
 *
 *  Copyright (C) 2012 Felix Fietkau <nbd@nbd.name>
 *  Copyright (C) 2008-2010 Gabor Juhos <juhosg@openwrt.org>
 *
 *  Based on the diag.c - GPIO interface driver for Broadcom boards
 *    Copyright (C) 2006 Mike Baker <mbm@openwrt.org>,
 *    Copyright (C) 2006-2007 Felix Fietkau <nbd@nbd.name>
 *    Copyright (C) 2008 Andy Boyett <agb@openwrt.org>
 *
 *  This program is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License version 2 as published
 *  by the Free Software Foundation.
 */

#include <linux/module.h>
#include <linux/version.h>
#include <linux/kmod.h>

#include <linux/workqueue.h>
#include <linux/skbuff.h>
#include <linux/netlink.h>
#include <linux/kobject.h>
#include <linux/input.h>
#include <linux/interrupt.h>
#include <linux/platform_device.h>
#include <linux/of_irq.h>

static int __init helloworld_init(void)
{
    printk("helloworld driver init !\n");
    return 1;
}

static void __exit helloworld_exit(void)
{
    printk("helloworld driver exit !\n");
}

//声明出入口函数
module_init(helloworld_init);
module_exit(helloworld_exit);
MODULE_AUTHOR("ZHANG");
MODULE_DESCRIPTION("helloworld driver");
MODULE_LICENSE("GPL v2");                                                     

2、修改配置

在openwrt根目录运行make menuconfig后选中helloworld模块,选择Y(会显示为*号)并且使用后续所提到的两种方式中的第二种方式会编译出helloworld.ko模块在四个指定的目录且会编译进内核,选择M并且使用第一种方式会编译出helloworld.ko目录在四个指定的目录以及ipk安装包(ipk安装包后面会解释),无论后面的步骤选择什么方式,都可以拷贝这一步生成的helloworld.ko模块到openwrt系统安装模块,其中第一个目录的模块文件大小是最小的,其他三个目录的模块大概是第一个目录的七倍左右,以下是四个会生成模块的目录:

运行以下命令修改配置:


make menuconfig

这里我选择M,保存配置,因为需要先配合演示第一种方式:

3、第一种方式——单独编译ipk安装包

有两种方式可以选择,一种是单独编译驱动模块为ipk安装包,然后把驱动模块ipk安装包拷贝到openwrt系统上安装运行,还有一种是将驱动模块直接编译进openwrt系统。

这里先说第一种。第一种有一个前提,使用的是第2步配置里选择M后使用以下命令编译出来的镜像:


make V=s -j1

因为如果事先已经把驱动模块编译进镜像,那再移植个安装包过去没必要,效用重复。第一种实际上是不包括编译镜像这一步的,本就是为了提升效率,不必每次添加驱动模块都重新编译一次镜像,只编译出一个ipk安装包,作者个人理解和直接编译进镜像效果差不多,除此之外,还有ipk安装包比第2步四个目录中第一个目录的模块文件大小稍小这个优点,除了以上两点,不知道还有什么优点,有知晓的麻烦告知一下,感激不尽。

在openwrt根目录运行以下命令单独编译helloworld驱动模块的ipk安装包:


make package/kernel/helloworld/compile V=s

生成helloworld的ipk安装包在目录openwrt/bin/targets/x86/generic/packages/下:

可以看到刚刚单独生成的helloworld驱动模块的ipk安装包kmod-helloworld_5.15.98-1_i386_pentium4.ipk创建时间与其他的ipk安装包不同,是刚刚运行完命令的时间,所以确认它是新生成的,另外经测试运行make package/kernel/helloworld/compile V=s后第2步的四个目录会生成对应的helloworld.ko模块。

4、为openwrt安装sftp服务

将ipk安装包通过Filezilla的SFTP服务拷贝到openwrt虚拟机上,其它方式也可以。使用要求在openwrt虚拟机上先安装SFTP服务,在openwrt虚拟机上运行以下命令安装:


opkg update
opkg install vsftpd openssh-sftp-server

5、拷贝ipk安装包并运行

运行后重启板子或者vsftpd服务即可传输文件(其实直接用也行,要是不行就做这一步),传输完ipk安装包后进入到ipk安装包所在目录,使用opkg安装:


opkg install kmod-helloworld_5.15.98-1_i386_pentium4.ipk

运行lsmod命令可以查看当前模块:

然后分别运行以下两条命令安装和卸载模块:


insmod helloworld.ko
rmmod helloworld.ko

运行dmesg命令查看内核缓冲区即可看到打印内容:

第一种方式的步骤到此结束。

顺便提一下如果想要卸载刚才装的ipk安装包的软件,运行以下命令


opkg list_installed

查看安装软件的列表,然后运行opkg remove xxxx(xxxx为列表里的名字),原来ipk安装包的名字是无效的噢。

6、第二种方式——将驱动编译进openwrt镜像

这里接着第二步,首先运行以下命令修改配置:


make menuconfig

修改配置选中helloworld模块为Y,保存配置:

在openwrt根目录运行以下命令编译openwrt镜像:


make V=s -j1

经测试执行完此命令在第2步的四个目录下会生成对应helloworld.ko模块文件。在openwrt/bin/targets/x86/generic/下可以看到最新生成的镜像:

将openwrt-x86-generic-generic-ext4-combined.vmdk.gz解压出的镜像openwrt-x86-generic-generic-ext4-combined.vmdk替换openwrt虚拟机的硬盘或者重新以此镜像做一个openwrt虚拟机。

7、运行

经过配置网络等基本操作后,运行lsmod命令可以看到当前模块已有helloworld,运行rmmod命令卸载模块后再运行insmod安装模块,运行dmesg命令可以查看内核缓冲区有打印信息:

helloworld.ko模块文件在openwrt系统的/lib/modules/5.15.98/目录下。

第二种方式到此结束。

  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值