Linux 驅動開發環境構建

1、查看當前系統內核代碼版本

caojiaju@caojiaju-iMac:~$ sudo apt-cache search linux-source
[sudo] password for caojiaju: 
linux-source - Linux kernel source with Ubuntu patches
linux-source-4.15.0 - Linux kernel source for version 4.15.0 with Ubuntu patches
 

2、下載源代碼

caojiaju@caojiaju-iMac:~$ sudo apt-get install linux-source-4.15.0
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following package was automatically installed and is no longer required:
  libssl-doc
Use 'sudo apt autoremove' to remove it.
Suggested packages:
  libqt3-dev
The following NEW packages will be installed:
  linux-source-4.15.0
0 upgraded, 1 newly installed, 0 to remove and 8 not upgraded.
Need to get 129 MB of archives.
After this operation, 146 MB of additional disk space will be used.
Get:1 http://cn.archive.ubuntu.com/ubuntu bionic-updates/main amd64 linux-source-4.15.0 all 4.15.0-42.45 [129 MB]
Fetched 129 MB in 2min 49s (763 kB/s)                                                                                  
Selecting previously unselected package linux-source-4.15.0.
(Reading database ... 259947 files and directories currently installed.)
Preparing to unpack .../linux-source-4.15.0_4.15.0-42.45_all.deb ...
Unpacking linux-source-4.15.0 (4.15.0-42.45) ...
Setting up linux-source-4.15.0 (4.15.0-42.45) ...
3、查看下載的源代碼文件

caojiaju@caojiaju-iMac:/usr/src$ ls
linux-headers-4.15.0-39          linux-headers-4.15.0-42          linux-source-4.15.0          vboxhost-6.0.0
linux-headers-4.15.0-39-generic  linux-headers-4.15.0-42-generic  linux-source-4.15.0.tar.bz2
4、創建源碼目錄

caojiaju@caojiaju-iMac:/usr/src$ sudo mkdir /home/kernel/KernelSource -p

5、解壓源碼包

caojiaju@caojiaju-iMac:/usr/src$ sudo tar jxvf linux-source-4.15.0.tar.bz2 -C /home/kernel/KernelSource/

6、配置內核

caojiaju@caojiaju-iMac:/usr/src$ cd /home/kernel/KernelSource/
caojiaju@caojiaju-iMac:/home/kernel/KernelSource$ cd linux-source-4.15.0/
caojiaju@caojiaju-iMac:/home/kernel/KernelSource/linux-source-4.15.0$ ls
arch   COPYING  Documentation  firmware  init    Kconfig  MAINTAINERS  net      scripts         sound  ubuntu  zfs
block  CREDITS  drivers        fs        ipc     kernel   Makefile     README   security        spl    usr
certs  crypto   dropped.txt    include   Kbuild  lib      mm           samples  snapcraft.yaml  tools  virt
caojiaju@caojiaju-iMac:/home/kernel/KernelSource/linux-source-4.15.0$ sudo make oldconfig
  HOSTCC  scripts/basic/fixdep
  HOSTCC  scripts/kconfig/conf.o
  SHIPPED scripts/kconfig/zconf.tab.c
  SHIPPED scripts/kconfig/zconf.lex.c
  HOSTCC  scripts/kconfig/zconf.tab.o
  HOSTLD  scripts/kconfig/conf
scripts/kconfig/conf  --oldconfig Kconfig
security/Kconfig:393:warning: defaults for choice values not supported
security/Kconfig:397:warning: defaults for choice values not supported
security/Kconfig:401:warning: defaults for choice values not supported
security/Kconfig:405:warning: defaults for choice values not supported
security/Kconfig:409:warning: defaults for choice values not supported
#
# using defaults found in /boot/config-4.15.0-42-generic
#
#
# configuration written to .config
#
7、編譯內核

