Add drivers to the current kernel

Continue to the previous article about compiling the kernel

After the kernel is compiled, you need to add your own driver to the kernel. What if you add it?

Choose a directory to add your own driver files

nasri@ubuntu:~/Work/linux-source-3.13.0/$ 

Because the previous compiled kernel was compiled in the above directory

So add your own driver file here and create a directory nasritest under driver/char

nasri@ubuntu:~/Work/linux-source-3.13.0/drivers/char$ 

 Create a driver file under nasritest

nasri@ubuntu:~/Work/linux-source-3.13.0/drivers/char$ vim nasritest/nasritest.c

The nasritest.c driver file is as follows

#include <linux/module.h>
#include <linux/init.h>
#include <linux/mm.h>
static int __init my_init(void)
{
    int i = 0;
    for(i = 0 ; i < 4 ; i++)
        printk("nasritest i = %d\n",i);

    printk("my_init");
    return 0;
}
static void __exit my_exit(void)
{
    printk("nasritest exit!");
}

module_init(my_init);
module_exit(my_exit);

Of course Makefile and Kconfig are indispensable

Create a Mafile file in the nasrittest directory, the content:

obj-$(CONFIG_NASRITEST) += nasritest.o

Kconfig file

menu "NASRI TEST Driver "
comment "NASRI TEST Driver Config"

 config NASRITEST
    tristate "nasritest module test"
    default m
    help
        This is the hello test driver
endmenu

Kconfig is the configuration file used by the kernel for the kernel configuration tool to recognize. With these Kconfig, you can quickly configure various drivers through menuconfig

Just these are not enough

How to connect with other configuration files? Or how to connect with the upper level directory?

Then directly find the upper directory of this directory

We see that there is a folder hw_random similar to nasritest in the upper level directory, we will add our own driver configuration according to its method, you can see

Source the Kconfig we have just now.

config NWFLASH
    tristate "NetWinder flash support"
    depends on ARCH_NETWINDER
    ---help---
      If you say Y here and create a character device /dev/flash with
      major 10 and minor 160 you can manipulate the flash ROM containing
      the NetWinder firmware. Be careful as accidentally overwriting the
      flash contents can render your computer unbootable. On no account
      allow random users access to this device. :-)

      To compile this driver as a module, choose M here: the
      module will be called nwflash.

      If you're not sure, say N.

source "drivers/char/hw_random/Kconfig"
source "drivers/char/nasritest/Kconfig"

Also need to find the folder nasritest when compiling, then add to the Makefile of the char directory

obj-$(CONFIG_MWAVE)     += mwave/
obj-$(CONFIG_AGP)       += agp/
obj-$(CONFIG_NASRITEST)     += nasritest/
obj-$(CONFIG_PCMCIA)        += pcmcia/

That's it right here. After adding it, there are two more functions:

1: We can configure the desired driver through the menuconfig kernel configuration tool

2: It can be compiled to our driver file through the make command

Let’s first look at menuconfig

nasri@ubuntu:~/Work/linux-source-3.13.0$ make menuconfig
scripts/kconfig/mconf Kconfig


*** End of the configuration.
*** Execute 'make' to start the build or try 'make help'.

 

Search on the menuconfig interface ("press slash / search nasri, you can see the configuration path of our driver")

┌──────────────────────────────────────────────────────────── Search Results ────────────────────────────────────────────────────────────┐
  │ Symbol: NASRITEST [=m]                                                                                                                 │  
  │ Type  : tristate                                                                                                                       │  
  │ Prompt: nasritest module test                                                                                                          │  
  │   Location:                                                                                                                            │  
  │     -> Device Drivers                                                                                                                  │  
  │       -> Character devices                                                                                                             │  
  │ (1)     -> NASRI TEST Driver                                                                                                           │  
  │   Defined at drivers/char/nasritest/Kconfig:4                                                                                          │  

