|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "LinuxLab 真板开发:开发内核模块" |
| 4 | +author: iosdevlog |
| 5 | +date: 2020-12-31 15:09:32 +0800 |
| 6 | +description: "" |
| 7 | +cover-img: /assets/images/LinuxLab/IMX6ULL/hello_linux_lab_imx6ull.png |
| 8 | +category: |
| 9 | +tags: [LinuxLab, IMX6ULL, module] |
| 10 | +--- |
| 11 | + |
| 12 | +### 撰写并运行内核模块 |
| 13 | + |
| 14 | +```bash |
| 15 | +mkdir -p src/modules/hello_imx6ull |
| 16 | +cd src/modules/hello_imx6ull |
| 17 | +vi hello_imx6ull.c |
| 18 | +``` |
| 19 | + |
| 20 | +### hello_imx6ull.c |
| 21 | + |
| 22 | +```c |
| 23 | +#include <linux/kernel.h> |
| 24 | +#include <linux/module.h> |
| 25 | +#include <linux/init.h> |
| 26 | + |
| 27 | +static int __init hello_imx6ull_init(void) |
| 28 | +{ |
| 29 | + pr_info("hello imx6ull module init\n"); |
| 30 | + |
| 31 | + return 0; |
| 32 | +} |
| 33 | + |
| 34 | +static void __exit hello_imx6ull_exit(void) |
| 35 | +{ |
| 36 | + pr_info("hello imx6ull module exit\n"); |
| 37 | +} |
| 38 | + |
| 39 | +module_init(hello_imx6ull_init); |
| 40 | +module_exit(hello_imx6ull_exit); |
| 41 | + |
| 42 | +MODULE_DESCRIPTION("hello imx6ull - Linux Lab real board module example"); |
| 43 | +MODULE_AUTHOR("iOSDevLog <iosdevlog@iosdevlog.com>"); |
| 44 | +MODULE_LICENSE("GPL"); |
| 45 | +``` |
| 46 | +
|
| 47 | +### Makefile |
| 48 | +
|
| 49 | +```make |
| 50 | +# ARCH = arm |
| 51 | +# CROSS_COMPILE = arm-linux-gnueabi- |
| 52 | +
|
| 53 | +KERNEL_SRC ?= /lib/modules/`uname -r`/build |
| 54 | +
|
| 55 | +obj-m += hello_imx6ull.o |
| 56 | +
|
| 57 | +
|
| 58 | +modules: |
| 59 | + $(MAKE) -C $(KERNEL_SRC) M=$$PWD modules; |
| 60 | +
|
| 61 | +modules-install: |
| 62 | + $(MAKE) -C $(KERNEL_SRC) M=$$PWD modules-install; |
| 63 | +
|
| 64 | +clean: |
| 65 | + $(MAKE) -C $(KERNEL_SRC) M=$$PWD clean; |
| 66 | + $(RM) *.ko; |
| 67 | +``` |
| 68 | + |
| 69 | +### 编译该内核模块 |
| 70 | + |
| 71 | +```bash |
| 72 | +make modules m=hello_imx6ull |
| 73 | +make modules-install m=hello_imx6ull |
| 74 | +make modules-upload |
| 75 | +``` |
| 76 | + |
| 77 | +### 在真板上启动内核模块 |
| 78 | + |
| 79 | +```bash |
| 80 | +make login |
| 81 | +``` |
| 82 | + |
| 83 | +登录 IMX6ULL。 |
| 84 | + |
| 85 | +```bash |
| 86 | +sudo modprobe hello_imx6ull |
| 87 | +modprobe: ERROR: could not insert 'hello_imx6ull': Exec format error |
| 88 | +``` |
| 89 | + |
| 90 | +原因未知。 |
| 91 | + |
| 92 | +## `2021/01/04` 重头执行 |
| 93 | + |
| 94 | +将 *预览版* 从 TF卡 [fire-config刷机](https://embed-linux-tutorial.readthedocs.io/zh_CN/latest/linux_basis/fire-config_brief.html) 到 emmc。 |
| 95 | + |
| 96 | +重新执行了一遍 `next` 分支的操作。 |
| 97 | + |
| 98 | +已经有了 `hello_imx6ull` 模块,这次新建一个 `hello_linux_lab_hello`。 |
| 99 | + |
| 100 | +```bash |
| 101 | +cp -r src/modules/hello src/modules/hello_linux_lab_hello |
| 102 | +cd src/modules/hello_linux_lab_hello |
| 103 | +mv hello.c hello_linux_lab_hello.c |
| 104 | +``` |
| 105 | + |
| 106 | +`hello_linux_lab_hello.c` |
| 107 | + |
| 108 | +```c |
| 109 | +#include <linux/kernel.h> |
| 110 | +#include <linux/module.h> |
| 111 | +#include <linux/init.h> |
| 112 | + |
| 113 | +static int __init hello_linux_lab_imx6ull_init(void) |
| 114 | +{ |
| 115 | + pr_info("hello linux-lab imx6ull module init\n"); |
| 116 | + |
| 117 | + return 0; |
| 118 | +} |
| 119 | + |
| 120 | +static void __exit hello_linux_lab_imx6ull_exit(void) |
| 121 | +{ |
| 122 | + pr_info("hello linux-lab imx6ull module exit\n"); |
| 123 | +} |
| 124 | + |
| 125 | +module_init(hello_linux_lab_imx6ull_init); |
| 126 | +module_exit(hello_linux_lab_imx6ull_exit); |
| 127 | + |
| 128 | +MODULE_DESCRIPTION("hello linux-lab imx6ull - Linux Lab real board module example"); |
| 129 | +MODULE_AUTHOR("iOSDevLog <iosdevlog@iosdevlog.com>"); |
| 130 | +MODULE_LICENSE("GPL"); |
| 131 | +``` |
| 132 | +
|
| 133 | +`Makeifle` |
| 134 | +
|
| 135 | +```make |
| 136 | +KERNEL_SRC ?= /lib/modules/`uname -r`/build |
| 137 | +
|
| 138 | +obj-m += hello_linux_lab_imx6ull.o |
| 139 | +
|
| 140 | +
|
| 141 | +modules: |
| 142 | + $(MAKE) -C $(KERNEL_SRC) M=$$PWD modules; |
| 143 | +
|
| 144 | +modules-install: |
| 145 | + $(MAKE) -C $(KERNEL_SRC) M=$$PWD modules-install; |
| 146 | +
|
| 147 | +clean: |
| 148 | + $(MAKE) -C $(KERNEL_SRC) M=$$PWD clean; |
| 149 | + rm -f *.ko; |
| 150 | +``` |
| 151 | + |
| 152 | +Compile a kernel module and upload it |
| 153 | + |
| 154 | +```bash |
| 155 | +make modules m=hello_linux_lab_imx6ull |
| 156 | +make modules-install m=hello_linux_lab_imx6ull |
| 157 | +make modules-upload |
| 158 | +``` |
| 159 | + |
| 160 | +验证 |
| 161 | + |
| 162 | +```bash |
| 163 | +make login |
| 164 | +``` |
| 165 | + |
| 166 | +查看 `hello_linux_lab_imx6ull.ko` 类型。 |
| 167 | + |
| 168 | +```bash |
| 169 | +debian@npi:~$ ls /lib/modules/4.19.35+/extra/ |
| 170 | +hello.ko hello_imx6ull.ko hello_linux_lab_imx6ull.ko |
| 171 | +sudo apt install file |
| 172 | +file /lib/modules/4.19.35+/extra/* |
| 173 | +``` |
| 174 | + |
| 175 | +同 `hello.ko` 一样。 |
| 176 | + |
| 177 | +```bash |
| 178 | +/lib/modules/4.19.35+/extra/hello.ko: ELF 32-bit LSB relocatable, ARM, EABI5 version 1 (SYSV), BuildID[sha1]=c669968dec7cfd12ff354756b21f4cb6653fc74b, with debug_info, not stripped |
| 179 | +/lib/modules/4.19.35+/extra/hello_imx6ull.ko: ELF 32-bit LSB relocatable, ARM, EABI5 version 1 (SYSV), BuildID[sha1]=70f2771a81593d78ef1aea082b7bbf1ab54d6e11, with debug_info, not stripped |
| 180 | +/lib/modules/4.19.35+/extra/hello_linux_lab_imx6ull.ko: ELF 32-bit LSB relocatable, ARM, EABI5 version 1 (SYSV), BuildID[sha1]=a72a2e20aa70392320fdcef88bb7844ae38a7c2c, with debug_info, not stripped |
| 181 | +``` |
| 182 | + |
| 183 | +查看 `hello_linux_lab_imx6ull` 信息。 |
| 184 | + |
| 185 | +```bash |
| 186 | +debian@npi:~$ sudo modprobe hello_linux_lab_imx6ull |
| 187 | +[ 1177.900660] hello linux-lab imx6ull module init |
| 188 | +debian@npi:~$ lsmod | grep hello_linux_lab_imx6ull |
| 189 | +hello_linux_lab_imx6ull 16384 0 |
| 190 | +debian@npi:~$ dmesg | grep "hello linux" |
| 191 | +[ 1177.900660] hello linux-lab imx6ull module init |
| 192 | +debian@npi:~$ modinfo hello_linux_lab_imx6ull |
| 193 | +filename: /lib/modules/4.19.35+/extra/hello_linux_lab_imx6ull.ko |
| 194 | +license: GPL |
| 195 | +author: iOSDevLog <iosdevlog@iosdevlog.com> |
| 196 | +description: hello linux-lab imx6ull - Linux Lab real board module example |
| 197 | +srcversion: 23F6F2563D3621635B131C2 |
| 198 | +depends: |
| 199 | +name: hello_linux_lab_imx6ull |
| 200 | +vermagic: 4.19.35+ SMP preempt mod_unload modversions ARMv7 p2v8 |
| 201 | +``` |
| 202 | + |
| 203 | + |
| 204 | + |
| 205 | +## 总结 |
| 206 | + |
| 207 | +年前不知什么原因,导致 `Exec format error` 错误,今天用干净的环境重新执行一下又可以了。 |
| 208 | + |
| 209 | +确实要使用相同的环境(抽象出虚拟层)。 |
0 commit comments