caojiaju@caojiaju-iMac:/home/kernel/KernelSource/linux-source-4.15.0$ sudo make
scripts/kconfig/conf  --silentoldconfig Kconfig
security/Kconfig:393:warning: defaults for choice values not supported
security/Kconfig:397:warning: defaults for choice values not supported
security/Kconfig:401:warning: defaults for choice values not supported
security/Kconfig:405:warning: defaults for choice values not supported
security/Kconfig:409:warning: defaults for choice values not supported
  SYSTBL  arch/x86/include/generated/asm/syscalls_32.h
  SYSHDR  arch/x86/include/generated/asm/unistd_32_ia32.h
  SYSHDR  arch/x86/include/generated/asm/unistd_64_x32.h
  SYSTBL  arch/x86/include/generated/asm/syscalls_64.h
  HYPERCALLS arch/x86/include/generated/asm/xen-hypercalls.h
  SYSHDR  arch/x86/include/generated/uapi/asm/unistd_32.h
  SYSHDR  arch/x86/include/generated/uapi/asm/unistd_64.h
  SYSHDR  arch/x86/include/generated/uapi/asm/unistd_x32.h
  HOSTCC  scripts/basic/bin2c
  HOSTCC  arch/x86/tools/relocs_32.o
  HOSTCC  arch/x86/tools/relocs_64.o
  HOSTCC  arch/x86/tools/relocs_common.o
  HOSTLD  arch/x86/tools/relocs
  CHK     include/config/kernel.release
  UPD     include/config/kernel.release
  WRAP    arch/x86/include/generated/uapi/asm/bpf_perf_event.h
  WRAP    arch/x86/include/generated/asm/clkdev.h
  WRAP    arch/x86/include/generated/asm/dma-contiguous.h
  WRAP    arch/x86/include/generated/asm/early_ioremap.h
  WRAP    arch/x86/include/generated/asm/mcs_spinlock.h
  WRAP    arch/x86/include/generated/asm/mm-arch-hooks.h
  CHK     include/generated/uapi/linux/version.h
  UPD     include/generated/uapi/linux/version.h
  CHK     include/generated/utsrelease.h
  UPD     include/generated/utsrelease.h
  CC      arch/x86/purgatory/purgatory.o
  AS      arch/x86/purgatory/stack.o
  AS      arch/x86/purgatory/setup-x86_64.o
  CC      arch/x86/purgatory/sha256.o
  AS      arch/x86/purgatory/entry64.o
  CC      arch/x86/purgatory/string.o
  LD      arch/x86/purgatory/purgatory.ro
  BIN2C   arch/x86/purgatory/kexec-purgatory.c
  CHK     include/generated/timeconst.h
  UPD     include/generated/timeconst.h
  CC      kernel/bounds.s
  CHK     include/generated/bounds.h
  UPD     include/generated/bounds.h
  GEN     scripts/gdb/linux/constants.py
  CC      arch/x86/kernel/asm-offsets.s
  CHK     include/generated/asm-offsets.h
  UPD     include/generated/asm-offsets.h
  CALL    scripts/checksyscalls.sh
  DESCEND  objtool
  HOSTCC   /home/kernel/KernelSource/linux-source-4.15.0/tools/objtool/fixdep.o
  HOSTLD   /home/kernel/KernelSource/linux-source-4.15.0/tools/objtool/fixdep-in.o
  LINK     /home/kernel/KernelSource/linux-source-4.15.0/tools/objtool/fixdep
  CC       /home/kernel/KernelSource/linux-source-4.15.0/tools/objtool/exec-cmd.o
  CC       /home/kernel/KernelSource/linux-source-4.15.0/tools/objtool/help.o
  CC       /home/kernel/KernelSource/linux-source-4.15.0/tools/objtool/pager.o
  CC       /home/kernel/KernelSource/linux-source-4.15.0/tools/objtool/parse-options.o
  CC       /home/kernel/KernelSource/linux-source-4.15.0/tools/objtool/run-command.o
  CC       /home/kernel/KernelSource/linux-source-4.15.0/tools/objtool/sigchain.o
  CC       /home/kernel/KernelSource/linux-source-4.15.0/tools/objtool/subcmd-config.o
  LD       /home/kernel/KernelSource/linux-source-4.15.0/tools/objtool/libsubcmd-in.o
  AR       /home/kernel/KernelSource/linux-source-4.15.0/tools/objtool/libsubcmd.a
  GEN      /home/kernel/KernelSource/linux-source-4.15.0/tools/objtool/arch/x86/lib/inat-tables.c
  CC       /home/kernel/KernelSource/linux-source-4.15.0/tools/objtool/arch/x86/decode.o
  LD       /home/kernel/KernelSource/linux-source-4.15.0/tools/objtool/arch/x86/objtool-in.o
  CC       /home/kernel/KernelSource/linux-source-4.15.0/tools/objtool/builtin-check.o
  ... ....

8、編譯模塊

caojiaju@caojiaju-iMac:/home/kernel/KernelSource/linux-source-4.15.0$ sudo make modules
[sudo] password for caojiaju: 
  CHK     include/config/kernel.release
  CHK     include/generated/uapi/linux/version.h
  CHK     include/generated/utsrelease.h
  CHK     include/generated/bounds.h
  CHK     include/generated/timeconst.h
  CHK     include/generated/asm-offsets.h
  CALL    scripts/checksyscalls.sh
  DESCEND  objtool
  CHK     scripts/mod/devicetable-offsets.h
  Building modules, stage 2.
  MODPOST 5150 modules

9、安裝模塊