Find below this path, you can see that we have selected this module

 .config - Linux/x86 3.13.11-ckt39 Kernel Configuration
 > Device Drivers > Character devices ────────────────────────────────────────────────────────────────────────────────────────────────────────
  ┌────────────────────────────────────────────────────────── Character devices ───────────────────────────────────────────────────────────┐
  │  Arrow keys navigate the menu.  <Enter> selects submenus ---> (or empty submenus ----).  Highlighted letters are hotkeys.  Pressing    │  
  │  <Y> includes, <N> excludes, <M> modularizes features.  Press <Esc><Esc> to exit, <?> for Help, </> for Search.  Legend: [*] built-in  │  
  │  [ ] excluded  <M> module  < > module capable                                                                                          │  
  │                                                                                                                                        │  
  │ ┌───────────────────────────────^(-)─────────────────────────────────────────────────────────────────────────────────────────────────┐ │  
  │ │                               <M>   HDLC line discipline support                                                                   │ │  
  │ │                               <M>   GSM MUX line discipline support (EXPERIMENTAL)                                                 │ │  
  │ │                               <M>   Trace data router for MIPI P1149.7 cJTAG standard                                              │ │  
  │ │                               <M>   Trace data sink for MIPI P1149.7 cJTAG standard                                                │ │  
  │ │                               [ ] /dev/kmem virtual device support                                                                 │ │  
  │ │                                   Serial drivers  --->                                                                             │ │  
  │ │                               [*] TTY driver to output user messages via printk                                                    │ │  
  │ │                               <M> Parallel printer support                                                                         │ │  
  │ │                               [ ]   Support for console on line printer                                                            │ │  
  │ │                               <M> Support for user-space parallel port device drivers                                              │ │  
  │ │                               [*] Xen Hypervisor Console support                                                                   │ │  
  │ │                               [*]   Xen Hypervisor Multiple Consoles support                                                       │ │  
  │ │                               <*> Virtio console                                                                                   │ │  
  │ │                               <M> IPMI top-level message handler  --->                                                             │ │  
  │ │                               <*> Hardware Random Number Generator Core support                                                    │ │  
  │ │                               <M>   Timer IOMEM HW Random Number Generator support                                                 │ │  
  │ │                               <M>   Intel HW Random Number Generator support                                                       │ │  
  │ │                               <M>   AMD HW Random Number Generator support                                                         │ │  
  │ │                               < >   Atmel Random Number Generator support                                                          │ │  
  │ │                               <M>   VIA HW Random Number Generator support                                                         │ │  
  │ │                               <M>   VirtIO Random Number Generator support                                                         │ │  
  │ │                               < > EXYNOS HW random number generator support                                                        │ │  
  │ │                               <M> TPM HW Random Number Generator support                                                           │ │  
  │ │                                   NASRI TEST Driver   --->                                                                         │ │  
  │ │                               {M} /dev/nvram support                                                                               │ │  
  │ │                               <M> Siemens R3964 line discipline                                                                    │ │  
  │ │                               <M> Applicom intelligent fieldbus card support                                                       │ │  
  │ └───────────────────────────────┴(+)─────────────────────────────────────────────────────────────────────────────────────────────────┘ │  
  ├────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┤  
  │                                        <Select>    < Exit >    < Help >    < Save >    < Load >                                        │  
  └────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┘  
    

I saw NASRI TEST Driver, and I saw it. This is the string in the menu in the Kconfig under our nasritest directory. Go in and see:

 

.config - Linux/x86 3.13.11-ckt39 Kernel Configuration
 > Device Drivers > Character devices > NASRI TEST Driver  ───────────────────────────────────────────────────────────────────────────────────
  ┌────────────────────────────────────────────────────────── NASRI TEST Driver  ──────────────────────────────────────────────────────────┐
  │  Arrow keys navigate the menu.  <Enter> selects submenus ---> (or empty submenus ----).  Highlighted letters are hotkeys.  Pressing    │  
  │  <Y> includes, <N> excludes, <M> modularizes features.  Press <Esc><Esc> to exit, <?> for Help, </> for Search.  Legend: [*] built-in  │  
  │  [ ] excluded  <M> module  < > module capable                                                                                          │  
  │                                                                                                                                        │  
  │ ┌────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────┐ │  
  │ │                                   *** NASRI TEST Driver Config ***                                                                 │ │  
  │ │                               <M> nasritest module test                                                                            │ │  
  │ │                                                                       

Has been selected as M

