Presentation is loading. Please wait.

Presentation is loading. Please wait.

Linux Kernel Driver.

Similar presentations


Presentation on theme: "Linux Kernel Driver."— Presentation transcript:

1 Linux Kernel Driver

2 Char Driver: Basics Contact details exported to user space using /dev:
bash$ ls -l /dev total 0 brw-rw root disk 8, :01 /dev/sda brw-rw root disk 8, :01 /dev/sda1 brw-rw root disk 8, :01 /dev/sda2 ... crw--w root root 4, :01 /dev/tty0 crw root root 4, :01 /dev/tty1 crw root root 4, :01 /dev/tty2 Block device Char device Major number Minor number

3 Char Driver: Codeflow Initialization
Tie driver to the rest of the kernel I/O system entry points Read, write, open, seek, close (Interrupt routines) Cleanup

4 Char Driver: Initialization
#define DEVICE_NAME “rot13crypt” struct cdev crypt_cdev; static dev_t crypt_dev; static struct file_operations crypt_fops = { ... }; static int __init crypt_init(void) { int result; result = alloc_chrdev_region(&crypt_dev, 0, 1, DEVICE_NAME); /* dynamic allocation van device major number */ /* connect file operations with character dev */ cdev_init(&crypt_cdev, &crypt_fops); result = cdev_add(&crypt_cdev, crypt_dev, 1); return 0; } module_init(crypt_init) Kernel abstraction for character drivers Addresses of IO system entry points

5 Char Driver: Cleanup static void __exit crypt_cleanup(void) {
/* Remove the cdev */ cdev_del(&crypt_cdev); /* Release the major number */ unregister_chrdev_region(MAJOR(crypt_dev), 1); } module_exit(crypt_cleanup)

6 Char Driver: Entry points
static struct file_operations crypt_fops = { .owner = THIS_MODULE, .read = crypt_read, .write = crypt_write, .open = crypt_open, .release = crypt_close, }; /* Called when application opens device node */ int crypt_open(struct inode *inode, struct file *file) { return 0; } /* Called when application closes device node */ int crypt_release(struct inode *inode, struct file *file) {

7 Char Device: read/write
char *kbuf = NULL; static ssize_t crypt_write(struct file * file, const char * buf, size_t count, loff_t *ppos) { kbuf = kmalloc(count, GFP_KERNEL); if( buf == NULL ) return -ENOMEM; /* Copy the characters to kernel memory */ if( copy_from_user(kbuf, buf, count) ) { kfree(kbuf); return -EFAULT; } return nbyteswritten; static ssize_t crypt_read(struct file * file, char * buf, size_t count, loff_t *ppos) { if( copy_to_user(buf, kbuf, count) ) { return -EIO; return nbytesread;

8 Kernel Memory kmalloc, kfree Flag specifies the type of memory
copy_[from|to]_user Copy data between user and kernel memory Between different virtual memory mappings

9 Practical Driver prints major number (see dmesg)
Or 'cat /proc/devices | grep rot13crypt' minor = 0 mknod /dev/rot13crypt c major minor echo “string” > /dev/rot13crypt Calls open, write and close cat /dev/rot13crypt Calls open, read, close

10 Implementation Implement rot13 “encryption”
The transformation done using a lookup table: ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz NOPQRSTUVWXYZABCDEFGHIJKLMnopqrstuvwxyzabcdefghijklm

11 Build Makefile bash$ make obj-m := rot13crypt.o
kernel_release=`uname -r` all: make -C /lib/modules/${kernel_release}/build/ SUBDIRS=${PWD} modules


Download ppt "Linux Kernel Driver."

Similar presentations


Ads by Google