caojiaju@caojiaju-iMac:/home/kernel/KernelSource/linux-source-4.15.0$ sudo make modules_install 
  INSTALL arch/x86/crypto/aes-x86_64.ko
  INSTALL arch/x86/crypto/aesni-intel.ko
  INSTALL arch/x86/crypto/blowfish-x86_64.ko
  INSTALL arch/x86/crypto/camellia-aesni-avx-x86_64.ko
  INSTALL arch/x86/crypto/camellia-aesni-avx2.ko
  INSTALL arch/x86/crypto/camellia-x86_64.ko
  INSTALL arch/x86/crypto/cast5-avx-x86_64.ko
  INSTALL arch/x86/crypto/cast6-avx-x86_64.ko
  INSTALL arch/x86/crypto/chacha20-x86_64.ko
  INSTALL arch/x86/crypto/crc32-pclmul.ko
  INSTALL arch/x86/crypto/crct10dif-pclmul.ko
  INSTALL arch/x86/crypto/des3_ede-x86_64.ko
  INSTALL arch/x86/crypto/ghash-clmulni-intel.ko
  INSTALL arch/x86/crypto/glue_helper.ko
  INSTALL arch/x86/crypto/poly1305-x86_64.ko
  INSTALL arch/x86/crypto/salsa20-x86_64.ko
  INSTALL arch/x86/crypto/serpent-avx-x86_64.ko
  INSTALL arch/x86/crypto/serpent-avx2.ko
  INSTALL arch/x86/crypto/serpent-sse2-x86_64.ko
  INSTALL arch/x86/crypto/sha1-mb/sha1-mb.ko
  INSTALL arch/x86/crypto/sha1-ssse3.ko
  INSTALL arch/x86/crypto/sha256-mb/sha256-mb.ko
  INSTALL arch/x86/crypto/sha256-ssse3.ko
  INSTALL arch/x86/crypto/sha512-mb/sha512-mb.ko
  INSTALL arch/x86/crypto/sha512-ssse3.ko
  INSTALL arch/x86/crypto/twofish-avx-x86_64.ko
  INSTALL arch/x86/crypto/twofish-x86_64-3way.ko
  INSTALL arch/x86/crypto/twofish-x86_64.ko
  INSTALL arch/x86/events/intel/intel-cstate.ko
  INSTALL arch/x86/events/intel/intel-rapl-perf.ko
  INSTALL arch/x86/kernel/cpu/mcheck/mce-inject.ko
  INSTALL arch/x86/kernel/cpuid.ko
  INSTALL arch/x86/kernel/msr.ko
  INSTALL arch/x86/kvm/kvm-amd.ko
  INSTALL arch/x86/kvm/kvm-intel.ko
  INSTALL arch/x86/kvm/kvm.ko
  INSTALL arch/x86/oprofile/oprofile.ko
  INSTALL arch/x86/platform/atom/punit_atom_debug.ko
  INSTALL block/bfq.ko
  INSTALL block/kyber-iosched.ko
... ....

 

10、第一個驅動程序

hello.c代碼

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

module_init(hello_init);
module_exit(hello_exit);

 

Makefile文件。//注意空格與Tab

obj-m :=hello.o
hellomodule-objs :=module #可以把hello這個改成你的命名
KERNELDIR := /lib/modules/$(shell uname -r)/build
PWD := $(shell pwd)
default:
    $(MAKE) -C $(KERNELDIR) M=$(PWD) modules

保存

編譯:sudo make

裝載驅動:sudo insmod ./hello.ko

查看當前安裝的驅動模塊 : lsmod

查看驅動加載輸出> cat /var/log/syslog  或者使用 >dmesg | tail 此條命令更簡潔

卸載命令 > sudo rmmod ./hello.ko

 

caojiaju@caojiaju-iMac:/home/kernel$ sudo make
make -C /lib/modules/4.15.0-42-generic/build M=/home/kernel modules
make[1]: Entering directory '/usr/src/linux-headers-4.15.0-42-generic'
  CC [M]  /home/kernel/hello.o
  Building modules, stage 2.
  MODPOST 1 modules
  CC      /home/kernel/hello.mod.o
  LD [M]  /home/kernel/hello.ko
make[1]: Leaving directory '/usr/src/linux-headers-4.15.0-42-generic'
caojiaju@caojiaju-iMac:/home/kernel$ 
caojiaju@caojiaju-iMac:/home/kernel$ 
caojiaju@caojiaju-iMac:/home/kernel$ ls
hello.c  hello.ko  hello.mod.c  hello.mod.o  hello.o  KernelSource  Makefile  modules.order  Module.symvers
caojiaju@caojiaju-iMac:/home/kernel$ sudo insmod ./hello.ko 
caojiaju@caojiaju-iMac:/home/kernel$ lsmod
Module                  Size  Used by
hello                  16384  0
pci_stub               16384  1
vboxpci                24576  0
vboxnetadp             28672  0
...  ...

 

系統日誌:

Dec 20 06:55:09 caojiaju-iMac nm-dispatcher: req:1 'dhcp4-change' [enp3s0f0]: start running ordered scripts...
Dec 20 06:55:09 caojiaju-iMac NetworkManager[787]: <info>  [1545260109.3422] dhcp4 (enp3s0f0): state changed bound -> bound
Dec 20 06:55:09 caojiaju-iMac systemd[1]: Starting Network Manager Script Dispatcher Service...
Dec 20 06:55:09 caojiaju-iMac systemd[1]: Started Network Manager Script Dispatcher Service.
Dec 20 06:55:09 caojiaju-iMac dhclient[1029]: bound to 192.168.1.102 -- renewal in 3212 seconds.
Dec 20 06:56:47 caojiaju-iMac kernel: [37939.266114] Hello, caojiaju 

 

友情提示: 千萬別使用來歷不明的驅動或linux內核,原因你懂的,不是所有人都像我這麼厚道。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章