Here is the meaning of selecting Y and M in the table below

  y: The module driver is compiled into the kernel and automatically loaded at startup

   m: The module will be compiled, but will not be compiled into the kernel

Okay, save the menuconfig, just compile and have a look

Excuting an order

nasri@ubuntu:~/Work/linux-source-3.13.0$ make drivers
make[1]: Nothing to be done for `all'.
make[1]: Nothing to be done for `relocs'.
  CHK     include/config/kernel.release
/bin/sh: 1: cannot create include/config/kernel.release.tmp: Permission denied
make: *** [include/config/kernel.release] Error 2
nasri@ubuntu:~/Work/linux-source-3.13.0$ 

An error was reported, Permission denied, and I remembered that the sudo command used when compiling the kernel was also used here.

nasri@ubuntu:~/Work/linux-source-3.13.0$ sudo make drivers

Make drivers is to compile the contents of the drivers directory

nasri@ubuntu:~/Work/linux-source-3.13.0$ sudo make drivers
[sudo] password for nasri: 
make[1]: Nothing to be done for `all'.
make[1]: Nothing to be done for `relocs'.
  CHK     include/config/kernel.release
  CHK     include/generated/uapi/linux/version.h
  CHK     include/generated/utsrelease.h
  CALL    scripts/checksyscalls.sh

Just compiled it like this? It doesn't seem to be compiled to our driver module?

Come to think of it, I compiled it before, and now modify the driver file, the compilation tool sees that there is no change, so we did not compile our code again

Um, it doesn't seem right, this one should use make modules

nasri@ubuntu:~/Work/linux-source-3.13.0$ sudo make modules
.......
  LD [M]  arch/x86/crypto/twofish-x86_64-3way.ko
  LD [M]  arch/x86/crypto/twofish-x86_64.ko
  CC      drivers/char/nasritest/nasritest.mod.o
  LD [M]  drivers/char/nasritest/nasritest.ko

Finally, the .ko file is generated

With the ko file, we can "inject" into the kernel through insmode

try it!

Remember to use sudo 

nasri@ubuntu:~/Work/linux-source-3.13.0$ insmod drivers/char/nasritest/nasritest.ko
insmod: ERROR: could not insert module drivers/char/nasritest/nasritest.ko: Operation not permitted
nasri@ubuntu:~/Work/linux-source-3.13.0$ sudo insmod drivers/char/nasritest/nasritest.ko
nasri@ubuntu:~/Work/linux-source-3.13.0$ 

After insmode, we found nothing. After we did insmod, the ko driver was registered in the kernel.

We have to look at the kernel log to know if the ko is running:

View kernel log

nasri@ubuntu:~/Work/linux-source-3.13.0$ vim /var/log/syslog

Did you see the last nasritest?

Nov  6 07:22:35 ubuntu NetworkManager[750]: <info>   wins '192.168.230.2'
Nov  6 07:22:35 ubuntu dbus[413]: [system] Activating service name='org.freedesktop.nm_dispatcher' (using servicehelper)
Nov  6 07:22:35 ubuntu dbus[413]: [system] Successfully activated service 'org.freedesktop.nm_dispatcher'
Nov  6 07:30:01 ubuntu CRON[23035]: (root) CMD (start -q anacron || :)
Nov  6 07:30:01 ubuntu anacron[23038]: Anacron 2.3 started on 2019-11-06
Nov  6 07:30:01 ubuntu anacron[23038]: Normal exit (0 jobs run)
Nov  6 07:31:39 ubuntu kernel: [ 2832.390445] nasritest: module license 'unspecified' taints kernel.
Nov  6 07:31:39 ubuntu kernel: [ 2832.390902] Disabling lock debugging due to kernel taint
Nov  6 07:31:39 ubuntu kernel: [ 2832.393993] nasritest: module verification failed: signature and/or  required key missing - tainting kernel
Nov  6 07:31:39 ubuntu kernel: [ 2832.400330] nasritest i = 0
Nov  6 07:31:39 ubuntu kernel: [ 2832.400333] nasritest i = 1
Nov  6 07:31:39 ubuntu kernel: [ 2832.400334] nasritest i = 2
Nov  6 07:31:39 ubuntu kernel: [ 2832.400335] nasritest i = 3

