This article was reproduced in: Great God
In a previous article, we learned about the binder mechanism in Android and the remote service invocation in this article that focuses on how Android apps work when invoking some system services. Then continue to introduce how to compile the Android source code to manually add a system service, so that the compiled Android system exists in our service, each application can be called. But this article must first describe how to write a simple driver, introduced the driver, and then let the framework layer through JNI to access the driver, in the registration service to encapsulate this feature, and finally add the service to the system, the implementation of upper-level applications by accessing the service to operate the drive , of course, you have to master and understand how to compile the source code in Android if you have not compiled the Android source code, the following content will look very laborious.
Disclaimer: This article refers to the Luo Shenyang book: "Android system source code Scenario analysis" If you want to know more details, it is very recommended to buy this book
Thank Juno for this book, Bring me a lot of unknown knowledge, the great God's blog address: Http://blog.csdn.net/luoshengyang
First, write kernel-driven applet
The kernel-driven applet content is not covered here, because there is nothing to say about the Linux system API, and then look at the concept of the drive. Here the main introduction of the operating procedures, about the code behind I will give, interested students can take a detailed look.
First step: Enter the source code kernel directory to create the driver directory
CD Kernel/drivers
In this directory, we create a new driver directory of our own Freg:
See that there are a lot of system drivers in this directory. Then write the driver in this Freg directory:
The freg.c here is the driver, and here we don't write kernel drivers for real hardware devices. To facilitate describing the process of writing kernel drivers for Android, we use a virtual hardware device that has only a 4-byte register, which is readable and writable.
Step Two: Configure driver compilation information
Added kconfig and makefile two files in the Freg directory, where Kconfig is used to perform configuration command make menuconfig before compiling:
And makefile is used to execute the compile command:
Step three: Drive the driver to the system configuration
The above configuration of the driver itself compiled information, if you want to compile this driver into the kernel, you also need to configure the kernel information, modify arch/arm/kconfig and drivers/kconfig two files, in the menu "Device Drivers" and Endmenu add a line between:
SOURCE "Drivers/hello/kconfig"
This allows you to configure the compile options for the Hello module when you execute make menuconfig. Modify the Drivers/makefile file to add a line:
Fourth step: Compiling the kernel driver
Run the command in the kernel directory: Make Menuconfig
"Device Drivers" = "First Android Drivers" option, set to Y:
This compiles the driver successfully, the following will start to compile the kernel source code
Fifth Step: Launch simulator check driver
To the root directory of the source code, run the command: Emulator-kernel./kernel/common/arch/arm/boot/zimage &
Some students may encounter this error, but it doesn't matter, we are running the script file:
If the operation succeeds, you can see the simulator:
At this point, we use the adb shell command to view the features of the driver:
First the driver must be stored in the/dev directory, if you want to operate the driver, you need to find the specified driver in the/proc directory, and then you can use the echo command to write the data, the Cat command reads the data.
Here, we have successfully written the virtual driver, and compiled into the kernel, through the command access is also passed the test. Here is the system-provided method and the driver to interact, that is, through the proc file system and Devfs file system method.
Second, write kernel-driven access program
With the above driver content, in order to better facilitate the operation of this driver, we can write a simple C language to directly access the driver, and the operation of the driving method is very simple, using the open method.
Step one: Set up the System program directory
Enter the/external/directory and create a new Freg program directory:
Step two: Write the action driver
The code here is simple, open the driver directly in the main function, and then you can read and write operations directly.
Step Three: Build a compilation script and then compile the program
In the same directory of the program, create the compilation script: Android.mk file
Refer to the article on how to compile the modules in the Android source code separately, using the MMM command to compile:
Mmm./external/freg
Once the compilation is successful, you can see the executable hello in the Out/target/product/gerneric/system/bin directory.
RePack Android System files System.img:
Make Snod
This way, the repackaged system.img file contains the hello executable file that was just compiled.
Fourth step: Run the emulator
The command to run the emulator is described above, and here is not the demo, after the successful operation, you can use the/system/bin/hello executable to access the Linux kernel driver:
System commands are stored in the/system/bin directory, we can see to find our compiled read-driven system command program Freg, and then you can run this program to drive read and write operations. This completes the use of the C program to operate the driver.
Project Case code: http://download.csdn.net/detail/jiangwei0910410003/9642364
Iii. Summary
This article describes how to compile the Android kernel source code, to manually write a simple virtual driver, and then write a simple C program to access the driver, the following will show how the framework layer through the JNI to access the driver.
Android System--write a simple driver and compile it into the kernel source "go"