首页 > 系统相关 >ubuntu编译字符设备

ubuntu编译字符设备

时间:2023-04-20 15:46:15浏览次数:33  
标签:字符 buffer driver 编译 ubuntu world include hello size

前言

创建一个简单的字符设备驱动程序。

​ 本文命令的运行基本上都需要root权限,使用root账号,或者在命令前面加上sudo。

​ 如果你使用ssh远程连接的服务器进行代码编写。那么不要在root用户下创建文件或者文件夹。这会导致你ssh连接vscode编写代码的权限问题。可以在普通用户创建好所有的文件,然后编写。

代码

驱动程序

hello_driver.c

#include <linux/types.h>
#include <linux/init.h>
#include <linux/interrupt.h>
#include <linux/mm.h>
#include <linux/slab.h>
#include <linux/spinlock.h>
#include <linux/module.h>
#include <linux/device.h>
#include <linux/cdev.h>
 
 
dev_t hello_devid;
struct cdev hello_cdev;
int hello_major = 0;
int hello_minor;
 
 
uint8_t kernel_buffer[1024] = {0};
static struct class *hello_class;
 
 
 
static int hello_world_open(struct inode * inode, struct file * file)
{
	printk("hello_world_open\r\n");
	return 0;
}
 
static int hello_world_release (struct inode * inode, struct file * file)
{
	printk("hello_world_release\r\n");
	return 0;
}
 
static ssize_t hello_world_read (struct file * file, char __user * buffer, size_t size, loff_t * ppos)
{
	printk("hello_world_read size:%ld\r\n",size);
	copy_to_user(buffer,kernel_buffer,size);
	return size;
}
 
static ssize_t hello_world_write(struct file * file, const char __user * buffer, size_t size, loff_t *ppos)
{
	printk("hello_world_write size:%ld\r\n",size);
	copy_from_user(kernel_buffer,buffer,size);
	return size;
}
 
 
static const struct file_operations hello_world_fops = {
	.owner		= THIS_MODULE,
	.open		= hello_world_open,
	.release = hello_world_release,
	.read		= hello_world_read,
	.write	= hello_world_write,
};
 
 
static int __init hello_driver_init(void)
{
	int ret;
	printk("hello_driver_init\r\n");
 
	alloc_chrdev_region(&hello_devid, 0, 1, "hello");
	hello_major = MAJOR(hello_devid);
	hello_minor = MINOR(hello_devid);
	printk("hello driver major=%d,minor=%d\r\n",hello_major, hello_minor);	
 
	hello_cdev.owner = THIS_MODULE;
	cdev_init(&hello_cdev, &hello_world_fops);
	cdev_add(&hello_cdev, hello_devid, 1);
	
	hello_class = class_create(THIS_MODULE,"hello_class");
 
	device_create(hello_class,NULL,hello_devid,NULL,"hello"); /* /dev/hello */
 
	return 0;
}
 
static void __exit hello_driver_cleanup(void)
{
	printk("hello_driver_cleanup\r\n");
	cdev_del(&hello_cdev);
	unregister_chrdev_region(hello_devid, 1);
	
	device_destroy(hello_class,hello_devid);
	class_destroy(hello_class);
	
}
 
 
module_init(hello_driver_init);
module_exit(hello_driver_cleanup);
MODULE_LICENSE("GPL");

Makefile文件

KERNELDIR := /lib/modules/$(shell uname -r)/build
CURRENT_PATH := $(shell pwd)
obj-m := hello_driver.o

KBUILD_CFLAGS   += -Wno-unused-result -Wno-unused-variable
build: kernel_modules
 
kernel_modules:
	$(MAKE) ${CFLAGS} -C $(KERNELDIR) M=$(CURRENT_PATH) modules
	$(CROSS_COMPILE)gcc -o test_app test_app.c
clean:
	$(MAKE) -C $(KERNELDIR) M=$(CURRENT_PATH) clean
	rm -rf test_app

测试程序

test_app.c

#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>

uint8_t buffer[512] = {0};
 
int main(int argc, char *argv[])
{
	int fd;
	int ret;
	
	fd  = open(argv[1], O_RDWR);
    if(!fd)
    {
        printf("everthing is error\n");
    }
 
	if(!strcmp("read",argv[2]))
	{
		printf("read data from kernel\r\n");
		ret = read(fd,buffer,sizeof(buffer));
		printf("ret len:%d data:%s\r\n",ret,buffer);
	}
 
	if(!strcmp("write",argv[2]))
	{
		printf("write data to kernel %s len:%ld\r\n",argv[3],strlen(argv[3]));
		ret = write(fd,argv[3],strlen(argv[3]));
		printf("ret len:%d\r\n",ret);
	}
 
	
	close(fd);
 
	
}

编译

执行make命令编译

