Android模拟器linux内核的下载,编译,运行,驱动开发测试

Android模拟器linux内核的下载,编译,运行,内核模块开发

1.下载适合Android模拟器的内核

git clone https://aosp.tuna.tsinghua.edu.cn/android/kernel/goldfish.git
git branch -a
git checkout android-goldfish-4.14-gchips 

新建一个目录存放内核源码

在这里插入图片描述

查看源码

在这里插入图片描述

编写脚本文件

在这里插入图片描述

使用内核镜像启动模拟器

在这里插入图片描述

2.linux内核模块

使用vscode打开下载下来的内核源码

添加一个源文件c

在这里插入图片描述

#include <linux/module.h>
#include <linux/init.h>

/**
 * __init 是一个宏,表示 hello_init 是一个初始化函数
 */ 
static int __init hello_init(void)
{
	//printk 是内核中的日志打印函数
	printk("Hello world!\n");
	return 0;
}
 
/**
 * __exit 是一个宏,表示 hello_exit 是一个初始化函数
 */ 
static void __exit hello_exit(void)
{
	printk("hello exit\n");
}

/**
 * hello_init 是当前模块的启动函数
 */ 
module_init(hello_init);
/*
 * hello_exit 是当前模块的退出函数
 */
module_exit(hello_exit);

把这段代码编译进内核,让系统内核启动就执行。

修改 /drivers/char/Kconfig,让我们这段代码模块,能在内核中编译。

增加一个配置选项,变量名是HELLO_MODULE,bool是提示信息,默认值是y,,表示在make menuconfig 启动内核的编译配置选项, 选中了 hello module support, CONFIG_HELLO_MODULE 的值是 y,没有选中值是 m:

在这里插入图片描述

在makefile文件中添加编译规则,hello_module.o是hello_module.c编译后的可执行文件

在这里插入图片描述

如果配置设置默认值是y就让hello_module.o编译进内核代码中,在内核启动就会启动执行里面的代码,如果默认值是m就不会编译到内核中,也就不会执行。

配置内核编译时的菜单选项

在这里插入图片描述

在这里插入图片描述

下面就是我们自定义的字符驱动程序了

在这里插入图片描述

编译内核驱动程序

在这里插入图片描述

在这里插入图片描述

打印内核日志,说明这个自定义的模块被编译到内核并且执行了

在这里插入图片描述

3.linux驱动开发

inux 中一切皆文件,访问硬件就是对文件的读写操作

创建三个文件,在用户态和内核态之间拷贝数据

在这里插入图片描述

#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>

/* 主设备                                                         */
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__);
    //hello是驱动的名字。hello_drv要注册给驱动的结构体
	major = register_chrdev(0, "hello", &hello_drv);  /* /dev/hello */

	//创建设备节点。
	// 创建这个文件/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 */
	//通过 /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");

Makefile,编译进内核,类似于app开发的build.gradle文件,指导源码如何编译成二进制可执行文件,

在这里插入图片描述

定义一个变量存储linux内核的源码路径

KERN_DIR = /home/android/Kernel/goldfish

目标变量all,modules表示编译我们的模块,是内核里面的一个模块,-C是指定内核的路径。M是指模块在哪里生称的位置,就是pwd当前目录下。会自动到你所指定的 dir 目录中查找模块源码。

all:
	make -C $(KERN_DIR) M=`pwd` modules 
obj-m	+= hello_drv.o

编译出.o可执行文件。

执行 ./build_driver.sh,编译出 hello_drv.ko,

编译驱动的脚本文件

#!/bin/bash
export ARCH=x86_64
export SUBARCH=x86_64
export CROSS_COMPILE=x86_64-linux-android-
export PATH=~/aosp/prebuilts/gcc/linux-x86/x86/x86_64-linux-android-4.9/bin:$PATH
make

查看设备节点是否创建

在这里插入图片描述

测试驱动程序读写

在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值