Linux Device Driver Programming Lecture 23- Testing of an LKM on target

  • Post author:
  • Post category:Blog

 

Testing of an LKM on target

 

 

In the previous article, we successfully built our kernel module for our host system.

Now, let’s explore how to build and test the same module on our target system. This process involves transferring the kernel object file, typically denoted as a .ko file, from the host to the target and then conducting tests on the target system.

Command to build a module against host
Figure 1. Command to build a module against host

 

The first step is building the module against our host system. You can use the same command as shown in Figure 1.

Command to build a module against target
Figure 2. Command to build a module against target

 

The -C flag should point to the target’s kernel source tree, which is typically located in our workspace. For example, /workspace/ldd/source/linux_bbb_4.14. Alternatively, you can specify the modules_install path or the Linux source path.

M=$PWD remains the same, indicating the current directory.

The target (modules) remains unchanged.

But you need to cross-compile because you are testing this kernel module on the target, that is, Beaglebone. That’s why we have to mention the cross compiler. Before this -C mention ARCH=arm and CROSS_COMPILE=arm-linux-gnueabihf- this looks like a long command.

 

Later I will explain how to include this command in a makefile itself. Let’s hit enter. 

After executing this command, you’ll notice that the module is successfully built. To check the module’s architecture, run the command file main.ko as shown in Figure 3. 

Command to check module’s architecture
Figure 3. Command to check module’s architecture

 

It should indicate that the module is built for ARM.

 

Now, you can use the modinfo command (as shown in Figure 4) on main.ko to extract module information, such as the description, author, license, and other details that you defined in the kernel module source.

Modinfo commands
Figure 4. Modinfo commands

 

The string we added is Board = Beagle bone black, a description, a author, a license, and various other things. 

 

Additionally, you can analyze various sections of the kernel object file using the command arm-linux-gnueabihf-objdump -h main.ko as shown in Figure 5.

Command to analyze various sections of the kernel object file
Figure 5. Command to analyze various sections of the kernel object file

 

We can run this command to analyze various sections of the kernel object file. Just hit enter, and you see there is an init section, as shown in Figure 6.

 Init section
Figure 6. Init section

 

This command will reveal sections like .init.text for module initialization functions and .exit.text for cleanup functions. Other sections may include the data section, which may be empty in your kernel module. Basically, it is showing 0 because our kernel module does not contain any data.

With the module successfully built, it’s time to transfer the .ko file to the Beaglebone. Open a new terminal and connect your Beaglebone Black hardware to your PC. Power up the hardware and boot the kernel using minicom or a similar tool.

Create a directory on the target system, for example, “drivers,” and copy the kernel module into this directory. From your host system, use scp to transfer main.ko to the target. The command should look like this:

scp main.ko debian@192.168.7.2:/home/debian/drivers 

Target’s username- debian, IP address 192.168.7.2. And the path is /home/debian/drivers. This is a new folder we created in the home directory of the Beagle bone. Let’s hit enter.

 Target and the host setting- .ko file linux
Figure 7. Target and the host setting

 

Here, there is some problem with an ssh connection. 

If you encounter an SSH connection issue, follow the solution provided in Figure 8.

Testing of an LKM on target- .ko file linux
Figure 8. Solution

 

After resolving the SSH issue, try the scp command again. When prompted with “Are you sure you want to continue connecting?” (Figure 9), respond affirmatively.  Yes.

Testing of an LKM on target- .ko file linux
Figure 9. Asking for continue connecting

 

It is asking you the password of the beagle bone temp pwd, and you see here we copied main.ko as shown in Figure 10.

Testing of an LKM on target- .ko file linux
Figure 10. .ko file is copied

 

Let’s go to the beagle bone command prompt  here, and go to drivers, here it is main.ko as shown in Figure 11.

Testing of an LKM on target- .ko file linux

Figure 11. .ko file copied to target

 

Install the module using the command sudo insmod main.ko (Figure 12).

Testing of an LKM on target- .ko file linux
Figure 12. Insmod main.ko

 

You should see “Hello world” printed directly on the terminal. Actually, it got printed a directly onto the terminals. So, I’ll explain why that happened in a later article. When we discussed about a kernel logs and kernel log priorities.

 

You can also run dmesg here to see that a message, or you can just type dmesg command or dmesg | tail as shown in Figure 13. You can see that message.

Testing of an LKM on target- .ko file linux
Figure 13. Dmesg logs

 

Lastly, remove the module using sudo rmmod main.ko, which should print “Goodbye world” (Figure 14).

Testing of an LKM on target-.ko file linux
Figure 14. Remove module

 

Congratulations, you have successfully tested your kernel module on the target system!

In the following article, we will build the same kernel module, but not as out of tree but let’s try with In-tree building

 

Get the Full Course on Linux Device Driver(LDD1) Here.

FastBit Embedded Brain Academy Courses

Click here: https://fastbitlab.com/course1

 

FastBitLab

The FastBit Embedded Brain Academy uses the power of internet to bring the online courses related to the field of embedded system programming, Real time operating system, Embedded Linux systems, etc at your finger tip with very low cost. Backed with strong experience of industry, we have produced lots of courses with the customer enrolment over 3000+ across 100+ countries.

Leave a Reply