zoukankan      html  css  js  c++  java
  • how to write your first linux device driver

    how to write your first linux device driver

    0. environment
    -ubuntu 1804 64bit

    1. apt-get install linux-headers-$(uname -r)

    2. code hello.c

    #include <linux/init.h>
    #include <linux/module.h>
    MODULE_LICENSE("Dual BSD/GPL");
    
    static int hello_init(void)
    {
    printk(KERN_ALERT "Hello, solidmango
    ");
    return 0;
    }
    
    static void hello_exit(void)
    {
    printk(KERN_ALERT "Goodbye, solidmango
    ");
    }
    
    module_init(hello_init);
    module_exit(hello_exit);

    3. Makefile

    # If KERNELRELEASE is defined, we've been invoked from the
    # kernel build system and can use its language.
    ifneq ($(KERNELRELEASE),)
    obj-m := hello.o
    # Otherwise we were called directly from the command
    # line; invoke the kernel build system.
    else
    KERNELDIR ?= /lib/modules/$(shell uname -r)/build
    PWD := $(shell pwd)
    default:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
    endif

    4. sudo insmod hello.ko

    5. sudo rmmod hello

    6. view result

    sm@ubuntu:~/dev$ cat /var/log/syslog|grep solidmango
    Sep 26 00:30:04 ubuntu kernel: [ 3041.143749] Hello, solidmango
    Sep 26 00:30:31 ubuntu kernel: [ 3068.192172] Goodbye, solidmango

    7. sign your kernel module

    /lib/modules/4.15.18/build/scripts/sign-file sha512 /lib/modules/4.15.18/build/certs/signing_key.pem /lib/modules/4.15.18/build/certs/signing_key.x509 hello.ko

    signing_key.pem and signing_key.x509 files are generated when build the linux kernel

    reference
    https://wiki.gentoo.org/wiki/Signed_kernel_module_support#Enabling_module_signature_verification

  • 相关阅读:
    关于医学的一点想法
    我的ArcGis9.3 到Arcgis10.0 升级步骤
    最近一月的娱乐生活:看电影,玩游戏
    最近一月的娱乐生活:看电影,玩游戏
    5年技术学习历程的回顾
    5年技术学习历程的回顾
    网站开发的技术选型问题
    网站开发的技术选型问题
    学技术真累
    Java实现 LeetCode 200 岛屿数量
  • 原文地址:https://www.cnblogs.com/pugang/p/11592441.html
Copyright © 2011-2022 走看看