Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How android compiles kernel drivers as modules

2025-04-08 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/01 Report--

In this article, the editor introduces in detail "how android compiles kernel drivers as modules". The content is detailed, the steps are clear, and the details are handled properly. I hope this article "android how to compile kernel drivers in a modular way" can help you solve your doubts.

1.4418 query and environment variable setting of kernel compiler

In development, you will encounter the situation where the source code and compiler are integrated together. The 4418 source code of Xunwei is integrated with the arm compiler. This section focuses on how to find the compiler path and configure environment variables.

Enter the android source directory (this directory belongs to the author. If the unzipped directory is different, please note that the following Makefile files and environment variable parameters should also be modified).

As shown in the following figure, use the command "cd kernel".

Use the command "cp-r config_for_iTOP4418_android_RTL8211 .config" to configure the kernel default file, and then use the command "export ARCH=arm" to set the platform to ARM.

Finally, use the command "make menuconfig".

The kernel configuration interface is shown in the following figure.

Enter "General setup-- >", as shown in the following figure. The "arm-eabi-" in the red box below is the information we need. The compiler used by the kernel is "arm-eabi-".

Exit the menuconfig interface, use the command "cd.. /" to return to the android directory, and then use the command "find. /-name * arm-eabi-*" to find the compiler that comes with the source code in that directory.

As shown in the figure above, we found that the compiler used by the kernel is in the "prebuilts/gcc/linux-x86/arm/arm-eabi-4.7/bin" directory. You may be wondering why the arm compiler is placed in the "prebuilts/gcc/linux-x86/" directory. Maybe Samsung workers don't bother to build folders again. There are also 4.6 and 4.7 versions, and we

Just use the high version.

Then the full path to the compiler is "/ home/4418/android/prebuilts/gcc/linux-x86/arm/arm-eabi-4.7/bin/".

Use the command "cd" command, and then use the command "vim .bashrc" to open the environment variables file, as shown in the following figure.

After pressing Shift+g, enter the bottom line of the environment variable text, as shown in the following figure.

As shown below, the author's previous compiler used arm-2009, which the author needs to comment out here. If the user has not been set up before, this step can be ignored.

Then add the compiler environment variable of 4418, export PATH=$PATH:/home/4418/android/prebuilts/gcc/linux-x86/arm/arm-eabi-4.7/bin/

This is shown in the following figure.

Save the exit and update the environment variable with the command "source .bashrc". Enter "arm" in the console, and then press Tab, as shown in the following figure. If the following interface does not appear or the original compiler still appears, you can close the console and open it again.

At this point, the compiler and environment variables are introduced.

2 Makefile and test-driven source code and compilation

2.1Makefile

Makefile script file:

Export ARCH=arm

Obj-m + = iTOP4418_driver_hello.o

KDIR: = / home/4418/android/kernel

PWD = $(shell pwd)

All:

Make-C $(KDIR) masking $(PWD) modules

Clean:

Rm-rf * .o

In the script, export ARCH=arm means to set the target CPU category to arm, that is, the compiled dependent kernel and driver module target CPU is ARM.

Obj-m + = iTOP4418_driver_hello.o indicates that the compiled source file is iTOP4418_driver_hello.c

If the source file name changes, it needs to be changed to the corresponding one.

The KDIR parameter points to the corresponding kernel source directory. The author's kernel source code is in

Under the / home/4418/android/kernel directory, users need to modify it according to their own specific conditions.

2.2 simple driver source code

The name of the driver file is iTOP4418_driver_hello.c, and the source code is as follows:

# include

# include

MODULE_LICENSE ("Dual BSD/GPL")

MODULE_AUTHOR ("iTOPEET_dz")

Static int hello_init (void)

{

Printk (KERN_EMERG "Hello World enter!\ n")

Return 0

}

Static void hello_exit (void)

{

Printk (KERN_EMERG "Hello world exit!\ n")

}

Module_init (hello_init)

Module_exit (hello_exit)

The driver source code has only basic entry and exit functions. Print "Hello Worldenter!" when loading and uninstalling, respectively. And "Hello world exit!"

2.3 compilation

Copy the source code to the Ubuntu system as shown in the figure below.

Using the command "make", as shown in the following figure, you can see that the "iTOP4418_driver_hello.ko" file is generated.

3 frequently asked questions

In the process of compiling the driver as a module, the novice may have the following problems.

1. The kernel source code is not compiled or the kernel source code path is not set correctly.

If the kernel source code is not compiled, the module will prompt for errors such as missing libraries; if the path is not set correctly, the kernel will not be found.

two。 The compiler is not set correctly.

You will be prompted that an error such as the arm-gcc library cannot be found. Please check the compiler path carefully to make sure that the arm-eabi compiler can appear after you enter arm+Tab on the console.

Other users may try to use other compilers, such as arm-2009q3, and so on. In theory, many compilers can also be compiled, but it is not recommended that the driver should use the same compiler as the kernel.

3. The source code and Makefile files are written under Windows and then copied to Ubuntu, resulting in transcoding errors due to different editors.

This kind of error is easy to solve. After Make is compiled, the system will prompt Makefile or a specific line of the driver file to have a problem. Use the vim editor to open and check, you can find some garbled, use the vim editor to correct it and then compile it.

After reading this, the article "how android compiles kernel drivers in a modular way" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it yourself. If you want to know more about related articles, you are welcome to follow the industry information channel.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report