root@ubuntu:/home/dong/workspace/drivercode# make
make  -C /lib/modules/5.19.0-38-generic/build M=/home/dong/workspace/drivercode modules
make[1]: Entering directory '/usr/src/linux-headers-5.19.0-38-generic'
warning: the compiler differs from the one used to build the kernel
  The kernel was built by: x86_64-linux-gnu-gcc (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0
  You are using:           gcc (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0
  CC [M]  /home/dong/workspace/drivercode/hello_driver.o
  MODPOST /home/dong/workspace/drivercode/Module.symvers
  CC [M]  /home/dong/workspace/drivercode/hello_driver.mod.o
  LD [M]  /home/dong/workspace/drivercode/hello_driver.ko
  BTF [M] /home/dong/workspace/drivercode/hello_driver.ko
Skipping BTF generation for /home/dong/workspace/drivercode/hello_driver.ko due to unavailability of vmlinux
make[1]: Leaving directory '/usr/src/linux-headers-5.19.0-38-generic'
gcc -o test_app test_app.c

会生成hello_driver.ko文件和test_app

加载模块

insmod hello_driver.ko

卸载模块

rmmod hello_driver

查看模块

lsmod |grep hello_driver

运行测试

创建设备文件

这个文件不是随便创造的,会分配设备号,如果不使用分配的设备号创建会出现读写错误。

​ 每一次加载或者卸载模块都会有dmesg信息。

​ 使用dmesg -c清除信息。加载模块,使用dmesg查看模块信息。

root@ubuntu:/home/dong/workspace/drivercode# dmesg
[164284.337396] hello_driver_init
[164284.337399] hello driver major=238,minor=0

我的设备主设备号就是238,从设备号是0

创建设备文件命令

mknod /dev/hello c 238 0

测试

读 : ./test_app /dev/hello read

写: ./test_app /dev/hello write 需要写入的内容

标签:字符,buffer,driver,编译,ubuntu,world,include,hello,size
From: https://www.cnblogs.com/wdgray/p/17337041.html

相关文章

  • c++ 静态编译和动态编译
      C++是一种高级编程语言,它支持两种不同的编译方式:静态编译和动态编译。下面是它们的介绍:静态编译  静态编译是将程序代码和库函数一起编译成一个可执行文件的过程。在静态编译过程中,程序代码和库函数的代码被组合在一起,形成一个独立的可执行文件,该文件可以在任何系统上运行......
  • Ubuntu部署FastApi项目
    环境介绍系统:Ubuntu22.04Pyhton版本:3.8.10Fastapi版本:0.95.0Gunicorn版本:20.1.0准备工作1.ssh连接工具(本例使用基于Windows的Linux子系统中的ssh工具)2.配置nginx代理服务器3.配置GunicornWSGIHTTP服务器一、SSH连接Ubuntu服务器sshusername@hostusername......
  • jad 命令行批量反编译
    下载地址:http://varaneckas.com/jad/ 解压放到jdkbin目录比如需求是要把org文件夹下及其子文件夹下所有的class文件反编译后放到src目录中,并保持package原始结构命令这样:jad-r-dsrc-sjava"org/**/*.class"-r表示保持原始package结构-dsrc表示output目录为当前的src目录-......
  • 修改maven3项目的默认的编译级别(compile level)
    评:听闻maven的鼎鼎大名打算在最近的一个项目中试下爽,结果遇到了这个问题,虽对项目影响不大,但做技术刨根问题是必须的了,少废话。1.cmd命令建立web项目:mvnarchetype:generate-DgroupId=biz.yunduo-DartifactId=dts-DpackageName=dts-DarchetypeArtifactId=maven-archetype-we......
  • JavaScript字符串的常用操作
    在JavaScript中,字符串是不可变的,也就是说,一旦创建了一个字符串,就不能直接修改其值。如果需要对字符串进行修改,则需要创建一个新的字符串。字符串的增删改查操作如下:1.字符串的增加可以使用加号运算符`+`将两个字符串连接起来,从而实现字符串的增加。```javascriptvarstr1=......
  • ubuntu 22.04安装postgresql
    安装sudoaptinstallpostgresql修改/etc/postgresql/14/main/postgresql.conf把listen_addresses='127.0.0.1'修改为listen_addresses='*'/etc/postgresql/14/main/pg_hba.conf添加hostallall0.0.0.0/0m......
  • Azure DevOps(一)基于 Net6.0 的 WPF 程序如何进行持续集成、持续编译
    一,引言我们是否正在为如何快速的编译、部署客户端应用程序而烦恼?这也是博主最近遇到的问题。目前博主所在公司主要做项目级的定制化开发,多以C/S架构的WPF程序为主,每次到了协助开发团队给实施团队编译好的要测试程序包时,就会出现多人协助,编译、打包好的二进制程序包pull......
  • Java字符串
    StringJava中的字符串是String类的实例,字符串常量"xxxx" ,系统会自动创建一个对应的String类对象。注意,Java中的字符串并不是数组,字符串和数组没有直接关系。字符串对象一旦被创建,则其内容是不可变的。语法: String变量名[= ["xxxx"|newString("xxxx")] ]?;  ......
  • 编译器指令重排序问题(使用编译器屏障)
    环境:Windows平台:win7_64旗舰版、VS2019Linux平台:CentOSLinuxrelase7.2.1511、GCC_4.8.5-4场景:为了提高性能,编译器会对指令进行重新排序,在多线程环境下指令的乱序执行会造成无法预测的行为。开始:一、指令重排序实例inta=0,b=0;voidtest(){a=......
  • 力扣---1071. 字符串的最大公因子
    对于字符串s和t,只有在s=t+...+t(t自身连接1次或多次)时,我们才认定“t能除尽s”。给定两个字符串str1和str2。返回最长字符串x,要求满足x能除尽str1且X能除尽str2。示例1:输入:str1="ABCABC",str2="ABC"输出:"ABC"示例2:输入:str1="ABABAB",str2=......