But I see this error in the unspecified line

When module license'unspecified' taints kernel appears when driving, add MODULE_LICENSE("GPL"); this error will not appear.

OK, after adding this sentence, compile and see

Also make modules?

You can just compile this module

nasri@ubuntu:~/Work/linux-source-3.13.0$ sudo make modules SUBDIRS=drivers/char/nasritest/ 
  CC [M]  drivers/char/nasritest/nasritest.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      drivers/char/nasritest/nasritest.mod.o
  LD [M]  drivers/char/nasritest/nasritest.ko
nasri@ubuntu:~/Work/linux-source-3.13.0$ 

Do you want to hit me impulsively? Isn't it enough to use this command directly? Haha

Well, try insmod again

nasri@ubuntu:~/Work/linux-source-3.13.0$ sudo insmod drivers/char/nasritest/nasritest.ko
insmod: ERROR: could not insert module drivers/char/nasritest/nasritest.ko: File exists

Um, you need to remove this driver first, and then add

nasri@ubuntu:~/Work/linux-source-3.13.0$ sudo rmmod drivers/char/nasritest/nasritest.ko

After adding with sudo insmod again, check syslog

nasri@ubuntu:~/Work2/nasritest$ vim /var/log/syslog
nasri@ubuntu:~/Work2/nasritest$ 
Nov  6 07:30:01 ubuntu anacron[23038]: Normal exit (0 jobs run)
Nov  6 07:31:39 ubuntu kernel: [ 2832.390445] nasritest: module license 'unspecified' taints kernel.
Nov  6 07:31:39 ubuntu kernel: [ 2832.390902] Disabling lock debugging due to kernel taint
Nov  6 07:31:39 ubuntu kernel: [ 2832.393993] nasritest: module verification failed: signature and/or  required key missing - tainting kernel
Nov  6 07:31:39 ubuntu kernel: [ 2832.400330] nasritest i = 0
Nov  6 07:31:39 ubuntu kernel: [ 2832.400333] nasritest i = 1
Nov  6 07:31:39 ubuntu kernel: [ 2832.400334] nasritest i = 2
Nov  6 07:31:39 ubuntu kernel: [ 2832.400335] nasritest i = 3
Nov  6 07:36:33 ubuntu dhclient: DHCPREQUEST of 192.168.230.132 on eth0 to 192.168.230.254 port 67 (xid=0x33172383)
Nov  6 07:36:33 ubuntu dhclient: DHCPACK of 192.168.230.132 from 192.168.230.254
Nov  6 07:36:33 ubuntu dhclient: bound to 192.168.230.132 -- renewal in 865 seconds.
Nov  6 07:36:33 ubuntu NetworkManager[750]: <info> (eth0): DHCPv4 state changed renew -> renew
Nov  6 07:36:33 ubuntu NetworkManager[750]: <info>   address 192.168.230.132
Nov  6 07:36:33 ubuntu NetworkManager[750]: <info>   prefix 24 (255.255.255.0)
Nov  6 07:36:33 ubuntu NetworkManager[750]: <info>   gateway 192.168.230.2
Nov  6 07:36:33 ubuntu NetworkManager[750]: <info>   nameserver '192.168.230.2'
Nov  6 07:36:34 ubuntu NetworkManager[750]: <info>   domain name 'localdomain'
Nov  6 07:36:34 ubuntu NetworkManager[750]: <info>   wins '192.168.230.2'
Nov  6 07:36:34 ubuntu dbus[413]: [system] Activating service name='org.freedesktop.nm_dispatcher' (using servicehelper)
Nov  6 07:36:34 ubuntu dbus[413]: [system] Successfully activated service 'org.freedesktop.nm_dispatcher'
Nov  6 07:43:45 ubuntu kernel: [ 3557.621564] nasritest i = 0
Nov  6 07:43:45 ubuntu kernel: [ 3557.621567] nasritest i = 1
Nov  6 07:43:45 ubuntu kernel: [ 3557.621568] nasritest i = 2
Nov  6 07:43:45 ubuntu kernel: [ 3557.621569] nasritest i = 3
/nasri                                                                                                                       35,47         Bot

