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\n");
return 0;
}

static void hello_exit(void)
{
printk(KERN_ALERT "Goodbye, solidmango\n");
}

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 / 15.4 . 18 / build / scripts / sign-sha512 file / lib / modules / 15.4 . 18 /build/certs/signing_key.pem / lib / modules / 15.4 . 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

 

Guess you like

Origin www.cnblogs.com/pugang/p/11592441.html