The kernel driver is compiled under Ubuntu.

Source: Internet
Author: User
Tags syslog

Objective:

1. Drive warm-up. There are many similar articles on the Internet for reference.

2. In the operating system, write the driver for this device.

3. Drive practiced hand for watchdog to write Qemu.

A friend asked make's watchdog driver what to prepare, so write this blog.

Environment:

Ubuntu 12.04.4

Warm:

First, write a simple Hello World driver.

Time-consuming (based on familiarity with the Linux environment and operating system conditions):

1. Install the operating system for 30-60 minutes.

2. Configure the operating system for 30 minutes. The main configuration is Chinese calligraphy. No other special configuration.

3. Code writing runs for 40 minutes.

To view your own kernel version:Search for kernel version (optional) (search kernel versions)

Type the following command:

$ Apt-cache Search linux-headers-$ (uname-r)

Install Linux-header Package under Debina or Ubuntu Linux (version of update kernel)

Type the following command:

$ sudo apt-get install linux-headers-$ (uname-r)

Code: //hello.c driver:#include <linux/init.h>//initial swap function #include<linux/kernel.h>//kernel header file #include<linux/module.h>//module header file Module_license ("Shaohef BSD/GPL");static int __init hello_start (void){PRINTK (kern_alert "Loading Hello module...\n");PRINTK (kern_alert "Hello world\n");return 0;}static void __exit hello_end (void){PRINTK (kern_alert "Goodbye mr.\n");}Module_init (hello_start);Module_exit (hello_end);

///Makefile File:Ifeq ($ (kernelrelease),) kversion = $ (Shell uname-r) all:make-c/lib/modules/$ (kversion)/build m=$ (shell pwd) modulescle An:make-c/lib/modules/$ (kversion)/build m=$ (shell pwd) cleanmodules_install
$ (make)-C $ (Kerneldir) m=$ (PWD) modules_installelse obj-m: =hello.oendif Install driver (may require reboot):

Compile

$ make

Mount Drive

$ sudo insmod Hello.ko

Uninstalling the Drive

$ sudo rmmod hello

View drivers , preferably with piping |more instructions or |grep

Lsmod |grep Hello

 System-initiated Load module

File/etc/modules set the loaded kernel, this file contains the module to be loaded when the system starts, one line per module.

First the Hello module is copied to the/lib/modules/$ (uname-r)/kernel/drivers.

Recommended steps:

(a) Create a directory for the Hello module

$ sudo mkdir-p/lib/modules/$ (uname-r)/kernel/drivers/hello

(b) Copy module

$ sudo cp hello.ko/lib/modules/$ (uname-r)/kernel/drivers/hello/

(c) Edit the/etc/modules file

$ sudo vim/etc/modules

Add the following line

Hello

(d) Reboot to see if it is loaded.

$ sudo cat/proc/modules

Or

$ sudo lsmod | Less

to view the output of the program:#cat/var/log/syslog |grep World

If the syslog has no output, view the information inside the/var/log/message

# tail-f/var/log/message

Note:

The essence of Insmod is the process of linking KO files with the running kernel. A link procedure similar to compiling HelloWorld.

A link must be compiled first to determine if the required external symbol (EXPORT_SYMBOLS) exists, because some symbols (functions or global variables) are in the kernel. If you use these symbols in the drive, you must reserve a location to further determine the exact location of the symbols (symbolic bindings) when insmod.

If the kernel has not been compiled, how to know if these symbols are programmed into the kernel

The kernel driver is compiled under Ubuntu.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.