How to add my source code to Kernel source tree - c

I've an I2C storage CHIP attached with system, A kernel space driver program of that CHIP is functioning with insmod / rmmod from running system.
But I would like to add this program with kernel source, so that on Kernel booting (from zImage) it will read from I2C CHIP, and Print something (e.g. Serial #) from it.
My question is, is it enough to add the driver object with kernel/Makefile
as obj-y += ?

while searching for Kernel module writing and so on, you'll find a hundreds of thousands "hello_world.c" program, that will describe how you can print
hello_world at kernel log, when your kernel and system all working and up. Which are called loadable kernel module, using program like insmod to load and rmmod to unload.
But, I was searching how to load my own script (i.e. that hello_world.c) when kernel is booting, and wanna see something on console that exactly coming from kernel at booting time, then google is almost silent.
Well, its just not difficult, and quite easy. What I've done by myself to get the printk() message at kernel booting is -
Kept the hello_world.c program inside ./kernel/driver (You can keep it almost anywhere inside Kernel source folder also can make your own folder, but that will require your own Makefile).
And edit the ./kernel/driver/Makefile to add obj-y += hello_world.o
at the end of the Makefile.
Then compile the full kernel and generated zImage transferred to sd-card.
While booting, just after USB HID core driver, I am able to see my custom message from hello_world printk().
[ 3.652944] usbcore: registered new interface driver usbhid
[ 3.657253] usbhid: USB HID core driver
[ 3.660152] HELLO: HELLOING WORLD from KERNEL BINARY
NOTE:- Unlike, loadable module, function maked with __exit would never called by compile when subjected code is in-built with kernel source.
i.e. in Such case hello_world.c is "Programmed to received (read include) but can't never leave"

Related

Rebuild linux kernel module for another architecture

Suppose I have x86-64 machine with some version of Linux kernel. And I have directory with kernel sources of another version. The kernel was built for arm arch and loaded to the appropriate device.
Now I need to rebuild just one kernel module in this big directory.
I read this post and tried something like
make path/to/the/module/itself.ko
, but it build module for amd64.
When I try
make M=path/to/the/module/
it gives a bunch of arch-related C-errors.
Could someone explain how can easy use this ARM-ready environment to rebuild some kernel module?
You could try:
make ARCH=arm CROSS_COMPILE=arm-linux-gnueabi- M=path/to/the/module/
Also read:
Cross compiling a kernel module
gcc-arm-linux-gnueabi command not found

Debugging of module in linux

How to debug the modules in Linux? As their will be no executable file for the modules? What does ELF will do ? Only the Makefile i have complied and given me object file and various other files.
As their will be no executable file for the modules?
There is no executable (like a.out), but there is kernel object file *.ko. Kernel object files are added to or removed from the kernel via insmod and rmmod.
So at minimum, you need to be able to do the following to debug a kernel module:
Locate the module itself (*.c file and corresponding *.ko kernel object).
Add (a lot of) printk
Rebuild the kernel.
rmmod the old module, insmod the new version.
Watch out debug logs, normally via dmesg.
There are several methods for debugging by printing (i.e printk),watching,querying
Refer to understand different debugging methods for module. Obvious method is we can use printk and after inserting (insmod) and removing (rmmod) module and that message can be seen in kernel buffer using $dmesg

Compiling an individual kernel module (Debian/Ubuntu)

I need to modify the ELF loader's kernel implementation of an Ubuntu 14.04 distribution. Having downloaded the sources using:
sudo apt-get source linux-image-$(uname -r)
I ran the configuration script:
make config
in the root source tree. After a seemingly endless sequence of input requests, the script created the .config file needed to build the kernel(or a set of modules). The kernel version I am using is linux-3.13.0 and has the following source tree layout:
$ ls
arch COPYING crypto Documentation dropped.txt FileSystemMakefile fs init Kbuild kernel MAINTAINERS mm README samples security sound ubuntu virt
block CREDITS debian.master drivers elf.dat firmware include ipc Kconfig lib Makefile net REPORTING-BUGS scripts shortcuts tools usr
The ELF loader is located in /path/to/source/fs/binfmt_elf.c. Following this question,in order to compile an individual module it is sufficient to run
make /path/to/module/directory.
In this case that would be:
make ./path/to/source/fs
The compilation is quite lengthy; it takes about twenty minutes(on a virtual machine) and the output is written(by default) in the same directory in which the module is located. I've found the object files by running:
find . -name "*.o"
in /path/to/source/fs. Filtering by name the ELF loader can be located by running:
find . -name "*elf*.o"
In the current sources it is written(by default) in:
/path/to/source/fs/binfmt_elf.o
Having gone through this tutorial, I've noticed that kernel modules have the naming convention [module_name].ko in order to distinguish them from user space object files.
My question is how can I insert the new(modified) ELF loader into the kernel given that the current ELF loader is present(as unloading it may prevent binaries from being executed)?
What you have described is not really compiling a "kernel module" as it is commonly referred to. You have built an object that is statically linked into the kernel and there is no way that you can load just that object into a running kernel.
"kernel module" usually refers to "loadable kernel module" (LKM). Building and loading the fs as an LKM is what you need/want. Take a look at the below HOWTO. Follow that to build the desired fs as an LKM. Then you can just replace that one LKM (.ko) file and reboot (normally you can dynamically remove and insert LKMs but not sure how that will affect something fundamental like the ELF fs - you can try rmmod/modprobe without a reboot first if you ike).
http://www.tldp.org/HOWTO/Module-HOWTO/x73.html

Usb to Ethernet driver compilation on linux kernel 3.0 and above

I cross compiled USB to Ethernet driver in the Linux Kernel source tree at drivers/net/usb/smsc75xx.c for Android Kernel 3.0.8. Cross compilation worked fine as well as inserting the Kernel module using insmod. But the ethernet interface does not show up nor dmesg detects module's insertion (using insmod) or removal (using rmmod). Since dmesg is silent about its insertion or removal, something seems not right?
How to debug this non-working driver in a non-verbose environment?
Check the drivers/net/usb/smsc75xx.c. It seems the driver is already in the upstream.

Testing kernel Crypto API on linux

I have a Freescale i.MX board on which I run the Linux 3.0.35 kernel.
I want to test if the Kernel Crypto API of my Linux system works. I just found out the test program is called tcrypt. I see that under /lib/modules/ there is a tcrypt.ko in the drivers directory. This being the case, is there anyway I can test run this library? How do I call it? Do I need to reconfigure my kernel to "enable" something to call it?
Please keep in mind that I am new to kernel API's.
I had to compile the kernel modules. This built the "tcrypt.ko" module too. Then I used "insmod" command to load the module. Once loaded, the module was started with :
modprobe tcrypt sec=1 mode=200
(where mode is the algorithm to test)

Resources