Obs: There are several commands enclosed by "". When typing in your terminal, just use the string, not the "". Example: "dmesg | tail -2" - when typing, use dmesg | tail -2.
Step 1) First of all, lets go to our home folder. To do so, type "cd". This will be our working directory for now.
Step 2) Create a folder called Drivers:
mkdir -p ./Documents/Drivers.
Step 3) Create a folder called "Aloha" inside new created folder "Drivers":
mkdir -p ./Documents/Drivers/Aloha
Step 4) Navigate to the new created folder:
cd ./Documents/Drivers/Aloha
Step 5) Create a file, using Vim, called aloha.c:
vim aloha.c
If you don't have vim, you can install it (using "sudo apt-get install vim") or you can use gedit to create and edite the file (to do so, use "gedit aloha.c" instead of the "vim aloha.c" command).
Step 6) Copy the text bellow to aloha.c and close this file. Basically, this is the core of a source driver file.
A brief explanation: When the driver is registered in your system, it will run this file. During the modules's initialization, the function "hello_init()" will be called and the message "Hello world!" will be showed in your system's messages.
Similarly, when unregistering the module, the function "hello_exit()" will be called.
<1> is the message's priority.
#include <linux/init.h>
#include <linux/module.h>
#include <linux/kernel.h>
MODULE_LICENSE("Dual BSD/GPL");
static int hello_init(void) {
printk("<1> Hello world!\n");
return 0;
}
static void hello_exit(void) {
printk("<1> Bye, cruel world\n");
}
module_init(hello_init);
module_exit(hello_exit);
|
Step 7) Create a file name "Makefile":
vim Makefile
Step 8) Copy the text bellow to Makefile and close this file.
Obs: There are many ways to do this kind of Makefile, I'm proposing one of them. Surelly, there are far more code-capable people out there who can make this code even more general.
Basically, what it does is:
A) Define some variables:
- ARQ is a variable containing the name of the compiled architecture;
- COMPILER is a variable containing the direction to the toolchain's compiler. Note that is should point to /bin folder and you should suppress "-gcc" in the end of "arm-none-linux-gnueabi"
- KERNELVERSION is a variable containing the direction of the kernel's compiled source file. THIS IS REALLY IMPORTANT. When getting your kernel source file's, you should issue some make commands. To do so, I suggest you to search Google for "how to compile kernel's source files".
-KERNELSOURCE is a variable containing the direction of host's kernel version source files.
-PWD is a variable containing the direction of the working directory. What actually PWD contains is the result of the shell command "pwd". If you're curious, open a new shell instance and type pwd and see the magic happens.
B) Define make commands:
- 2pc: Used when you want to compile the module to work in your host's kernel.
- 2ihm: Used when you want to cross-compile the module to another architecture.
- clean2pc: Clean your pc's compiled files.
- clean2ihm: Clean your ihm's compiled files.
# Makefile
# Autor: Fávero Santos # Property: Quantum Eletrônica # Year: 2015 # Description: A model Makefile for future consultations # Chooses the used architecture ARQ:=arm #Chooses the used compiler COMPILER:=/usr/local/gcc-4.6.2-glibc-2.13-linaro-multilib-2011.12/fsl-linaro-toolchain/bin/arm-none-linux-gnueabi- #Chooses the kernel sources for compilation KERNELVERSION:=/home/favero/Documents/IHM_imx6/KSource3/linux-3.0.35.imx-4.1.0-edm KERNELSOURCE:=/lib/modules/`uname -r`/build PWD:=$(shell pwd) # Name of the compiled file obj-m := aloha.o 2pc: $(MAKE) -C $(KERNELSOURCE) M=$(PWD) modules clean2pc: $(MAKE) -C $(KERNELSOURCE) M=$(PWD) clean 2ihm: $(MAKE) -C $(KERNELVERSION) ARCH=$(ARQ) CROSS_COMPILE=$(COMPILER) M=$(PWD) modules clean2ihm: $(MAKE) -C $(KERNELVERSION) M=$(PWD) clean |
Step 9) Run "make 2pc" to compile the module using your hosts' kernel source files OR run "make 2ihm" to cross-compile the module using the other platform's kernel source files
OBS: If everything went OK, issue a "ls" in this folder. You'll be able to see "aloha.ko" file. This file is the compiled module. To install it in your pc (you have used make 2pc), use "insmod aloha.ko". To remove, "rsmod aloha.ko". To check if it worked, use "dmesg | tail -2". You'll see "Hello World! and Bye, cruel world" messages.
Step 10) If needed, to clean the cross-compiled's module, use make clean2ihm. To clean the host's module, use make clean2pc. Always issue clean before changing platform.
Nenhum comentário:
Postar um comentário