See that there is no last nasritest: module license'unspecified' taints kernel.

Now we know how to inject drivers into it.

 

In addition, when it comes to compiling modules, there can be a better way, or a simpler way for us to choose

For example, if I want to write a driver, I create an ordinary directory, which may not be in the kernel source file directory.

nasri@ubuntu:~$ mkdir Work2
nasri@ubuntu:~$ cd Work2/

Create a Work2 directory, and then copy the previous nasritest directory, because there are other generated files.o.ko in addition to Makefile, nasritest.c, and Kconfig. We only keep Makefile and nasritest.c, Delete everything else

Makefile written like this

obj-m += nasritest.o

m means we are a module

Then directly compile, pay attention to the compiling command:

nasri@ubuntu:~/Work2/nasritest$ make -C ../../Work/linux-source-3.13.0/ SUBDIRS=~/Work2/nasritest/ modules
make: Entering directory `/home/nasri/Work/linux-source-3.13.0'
  CC [M]  /home/nasri/Work2/nasritest/nasritest.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /home/nasri/Work2/nasritest/nasritest.mod.o
  LD [M]  /home/nasri/Work2/nasritest/nasritest.ko
make: Leaving directory `/home/nasri/Work/linux-source-3.13.0'
nasri@ubuntu:~/Work2/nasritest$ 

Make -C is followed by a directory, the directory is our kernel directory, the directory of the compiled kernel source files

Of course, directories like this still exist in the lib directory of the root directory in our entire system

nasri@ubuntu:~/Work2/nasritest$ ls /lib/modules/3.13.11-ckt39/build/
arch     crypto         firmware  ipc      lib          modules.builtin  notes.txt       scripts           sound       usr        x509.genkey
block    Documentation  fs        Kbuild   MAINTAINERS  modules.order    README          security          System.map  virt
COPYING  drivers        include   Kconfig  Makefile     Module.symvers   REPORTING-BUGS  signing_key.priv  tools       vmlinux
CREDITS  dropped.txt    init      kernel   mm           net              samples         signing_key.x509  ubuntu      vmlinux.o
nasri@ubuntu:~/Work2/nasritest$ 

So someone wrote the compilation command as: make -C /lib/modules/`uname -r`/build SUBDIRS=~/Work2/nasritest/ modules

nasri@ubuntu:~/Work2/nasritest$ make -C /lib/modules/`uname -r`/build SUBDIRS=~/Work2/nasritest/ modules
make: Entering directory `/home/nasri/Work/linux-source-3.13.0'
  CC [M]  /home/nasri/Work2/nasritest/nasritest.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /home/nasri/Work2/nasritest/nasritest.mod.o
  LD [M]  /home/nasri/Work2/nasritest/nasritest.ko
make: Leaving directory `/home/nasri/Work/linux-source-3.13.0'
nasri@ubuntu:~/Work2/nasritest$ 

Or write directly as make -C /lib/modules/`uname -r`/build SUBDIRS=$PWD modules

nasri@ubuntu:~/Work2/nasritest$ make -C /lib/modules/`uname -r`/build SUBDIRS=$PWD modules
make: Entering directory `/home/nasri/Work/linux-source-3.13.0'
  Building modules, stage 2.
  MODPOST 1 modules
make: Leaving directory `/home/nasri/Work/linux-source-3.13.0'
nasri@ubuntu:~/Work2/nasritest$ 

Note that this is $PWD, the uppercase PWD is an environment variable, which means the current directory

And the lowercase pwd is the command,

Here you can see the difference

nasri@ubuntu:~/Work2/nasritest$ echo $(pwd)
/home/nasri/Work2/nasritest
nasri@ubuntu:~/Work2/nasritest$ $PWD
bash: /home/nasri/Work2/nasritest: Is a directory
nasri@ubuntu:~/Work2/nasritest$ echo $PWD
/home/nasri/Work2/nasritest
nasri@ubuntu:~/Work2/nasritest$ echo $(PWD)
PWD: command not found

nasri@ubuntu:~/Work2/nasritest$ echo $pwd

nasri@ubuntu:~/Work2/nasritest$ 

