1.什么是内核?为什么要编译内核

“内核是操作系统最基本的部分。它是为众多应用程序提供对计算机硬件的安全访问的一部分软件,这种访问是有限的,并且内核决定一个程序在什么时候对某部分硬件操作多长时间。内核的分类可分为单内核和双内核以及微内核。严格地说,内核并不是计算机系统中必要的组成部分。”——百度百科释义。
简单来说,操作系统内核主要负责提供对计算机设备的核心管理调用。在Linux内核中,包括了进程管理、设备驱动程序、系统调用和安全防护、内存管理。

为什么在编译驱动前要先编译内核?

1.驱动程序要用到内核文件 ,个人理解是因为驱动是工作在内核态,所以需要依赖内核文件
例如驱动程序中包含头文件<asm/io.h>,其中asm是一个链接文件,可能指向asm-arm或指向asm-mips或其他目录,但是具体指向哪个呢?得先配置,编译内核,才会生成asm这个链接文件,指向具体的某个目录。
2.要让编译驱动时的内核和开发板上运行的内核一致
开发板上运行的内核时出厂时烧录的,编译驱动的时候用的内核时自己编译的,而内核不一致导致一些问题。所以要在编译驱动程序前,将编译出来的内核放到板子上,替代原有内核。
3.更换板子上的内核后,板子上的其他驱动也要更换

2.编译内核

2024.09.04补充:一般来说,开发板出厂会有sdk,用该sdk编译内核就行,问题是编译驱动时的工具链不能用自己的,得用sdk里的。
kernel的编译过程如下:

 make mrproper
 make 100ask_imx6ull_defconfig #make xxxdeconfig
 make zImage -j4
 make dtbs
 cp arch/arm/boot/zImage ~/nfs_rootfs #将编译出的zImage内核文件,.dtb设备树二进制文件拷贝到挂载目录
 cp arch/arm/boot/dts/100ask_imx6ull-14x14.dtb ~/nfs_rootfs

编译完成后生成的文件如下图所示:
在这里插入图片描述
编译完成后,在 arch/arm/boot 目录下生成 zImage 内核文件, 在arch/arm/boot/dts 目录下生成设备 树的二进制文件100ask_imx6ull-14x14.dtb。把这 2 个文件复制到/home/book/nfs_rootfs 目录下备用,如图所示
在这里插入图片描述

3.编译内核模块

一些自己写的驱动,也是采用编译内核模块的方式编译。
进入内核源码目录,编译内核模块:
输入命令:
cd ~/100ask_imx6ull-sdk/Linux-4.9.88/
make modules
在这里插入图片描述
内核模块编译完成后如下图所示:
在这里插入图片描述
安装内核模块到Ubuntu某个目录备用,因为后面会用到tree命令查看目录结构,所以如果没有该命令,需要执行以下命令安装tree命令:
sudo apt install tree
执行以下命令安装模块
cd ~/100ask_imx6ull-sdk/Linux-4.9.88/
ARCH=arm INSTALL_MOD_PATH=/home/book/nfs_rootfs modules_install
把模块安装在nfs目录下
在这里插入图片描述
安装好驱动后的/home/book/nfs_rootfs/目录结构如图所示
在这里插入图片描述

4.安装内核和模块到开发板上

cp /mnt/100ask_imx6ull-14x14.dtb /boot #更新设备树
#执行上述命令拷贝文件后,内容可能还在内存里,执行以下命令同步一下,将内容刷到flash中。
sync
cp /mnt/lib/modules /lib -rfd #覆盖modules,其中-rfd的r是指递归复制,f是指强制复制,d是指之前是链接文件

执行reboot命令重启开发板。
在开发板中执行uname -a
可以发现已使用刚刚编译好的内核,安装完毕。
在这里插入图片描述

5.编译、测试第一个程序

写驱动程序要注意:1.将编译好的内核目录包含上,该目录里有Makefile和kconfig。
2.交叉编译工具链得用sdk里的,别用自己的
在这里插入图片描述
输入命令insmod hello_drv.ko装载驱动程序
在这里插入图片描述
执行驱动程序,-w写入字符串,-r是读取。
在这里插入图片描述
代码如下(摘自韦东山的驱动开发基础知识):

//**************hello_dev.c驱动程序******************//
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/errno.h>
#include <linux/miscdevice.h>
#include <linux/kernel.h>
#include <linux/major.h>
#include <linux/mutex.h>
#include <linux/proc_fs.h>
#include <linux/seq_file.h>
#include <linux/stat.h>
#include <linux/init.h>
#include <linux/device.h>
#include <linux/tty.h>
#include <linux/kmod.h>
#include <linux/gfp.h>

/* 1. 确定主设备号                                                                 */
static int major = 0;
static char kernel_buf[1024];
static struct class *hello_class;


#define MIN(a, b) (a < b ? a : b)

