5-Openwrt agregar módulo de función al paquete

Se menciona en el capítulo Makefile del paquete Openwrt, para agregar un capítulo para agregar un módulo personalizado, aquí hay dos ejemplos simples para mirar, de hecho, miramos que los ejemplos existentes probablemente pueden ser imitados.

1. Agregue el módulo de aplicación openwrt

De la siguiente manera, agregamos un módulo hello debajo del paquete, con los siguientes archivos

linye@ubuntu:~/14.07/package/hello$ tree
.
├── Makefile
└── src
    ├── hello.c
    └── Makefile

1 directory, 3 files

El Makefile más externo es el Makefile compilado de Openwrt. El significado específico de cada definición se puede ver en el capítulo Makefile del paquete Openwrt.

include $(TOPDIR)/rules.mk

PKG_NAME:=hello
PKG_VERSION:=1.01
PKG_BUILD_DIR:=$(BUILD_DIR)/$(PKG_NAME)_$(PKG_VERSION)

include $(INCLUDE_DIR)/package.mk

define Package/$(PKG_NAME)
    SUBMENU:=utils
    CATEGORY:=Base system
    TITLE:=hello test
endef

define Build/Prepare
    mkdir -p $(PKG_BUILD_DIR)
    $(CP) ./src/* $(PKG_BUILD_DIR)/
endef

TARGET_CFLAGS += -D_GNU_SOURCE

define Package/$(PKG_NAME)/install
    $(INSTALL_DIR) $(1)/usr/sbin
    $(INSTALL_BIN) $(PKG_BUILD_DIR)/$(PKG_NAME) $(1)/usr/sbin
endef

$(eval $(call BuildPackage,$(PKG_NAME)))

Makefile dentro de src se usa para compilar la función C

CFLAGS += -std=gnu99 -fstrict-aliasing -Iinclude -Wall -Werror
CFLAGS += -DRSA_VERIFY

all: hello

%.o: %.c
    $(CC) $(CFLAGS) -c -o $@ $^

zcheck: hello.o
    $(CC) $(LDFLAGS) -o $@ $^

.PHONY: clean

clean:
    rm -rf *.o hello

hola.c es la función principal

#include <stdio.h>

int main()
{
    printf("hello world\r\n");
}

Después de escribir el código, use make menuconfig para encontrar la opción hola, verifíquela

 Base system  --->
    utils  --->
        <*> hello...

Luego use el método de compilación del paquete make package/hello/compile V=99para compilar el módulo

Después de compilar, puede build_ver un hello_1.01módulo adicional a continuación

linye@ubuntu:~/14.07/build_dir/target-mipsel_1004kc_uClibc-0.9.33.2/hello_1.01$ tree
.
├── hello
├── hello.c
├── ipkg-mtk_1004kc
│   └── hello
│       ├── CONTROL
│       │   └── control
│       └── usr
│           └── sbin
│               └── hello
└── Makefile

5 directories, 5 files

Copia hola al tablero para correr

root@openwrt:/tmp# chmod +x hello
root@openwrt:/tmp# ./hello
hello world

Si primero agregamos scripts / archivos de configuración a este módulo, el enfoque general es crear un nuevo directorio de archivos y luego agregarlos de acuerdo con nuestra estructura rootfs.

Como sigue

linye@ubuntu:~/14.07/package/hello$ tree
.
├── files
│   ├── etc
│   │   ├── config
│   │   │   └── hello
│   │   └── init.d
│   │       └── autohello.sh
│   └── sbin
├── Makefile
└── src
    ├── hello.c
    └── Makefile

6 directories, 5 files

Luego copie estos archivos a las carpetas correspondientes en el Makefile

define Package/$(PKG_NAME)/install
    $(INSTALL_DIR) $(1)/usr/sbin
    $(INSTALL_BIN) $(PKG_BUILD_DIR)/$(PKG_NAME) $(1)/usr/sbin
    $(INSTALL_DIR) $(1)/etc/config
    $(INSTALL_DATA) ./files/etc/config/hello $(1)/etc/config/hello
    $(INSTALL_DIR) $(1)/etc/init.d
    $(INSTALL_BIN) ./files/etc/init.d/autohello.sh $(1)/etc/init.d/hello
endef

Luego use make V = 99 para volver a compilar.Una vez que se completa la compilación, puede ver en los rootfs que los archivos que necesitamos se han ejecutado en sus respectivos directorios, y el proceso puede comenzar automáticamente.

linye@ubuntu:~/14.07/build_dir/target-mipsel_1004kc_uClibc-0.9.33.2/rootfs$ find ./ -name hello
./usr/sbin/hello
./etc/config/hello
./etc/init.d/hello

2. Agregue el módulo del núcleo

El marco para agregar un módulo de kernel es similar al de agregar una aplicación. Las funciones de implementación interna directa son diferentes. De la siguiente manera, agregamos un módulo hello_kernel debajo del paquete, que tiene los siguientes archivos

linye@ubuntu:~/14.07/package/hello_kernel$ tree
.
├── Makefile
└── src
    ├── hello_kernel.c
    └── Makefile

1 directory, 3 files

El contenido del Makefile más externo es el siguiente, la diferencia se encuentra en la parte inferior$(eval $(call KernelPackage,$(PKG_NAME)))

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

PKG_NAME:=hello_kernel
PKG_VERSION:=1.01

include $(INCLUDE_DIR)/package.mk

define KernelPackage/$(PKG_NAME)
  SUBMENU:=Kernel Modules
  CATEGORY:=Base system
  TITLE:=netfilter debug
  DEPENDS:=kmod-ipt-conntrack
  FILES:=$(PKG_BUILD_DIR)/hello_kernel.ko
  AUTOLOAD:=$(call AutoLoad,66,hello_kernel)
endef

define Build/Prepare
    mkdir -p $(PKG_BUILD_DIR)
    $(CP) ./src/* $(PKG_BUILD_DIR)/
endef

define Build/Compile
    $(MAKE) -C "$(LINUX_DIR)" \
    CROSS_COMPILE="$(TARGET_CROSS)" \
    ARCH="$(LINUX_KARCH)" \
    SUBDIRS="$(PKG_BUILD_DIR)" \
    EXTRA_CFLAGS="$(BUILDFLAGS)" \
    modules
endef

$(eval $(call KernelPackage,$(PKG_NAME)))

Makefile en src

obj-m += hello_kernel.o

hello_kernel.c en src

#include <linux/init.h>      /* __init and __exit macroses */
#include <linux/kernel.h>    /* KERN_INFO macros */
#include <linux/module.h>    /* required for all kernel modules */


