linux驱动和应用程序如何上传到路由器

一、上传驱动流程

下面我将介绍一下如何将在Ubuntu上写的驱动程序刷进路由器上,本实验是以小米路由器为例的
1.首先在路径 /home/openwrt6/openwrt/package/kernel/  (该路径就是你所编译好的openwrt系统所在路径,openwrt目录下有package,package下有kernel)
然后在此目录下建立example文件夹,然后在里面建立src文件夹,并将准备好的Makefile文件拷贝进去,然后把你的驱动源程序拷贝在在src文件夹下,然后将相应的Makefile文件和Kconfig文件拷贝进去,
之后Kconfig和两个Makefile文件只需要对照我上传的文件修改相应的程序文件名即可
2.进入到openwrt文件夹下
执行命令 make package/kernel/example/compile V=99  
!!!若在执行过make命令之后发现日志信息提醒需要进行make menuconfig
则执行 Make menuconfig -> kernel modules -> other modules ->找到自己的模块然后按键M ->save ->ok ->ok ->exit
这一条命令是将驱动程序编译成能在路由器上进行安装的软件 *.ipk
3.确保你的电脑连着路由器的网络,然后进入 /home/openwrt6/openwrt/bin/ramips/package/base 即在base文件夹下
使用putty登陆路由器,在路由器root目录下建立hello文件夹
执行命令 scp kmod*.ipk [email protected]:hello (*代表第2步生成的ipk软件的名字)
4.使用putty登录路由器查看ipk软件是否上传成功
5.若上传成功,则安装ipk软件即相当于安装了驱动 
执行命令 opkg install *.ipk
若要卸载则执行 rmmod example.ko 
6.检查一下驱动是否挂载成功 执行命令 lsmod
因为程序中打印了 hello world 所以也可以检查一下是否打印日志成功 执行命令 dmesg |grep “”“Hello world” (或者直接 dmesg)

二、上传用户应用程序

1.在openwrt/package应用目录。参考其他的应用文件。创建helloworld文件夹,并进入。

创建Makefile:

##############################################
# OpenWrt Makefile for helloworld program
#
#
# Most of the variables used here are defined in
# the include directives below. We just need to
# specify a basic description of the package,
# where to build our program, where to find
# the source files, and where to install the
# compiled program on the router.
#
# Be very careful of spacing in this file.
# Indents should be tabs, not spaces, and
# there should be no trailing whitespace in
# lines that are not commented.
#
##############################################
include $(TOPDIR)/rules.mk


# Name and release number of this package
PKG_NAME:=helloworld
PKG_RELEASE:=1


# This specifies the directory where we're going to build the program. 
# The root build directory, $(BUILD_DIR), is by default the build_mipsel
# directory in your OpenWrt SDK directory


PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME)


include $(INCLUDE_DIR)/package.mk


# Specify package information for this program.
# The variables defined here should be self explanatory.
# If you are running Kamikaze, delete the DESCRIPTION
# variable below and uncomment the Kamikaze define
# directive for the description below
define Package/helloworld
SECTION:=utils
CATEGORY:=Utilities
TITLE:=Helloworld -- prints a snarky message
endef
# Uncomment portion below for Kamikaze and delete DESCRIPTION variable above
define Package/helloworld/description
If you can't figure out what this program does, you're probably
brain-dead and need immediate medical attention.
endef
# Specify what needs to be done to prepare for building the package.
# In our case, we need to copy the source files to the build directory.
# This is NOT the default.  The default uses the PKG_SOURCE_URL and the
# PKG_SOURCE which is not defined here to download the source from the web.
# In order to just build a simple program that we have just written, it is
# much easier to do it this way.
define Build/Prepare
mkdir -p $(PKG_BUILD_DIR)
$(CP) ./src/* $(PKG_BUILD_DIR)/
endef


define Build/Configure
endef




define Build/Compile
$(MAKE) -C $(PKG_BUILD_DIR) \
CC="$(TARGET_CC)" \
CFLAGS="$(TARGET_CFLAGS) -Wall" \
LDFLAGS="$(TARGET_LDFLAGS)"
endef
# We do not need to define Build/Configure or Build/Compile directives
# The defaults are appropriate for compiling a simple program such as this one
# Specify where and how to install the program. Since we only have one file,
# the helloworld executable, install it by copying it to the /bin directory on
# the router. The $(1) variable represents the root directory on the router running
# OpenWrt. The $(INSTALL_DIR) variable contains a command to prepare the install
# directory if it does not already exist.  Likewise $(INSTALL_BIN) contains the
# command to copy the binary file from its current location (in our case the build
# directory) to the install directory.
define Package/helloworld/install
$(INSTALL_DIR) $(1)/bin
$(INSTALL_BIN) $(PKG_BUILD_DIR)/helloworld $(1)/bin/
endef
# This line executes the necessary commands to compile our program.
# The above define directives specify all the information needed, but this
# line calls BuildPackage which in turn actually uses this information to
# build a package.
$(eval $(call BuildPackage,helloworld))

2:在helloworld目录创建src文件夹,并进入。创建Makefile和helloworld.c:

# build helloworld executable when user executes "make"
helloworld: helloworld.o
$(CC) $(LDFLAGS) helloworld.o -o helloworld
helloworld.o: helloworld.c
$(CC) $(CFLAGS) -c helloworld.c
# remove object files and executable when user executes "make clean"
clean:
rm *.o helloworld

3.helloworld.c
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>


int main(int argc, char**argv)
{
    printf("Hell! O' world, why won't my code compile?\n\n");
    return 0;
}

4.make menuconfig-->Utilities-->helloworld。然后make ./package/helloworld/compile V=s( 只能在目录 root@ubuntu:/home/zhuyong/openwrt6/openwrt 下编辑)

5.最后在base文件夹下像上传驱动一样将用户程序的.ipk上传到路由器

6.在路由器上用opkg install 命令安装应用程序(同驱动程序)

7.之后打出应用程序的名字+tab+enter就可以执行了


猜你喜欢

转载自blog.csdn.net/manmanmanli/article/details/76097795