well, let's get back to business

So the make command can be written like this

nasri@ubuntu:~/Work2/nasritest$ make -C /lib/modules/`uname -r`/build SUBDIRS=$(pwd) modules
make: Entering directory `/home/nasri/Work/linux-source-3.13.0'
  Building modules, stage 2.
  MODPOST 1 modules
make: Leaving directory `/home/nasri/Work/linux-source-3.13.0'
nasri@ubuntu:~/Work2/nasritest$ 

Considering the convenient period, you can modify the Mafile to make the compilation more convenient

Makefile:

obj-m := nasritest.o
KERNELDIR ?=/lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
MAKE := make
modules:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules
modules_install:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules_install

Okay, compile and have a look

nasri@ubuntu:~/Work2/nasritest$ make
make: Nothing to be done for `modules'.

Nothing to be done for 'modules'

At first I thought that the module had been compiled before, and it would happen if it was compiled again without changes.

Then I changed the code and compiled it again and found that there was still an error.

Finally read an article online:

http://blog.chinaunix.net/uid-30184083-id-5028504.html

I saw the warning color in red in my Makefile

View with od -c Makefile

nasri@ubuntu:~/Work2/nasritest$ od -c Makefile 
0000000   o   b   j   -   m       :   =       n   a   s   r   i   t   e
0000020   s   t   .   o  \n   K   E   R   N   E   L   D   I   R       ?
0000040   =   /   l   i   b   /   m   o   d   u   l   e   s   /   $   (
0000060   s   h   e   l   l       u   n   a   m   e       -   r   )   /
0000100   b   u   i   l   d  \n   P   W   D       :   =       $   (   s
0000120   h   e   l   l       p   w   d   )  \n   M   A   K   E       :
0000140   =       m   a   k   e  \n   m   o   d   u   l   e   s   :  \n
0000160                   $   (   M   A   K   E   )       -   C       $
0000200   (   K   E   R   N   E   L   D   I   R   )       M   =   $   (
0000220   P   W   D   )       m   o   d   u   l   e   s  \n   m   o   d
0000240   u   l   e   s   _   i   n   s   t   a   l   l   :  \n        
0000260           $   (   M   A   K   E   )       -   C       $   (   K
0000300   E   R   N   E   L   D   I   R   )       M   =   $   (   P   W
0000320   D   )       m   o   d   u   l   e   s   _   i   n   s   t   a
0000340   l   l  \n
0000343

Found that it is not \t before $(MAKE)

Contrast what the Internet said:

Comment out set expandtab in vimrc, then modify the tab before make in Makefile

nasri@ubuntu:~/Work2/nasritest$ vim /etc/vim/vimrc
nasri@ubuntu:~/Work2/nasritest$ 
nasri@ubuntu:~/Work2/nasritest$ 
" Source a global configuration file if available
set ts=4
set softtabstop=4
set shiftwidth=4
set expandtab
set autoindent
if filereadable("/etc/vim/vimrc.local")
  source /etc/vim/vimrc.local
endif

Comment out set expandtab

Then modify the Makefile to write 4 spaces before $(MAKE)

nasri@ubuntu:~/Work2/nasritest$ od -c Makefile 
0000000   o   b   j   -   m       :   =       n   a   s   r   i   t   e
0000020   s   t   .   o  \n   K   E   R   N   E   L   D   I   R       ?
0000040   =   /   l   i   b   /   m   o   d   u   l   e   s   /   $   (
0000060   s   h   e   l   l       u   n   a   m   e       -   r   )   /
0000100   b   u   i   l   d  \n   P   W   D       :   =       $   (   s
0000120   h   e   l   l       p   w   d   )  \n   M   A   K   E       :
0000140   =       m   a   k   e  \n   m   o   d   u   l   e   s   :  \n
0000160  \t   $   (   M   A   K   E   )       -   C       $   (   K   E
0000200   R   N   E   L   D   I   R   )       M   =   $   (   P   W   D
0000220   )       m   o   d   u   l   e   s  \n   m   o   d   u   l   e
0000240   s   _   i   n   s   t   a   l   l   :  \n  \t   $   (   M   A
0000260   K   E   )       -   C       $   (   K   E   R   N   E   L   D
0000300   I   R   )       M   =   $   (   P   W   D   )       m   o   d
0000320   u   l   e   s   _   i   n   s   t   a   l   l  \n
0000335
nasri@ubuntu:~/Work2/nasritest$ 

But I don’t know why it becomes \t, so try it too:

nasri@ubuntu:~/Work2/nasritest$ make
make -C /lib/modules/3.13.11-ckt39/build M=/home/nasri/Work2/nasritest modules
make[1]: Entering directory `/home/nasri/Work/linux-source-3.13.0'
  Building modules, stage 2.
  MODPOST 1 modules