/* 3. 实现对应的open/read/write等函数,填入file_operations结构体                   */
static ssize_t hello_drv_read (struct file *file, char __user *buf, size_t size, loff_t *offset)
{
	int err;
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	err = copy_to_user(buf, kernel_buf, MIN(1024, size));
	return MIN(1024, size);
}

static ssize_t hello_drv_write (struct file *file, const char __user *buf, size_t size, loff_t *offset)
{
	int err;
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	err = copy_from_user(kernel_buf, buf, MIN(1024, size));
	return MIN(1024, size);
}

static int hello_drv_open (struct inode *node, struct file *file)
{
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	return 0;
}

static int hello_drv_close (struct inode *node, struct file *file)
{
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	return 0;
}

/* 2. 定义自己的file_operations结构体                                              */
static struct file_operations hello_drv = {
	.owner	 = THIS_MODULE,
	.open    = hello_drv_open,
	.read    = hello_drv_read,
	.write   = hello_drv_write,
	.release = hello_drv_close,
};

/* 4. 把file_operations结构体告诉内核:注册驱动程序                                */
/* 5. 谁来注册驱动程序啊?得有一个入口函数:安装驱动程序时,就会去调用这个入口函数 */
static int __init hello_init(void)
{
	int err;
	
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	major = register_chrdev(0, "hello", &hello_drv);  /* /dev/hello */


	hello_class = class_create(THIS_MODULE, "hello_class");
	err = PTR_ERR(hello_class);
	if (IS_ERR(hello_class)) {
		printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
		unregister_chrdev(major, "hello");
		return -1;
	}
	
	device_create(hello_class, NULL, MKDEV(major, 0), NULL, "hello"); /* /dev/hello */
	
	return 0;
}

/* 6. 有入口函数就应该有出口函数:卸载驱动程序时,就会去调用这个出口函数           */
static void __exit hello_exit(void)
{
	printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
	device_destroy(hello_class, MKDEV(major, 0));
	class_destroy(hello_class);
	unregister_chrdev(major, "hello");
}


/* 7. 其他完善:提供设备信息,自动创建设备节点                                     */

module_init(hello_init);
module_exit(hello_exit);

MODULE_LICENSE("GPL");
//*****hello_test,hellowrold应用程序******//
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>

/*
 * ./hello_drv_test -w abc
 * ./hello_drv_test -r
 */
int main(int argc, char **argv)
{
	int fd;
	char buf[1024];
	int len;
	
	/* 1. 判断参数 */
	if (argc < 2) 
	{
		printf("Usage: %s -w <string>\n", argv[0]);
		printf("       %s -r\n", argv[0]);
		return -1;
	}

	/* 2. 打开文件 */
	fd = open("/dev/hello", O_RDWR);
	if (fd == -1)
	{
		printf("can not open file /dev/hello\n");
		return -1;
	}

	/* 3. 写文件或读文件 */
	if ((0 == strcmp(argv[1], "-w")) && (argc == 3))
	{
		len = strlen(argv[2]) + 1;
		len = len < 1024 ? len : 1024;
		write(fd, argv[2], len);
	}
	else
	{
		len = read(fd, buf, 1024);		
		buf[1023] = '\0';
		printf("APP read : %s\n", buf);
	}
	
	close(fd);
	
	return 0;
}

# 1. 使用不同的开发板内核时, 一定要修改KERN_DIR
# 2. KERN_DIR中的内核要事先配置、编译, 为了能编译内核, 要先设置下列环境变量:
#以下xxx变量均采用内核sdk提供的路径和工具链
# 2.1 ARCH,          比如: export ARCH=arm64
# 2.2 CROSS_COMPILE, 比如: export CROSS_COMPILE=xxx
# 2.3 PATH,          比如: export PATH=$PATH:xxx

KERN_DIR = #内核源码路径

all:
    #单模块编译
	make -C $(KERN_DIR) M=`pwd` modules 
	#编译hello_drv_test应用程序
	$(CROSS_COMPILE)gcc -o hello_drv_test hello_drv_test.c 

clean:
#这一句我们拆成两个阶段,由简入繁分析。-C $(KERNEL_DIR) 代表切换工作目录,因为内核源码顶层的Makefile文件定义了伪目标 modules,所以要先将工作目录切换到内核源码顶层 Makefile 所在位置。
#M='pwd' 表示回到当前路径继续执行当前的Makefile。这个可以让makefile 回到自己所指定的目录下查找模块源码,将其编译,生成 ko 文件。
	make -C $(KERN_DIR) M=`pwd` modules clean
	rm -rf modules.order
	rm -f hello_drv_test

#obj-m 表示把文件 chrdevbase.o 作为"模块"编译,不会编译进内核,但会生成一个独立的 ko 文件
#obj-y 则是直接编译进内核,即加入到内核源码中
obj-m	+= hello_drv.o

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