static int __init hello_init(void)
{
    printk("hello install\n");
    return 0;
}

static void __exit hello_exit(void)
{

    printk("hello uninstall\n");
}

module_init(hello_init);
module_exit(hello_exit);

MODULE_LICENSE("GPL");
MODULE_DESCRIPTION("hello Debug Module");

Después de agregar la información anterior, puede encontrar las siguientes opciones en make menuconfig

Base system  --->
    Kernel Modules  --->
        <*> kmod-hello_kernel.......

Después de compilar, en Linux, puede ver más contenido del módulo hello_kernel

linye@ubuntu:~/14.07/build_dir/target-mipsel_1004kc_uClibc-0.9.33.2/linux-mtk_mt7621/hello_kernel-1.01$ tree
.
├── hello_kernel.c
├── hello_kernel.ko
├── hello_kernel.mod.c
├── hello_kernel.mod.o
├── hello_kernel.o
├── ipkg-mtk_1004kc
│   └── kmod-hello_kernel
│       ├── CONTROL
│       │   ├── control
│       │   └── postinst
│       ├── etc
│       │   └── modules.d
│       │       └── 66-hello_kernel
│       └── lib
│           └── modules
│               └── 3.10.49
│                   └── hello_kernel.ko
├── Makefile
├── modules.order
└── Module.symvers

8 directories, 12 files

Copie el archivo hello_kernel.ko directamente a la placa para la instalación de insmod

root@openwrt:/tmp# insmod hello_kernel.ko
root@openwrt:/tmp# lsmod | grep hello
hello_kernel            1888  0
root@openwrt:/tmp# rmmod hello_kernel

dmesg puede ver la información de impresión del núcleo

[67961.350000] hello install
[67969.390000] hello uninstall
106 artículos originales publicados · 76 elogiados · 130,000 visitas +

Supongo que te gusta

Origin blog.csdn.net/Creator_Ly/article/details/100539577
Recomendado
Clasificación