make[1]: Leaving directory `/home/nasri/Work/linux-source-3.13.0'
nasri@ubuntu:~/Work2/nasritest$ od -C Makefile 
od: invalid option -- 'C'
Try 'od --help' for more information.

Sure enough.

Because it has been compiled before, I did not see if it was compiled into .ko, modify nasritest.c

Compile and see:

nasri@ubuntu:~/Work2/nasritest$ make
make -C /lib/modules/3.13.11-ckt39/build M=/home/nasri/Work2/nasritest modules
make[1]: Entering directory `/home/nasri/Work/linux-source-3.13.0'
  CC [M]  /home/nasri/Work2/nasritest/nasritest.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /home/nasri/Work2/nasritest/nasritest.mod.o
  LD [M]  /home/nasri/Work2/nasritest/nasritest.ko
make[1]: Leaving directory `/home/nasri/Work/linux-source-3.13.0'

Okay, yes, .ko is generated.

 

Then import this ko into the kernel to see

Use the insmod command

nasri@ubuntu:~/Work2/nasritest$ sudo insmod nasritest.ko 

 

Nov  7 08:44:41 ubuntu dbus[413]: [system] Successfully activated service 'org.freedesktop.nm_dispatcher'
Nov  7 08:46:09 ubuntu kernel: [ 5385.250995] nasritest: module verification failed: signature and/or  required key missing - tainting kernel
Nov  7 08:46:09 ubuntu kernel: [ 5385.273230] nasritest i = 0
Nov  7 08:46:09 ubuntu kernel: [ 5385.273234] nasritest i = 1
Nov  7 08:46:09 ubuntu kernel: [ 5385.273235] nasritest i = 2
Nov  7 08:46:09 ubuntu kernel: [ 5385.273236] nasritest i = 3
Nov  7 08:46:09 ubuntu kernel: [ 5385.273236] nasritest i = 4
Nov  7 08:46:09 ubuntu kernel: [ 5385.273237] nasritest i = 5
Nov  7 08:46:09 ubuntu kernel: [ 5385.273238] nasritest i = 6
Nov  7 08:46:09 ubuntu kernel: [ 5385.273239] nasritest i = 7
Nov  7 08:46:09 ubuntu kernel: [ 5385.273239] nasritest i = 8
Nov  7 08:46:09 ubuntu kernel: [ 5385.273240] nasritest i = 9
Nov  7 08:46:09 ubuntu kernel: [ 5385.273241] nasritest i = 10
Nov  7 08:46:09 ubuntu kernel: [ 5385.273242] nasritest i = 11
Nov  7 08:46:09 ubuntu kernel: [ 5385.273243] nasritest i = 12
Nov  7 08:46:09 ubuntu kernel: [ 5385.273244] nasritest i = 13
Nov  7 08:46:09 ubuntu kernel: [ 5385.273244] nasritest i = 14
Nov  7 08:46:09 ubuntu kernel: [ 5385.273245] nasritest i = 15
Nov  7 08:46:09 ubuntu kernel: [ 5385.273246] nasritest i = 16
Nov  7 08:46:09 ubuntu kernel: [ 5385.273247] nasritest i = 17
Nov  7 08:46:09 ubuntu kernel: [ 5385.273247] nasritest i = 18
Nov  7 08:46:09 ubuntu kernel: [ 5385.273248] nasritest i = 19
                                                                  

At this point, the first link of writing drive is over. The following really enters the example in the book "Run Linux Kernel"

Guess you like

Origin blog.csdn.net/yangkunhenry/article/details/102945019