当前位置:网站首页>Raspberry pie 4B compiling kernel module

Raspberry pie 4B compiling kernel module

2022-06-10 05:47:00 weixin_ forty-five million ninety thousand seven hundred and tw

Raspberry pie 4B Compiling kernel modules

Implement simple kernel module compilation and testing on raspberry pie

Download source code and compile

  1. Environmental preparation
sudo apt install git bc bison flex libssl-dev make
  1. github Download the source code
git clone --depth=1 https://github.com/raspberrypi/linux

This command will only download the latest version , There will be no history , It is recommended to use the following command to download the same kernel version as the system .
Download the specified version of the source code , With 5.10 For example :

sudo git clone --depth=1 https://github.com/raspberrypi/linux --branch rpi-5.10.y
  1. compile
    For raspberry pie 4B 64 position , Use the following commands to configure :
cd linux
KERNEL=kernel8
sudo make bcm2711_defconfig

compile :

sudo make -j4 Image.gz modules dtbs

If you need to update the raspberry pie kernel , You can use the following command :

sudo make modules_install
sudo cp arch/arm64/boot/dts/broadcom/*.dtb /boot/
sudo cp arch/arm64/boot/dts/overlays/*.dtb* /boot/overlays/
sudo cp arch/arm64/boot/dts/overlays/README /boot/overlays/
sudo cp arch/arm64/boot/Image.gz /boot/$KERNEL.img

The update here should be the boot time kernel image 、 Update the device tree , At the same time, some modules are placed in the file system .

Kernel module testing

  1. Writing kernel module code hello_kernel and Makefile.

hello_kernel.c

#include <linux/module.h>

static int __init  my_init(void)
{
    
        printk(KERN_INFO"Hello kernel!\n");
        return 0;
}
static void __exit my_exit(void)
{
    
        printk(KERN_INFO"Good bye!\n");
        return ;
}
module_init(my_init);
module_exit(my_exit);

MODULE_LICENSE("GPL");

Makefile as follows :

KERNELDIR ?=
PWD :=$(shell pwd)
obj-m := hello_kernel.o

all:
	make -C $(KERNELDIR) M=$(PWD) modules
clean:
	rm -rf *.o  *.ko  *.mod.c *.mod *.order *.symvers
  1. compile
sudo make KERNELDIR=/usr/src/linux
  1. test
     Insert picture description here
    Use dmesg View kernel information :
     Insert picture description here

Error log

  1. insmod: ERROR: could not insert module hello_kernel.ko: Invalid module format
     Insert picture description here
    dmesg -c View log information , It is found that there is a problem with the version .
    see linux Source code and raspberry pie version , Find inconsistencies , Download the source code consistent with the kernel version .

Reference article

  1. Official information of raspberry pie
原网站

版权声明
本文为[weixin_ forty-five million ninety thousand seven hundred and tw]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/161/202206100532019665.html