• 【全志T113-S3_100ask】2-编写第一个驱动


    (1)开发环境

    windows开发环境:VsCode
    linux 开发环境:Ubuntu 18.04 环境参考

    1、指定交叉编译

    在buildroot 目录下发现两个 arm-linux-gnueabi-gcc

    root@znh-ubuntu:/disk/buildroot-100ask_t113-pro/buildroot# find ./* -name "*gnueabi-gcc"
    ./output/host/opt/ext-toolchain/bin/arm-linux-gnueabi-gcc
    ./output/host/bin/arm-linux-gnueabi-gcc
    
    • 1
    • 2
    • 3

    但 ./output/host/bin/arm-linux-gnueabi-gcc 是链接过去的
    在这里插入图片描述
    ./output/host/opt/ext-toolchain/bin/arm-linux-gnueabi-gcc 是原始的
    在这里插入图片描述

    在这里我使用链接后的gcc
    加载环境变量:

    export PATH=$PATH:/disk/buildroot-100ask_t113-pro/buildroot/output/host/bin
    export ARCH=arm
    export CROSS_COMPILE=arm-linux-gnueabi-
    
    • 1
    • 2
    • 3
    root@znh-ubuntu:~# export PATH=$PATH:/disk/buildroot-100ask_t113-pro/buildroot/output/host/bin
    root@znh-ubuntu:~# export ARCH=arm
    root@znh-ubuntu:~# export CROSS_COMPILE=arm-linux-gnueabi-
    root@znh-ubuntu:~#
    root@znh-ubuntu:~# arm-linux-gnueabi-gcc -v
    使用内建 specs。
    COLLECT_GCC=arm-linux-gnueabi-gcc
    COLLECT_LTO_WRAPPER=/disk/t113GitHub/eLinuxCore_100ask-t113-pro/toolchain/gcc-linaro-7.2.1-2017.11-x86_64_arm-linux-gnueabi/bin/../libexec/gcc/arm-linux-gnueabi/7.2.1/lto-wrapper
    目标:arm-linux-gnueabi
    配置为:'/home/tcwg-buildslave/workspace/tcwg-make-release/builder_arch/amd64/label/tcwg-x86_64-build/target/arm-linux-gnueabi/snapshots/gcc.git~linaro-7.2-2017.11/configure' SHELL=/bin/bash --with-mpc=/home/tcwg-buildslave/workspace/tcwg-make-release/builder_arch/amd64/label/tcwg-x86_64-build/target/arm-linux-gnueabi/_build/builds/destdir/x86_64-unknown-linux-gnu --with-mpfr=/home/tcwg-buildslave/workspace/tcwg-make-release/builder_arch/amd64/label/tcwg-x86_64-build/target/arm-linux-gnueabi/_build/builds/destdir/x86_64-unknown-linux-gnu --with-gmp=/home/tcwg-buildslave/workspace/tcwg-make-release/builder_arch/amd64/label/tcwg-x86_64-build/target/arm-linux-gnueabi/_build/builds/destdir/x86_64-unknown-linux-gnu --with-gnu-as --with-gnu-ld --disable-libmudflap --enable-lto --enable-shared --without-included-gettext --enable-nls --disable-sjlj-exceptions --enable-gnu-unique-object --enable-linker-build-id --disable-libstdcxx-pch --enable-c99 --enable-clocale=gnu --enable-libstdcxx-debug --enable-long-long --with-cloog=no --with-ppl=no --with-isl=no --disable-multilib --with-float=soft --with-mode=thumb --with-tune=cortex-a9 --with-arch=armv7-a --enable-threads=posix --enable-multiarch --enable-libstdcxx-time=yes --enable-gnu-indirect-function --with-build-sysroot=/home/tcwg-buildslave/workspace/tcwg-make-release/builder_arch/amd64/label/tcwg-x86_64-build/target/arm-linux-gnueabi/_build/sysroots/arm-linux-gnueabi --with-sysroot=/home/tcwg-buildslave/workspace/tcwg-make-release/builder_arch/amd64/label/tcwg-x86_64-build/target/arm-linux-gnueabi/_build/builds/destdir/x86_64-unknown-linux-gnu/arm-linux-gnueabi/libc --enable-checking=release --disable-bootstrap --enable-languages=c,c++,fortran,lto --build=x86_64-unknown-linux-gnu --host=x86_64-unknown-linux-gnu --target=arm-linux-gnueabi --prefix=/home/tcwg-buildslave/workspace/tcwg-make-release/builder_arch/amd64/label/tcwg-x86_64-build/target/arm-linux-gnueabi/_build/builds/destdir/x86_64-unknown-linux-gnu
    线程模型:posix
    gcc 版本 7.2.1 20171011 (Linaro GCC 7.2-2017.11)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    不想每次否加载可以写在自启动脚本

    vim ~/.bashrc
    
    • 1

    在这里插入图片描述

    (2)编写驱动

    1、编写 helloword驱动 hello_drv.c

    #include 
    
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    /* 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");
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102

    2、编写测试驱动文件 hello_drv_test.c

    #include 
    #include 
    #include 
    #include 
    #include 
    #include 
    
    /*
     * ./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 \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
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51

    3、编写Makefile

    # 1. 使用不同的开发板内核时, 一定要修改KERN_DIR
    # 2. KERN_DIR中的内核要事先配置、编译, 为了能编译内核, 要先设置下列环境变量:
    # 2.1 ARCH,          比如: export ARCH=arm64
    # 2.2 CROSS_COMPILE, 比如: export CROSS_COMPILE=aarch64-linux-gnu-
    # 2.3 PATH,          比如: export PATH=$PATH:/home/book/100ask_roc-rk3399-pc/ToolChain-6.3.1/gcc-linaro-6.3.1-2017.05-x86_64_aarch64-linux-gnu/bin 
    # 注意: 不同的开发板不同的编译器上述3个环境变量不一定相同,
    #       请参考各开发板的高级用户使用手册
    
    KERN_DIR = /disk/buildroot-100ask_t113-pro/buildroot/output/build/linux-d96275805a67d54998123d36e59108cb1ed52ad5
    
    all:
    	make -C $(KERN_DIR) M=`pwd` modules 
    	$(CROSS_COMPILE)gcc -o hello_drv_test hello_drv_test.c 
    
    clean:
    	make -C $(KERN_DIR) M=`pwd` modules clean
    	rm -rf modules.order
    	rm -f hello_drv_test
    
    obj-m	+= hello_drv.o
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    关于 KERN_DIR ,本环境使用的是 buildroot ,通过GitHub把kernel下载下来的,源码在./buildroot-100ask_t113-pro/buildroot/dl/linux/git 下,但是该源码未经过编译,而上一节已经编译过 buildroot 生成镜像img了,那就是已经编译过了,但是指定该目录编译不了
    在这里插入图片描述
    真正的编译目录在
    ./buildroot-100ask_t113-pro/buildroot/output/build/linux-d96275805a67d54998123d36e59108cb1ed52ad5
    (我不确实后面的字母数字是不是每个人一样)
    在这里插入图片描述

    (3)测试

    将生成的 hello_drv.ko、hello_drv_test 放到开发板上
    在这里使用 tftp 下载。(TFTP的搭建参考:链接
    但第一次使用系统没有连接网络(现在已经插入网线)

    # ifconfig
    lo        Link encap:Local Loopback
              inet addr:127.0.0.1  Mask:255.0.0.0
              inet6 addr: ::1/128 Scope:Host
              UP LOOPBACK RUNNING  MTU:65536  Metric:1
              RX packets:6 errors:0 dropped:0 overruns:0 frame:0
              TX packets:6 errors:0 dropped:0 overruns:0 carrier:0
              collisions:0 txqueuelen:1000
              RX bytes:603 (603.0 B)  TX bytes:603 (603.0 B)
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    1、使用udhcpc 自动联网

    # udhcpc
    udhcpc: started, v1.35.0
    [ 3538.458305] libphy: 4500000.eth: probed
    [ 3538.462913] sunxi-gmac 4500000.eth eth0: eth0: Type(7) PHY ID 001cc816 at 0 IRQ poll (4500000.eth-0:00)
    udhcpc: broadcasting discover
    [ 3541.673890] sunxi-gmac 4500000.eth eth0: Link is Up - 100Mbps/Full - flow control off
    [ 3541.682714] IPv6: ADDRCONF(NETDEV_CHANGE): eth0: link becomes ready
    udhcpc: broadcasting discover
    udhcpc: broadcasting select for 192.168.3.45, server 192.168.3.1
    udhcpc: lease of 192.168.3.45 obtained from 192.168.3.1, lease time 604800
    deleting routers
    adding dns 192.168.3.1
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    2、通过 tftp 下载资源

    # tftp -g -r hello_drv.ko 192.168.3.44
    # tftp -g -r hello_drv_test 192.168.3.44
    #
    # ls
    hello_drv.ko    hello_drv_test
    
    • 1
    • 2
    • 3
    • 4
    • 5

    3、测试驱动

    1)加载驱动
    #  insmod hello_drv.ko
    [ 3860.564181] hello_drv: loading out-of-tree module taints kernel.
    [ 3860.571552] /disk/vsCode/01_hello/hello_drv.c hello_init line 702)查看驱动是否加载
    # ls /dev/hello*
    /dev/hello
    # lsmod
    Module                  Size  Used by    Tainted: G
    hello_drv              16384  03)添加权限
    # chmod 777 ./hello_drv_test4)向驱动写入数据
    # ./hello_drv_test  -w abc
    [ 3906.061995] /disk/vsCode/01_hello/hello_drv.c hello_drv_open line 45
    [ 3906.069274] /disk/vsCode/01_hello/hello_drv.c hello_drv_write line 38
    [ 3906.076579] /disk/vsCode/01_hello/hello_drv.c hello_drv_close line 515)读取驱动数据
    # ./hello_drv_test  -r
    [ 3913.268282] /disk/vsCode/01_hello/hello_drv.c hello_drv_open line 45
    [ 3913.275535] /disk/vsCode/01_hello/hello_drv.c hello_drv_read line 30
    APP read : abc
    [ 3913.283024] /disk/vsCode/01_hello/hello_drv.c hello_drv_close line 516)卸载驱动
    # rmmod hello_drv
    [ 4304.419170] /disk/vsCode/01_hello/hello_drv.c hello_exit line 90
    # lsmod
    Module                  Size  Used by    Tainted: G
    sunxi_ce               57344  0
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
  • 相关阅读:
    内网渗透神器CobaltStrike之Beacon详解(三)
    transform + asm资料
    数据结构--第九章--查找
    正则表达式的应用领域及基本语法解析
    【Unity3D】Unity 游戏画面帧更新 ( 游戏物体 GameObject 移动 | 借助 Time.deltaTime 进行匀速运动 )
    SQL快出来,收快递啦(分区,case when 连表)
    dimp V8:[WARNING]login fail, check your username and password, and check the server status
    Vue添加动态路由后,不同角色访问“/”产生404问题
    c语言进阶 数据的存储(上)
    maven项目正在idea的创建和配置
  • 原文地址:https://blog.csdn.net/qq_46079439/article/details/125897482