Let's code a Linux Driver - 1: Hello World Linux Kernel Module
Вставка
- Опубліковано 21 сер 2024
- Let's leave userspace and head towards Kernelspace!
In this series of videos I will show you how to write your own Linux Driver. In this video, I will create a simple Hello World Linux Kernel Module Enjoy!
Here is the link to my git-repo:
github.com/Joh...
You want to support my work? You can buy me a coffee here: www.buymeacoff...
Here are some of my sources:
Mike Netz. Eigener linux-gpio-treiber für raspberry pi 3 model b. mike-netz.biz/?..., 2018. letzter Aufruf: 19.12.2020.
Jürgen Platte. Linux Hardware Hackz. Carl Hanser Verlag, 2007.
Martin Züger Usr Graf, Andreas Kalberer. Einführung in die Linux Treiberentwicklung. FHO Hochschule Ostschweiz, 2015.
For anyone struggling to get this working on a raspberry pi 4. I struggled to get mine compiling because there wasn't a build folder in /lib/module/$(shell uname -r). The solution, I switched from a headless 32bit OS to a headless 64bit OS.
Thank you Johannes for the awesome playlist and channel!
Finally started with driver development, thanks Johannes, you make concepts easily understandable!
read a few documentations and still no clue how do i start. found your video and managed to get to a basic start! thank you!
Monkey see monkey do. I wish the Linux people would learn this. UA-cam is full of great presentations by the Linux foundation on various interesting things but when you sit down to actually test the stuff they presented you are immediately stuck. They spend 1h talking about something that 1 % of the listeners can apply. If they just did what they are trying to present live it would be a thousand times more useful.
You are awesome, you make it look so easy and simple. Thank you so much!!
eh ya zmeely
Hi Johannes, thanks for sharing these videos. They're very informative and great to follow along.
Thank you very much for this. I look forward to going through the entire playlist.
Thank you for this excellent video. The whole world should see it!
hi man! (or whatever you prefer to be called) i really appreciate the series. it was really hard for me to get into making my own driver, but now i feel a little bit more confident. thanks again :)
Thank you for your contribution.
Your video helped me a lot in starting my journey in this domain.👍
Super - exactly what I was looking for! I tried it on Jetson AGX Xavier and x86 PC - both Ubuntu 20.04. Works like a charm.
I really liked your video, very informative and would love to see more from you :)
Fantastic video. I've seen so many different ways to do this, so it would be a nice addition if you added how to build the module when not on the target machine. Use if depmod and other tool, etc.
Thank you. Yes, maybe later this year I want to do a session about Cross Compiling. But therefore I would like to use an industrial grade SoC on an evaluation board. And I am still looking for an affordable candidate.
Very Informative Videos. I am going to see and work along side of your videos.
You are a very inspiring person. Thanks
Many thank. I really appreciate this tutorial.
This is a very nice intro, really simple easy to tackle.
Could you please recommend some books to learn more about the core concepts of Linux kernel development?
Thank you. Yes, I can recommend Linux Device Drivers 3rd edition. It is a little bit old, bit the concepts still apply. makelinux.net/ldd3/ There are more recent books available, but I haven't read them, so I can't really recommend them.
Embetronix also has a great series of written tutorials to Linux Drivers. embetronicx.com/tutorials/linux/device-drivers/linux-device-driver-part-1-introduction/
Thanks a lot @@johannes4gnu_linux96
Please add more content, your tutorials are really neat and easy to follow.
Thank you this was really useful to get a starting grasp! :)
Thanks for the video, awesome tutorial =)
Thank You For Your Great Effort 💗
Please 🙏 Can You Make Preif Introduction On How To Contribute To The Linux Kernel And And Make Customisation From It ?!
It helped me, thanks for the videos
Great tutorial.
Really useful video, thank you.
Thanks, steep learning curve for me but I just want to get my keyboard backlight to work
Thanks for this video buddy
You're amazing!
Johannes, thanks for this video. I followed the exact steps but I run a problem. /usr/src don't have the linux-header files as it already does in your tutorial. I'm using raspbian on my Raspberry Pi 3+.
It took me a while to figure it out. But basically I could solve it with 2 commands:
sudo apt install raspberrypi-kernel
sudo apt install raspberrypi-kernel-headers
The first one will update the kernel (if not updated) and the second will download the headers of the last kernel version, that are required to compile a module.
Hope this info helps someone.
Thank you for our feedback and the info :)
I mentioned this "preparation steps" in the first video, I have done on this "Let's code a Linux Driver" session:
ua-cam.com/video/x1Y203vH-Dc/v-deo.html
But it is also good to mention it here.
Hello! I've got makefile error:
make -C /home/jonathan/Linux_Kernel/5.3.0-28-generic/build M=/home/jonathan/Linux_Kernel modules
make[1]: *** /home/jonathan/Linux_Kernel/5.3.0-28-generic/build: No such file or directory. Stop.
Makefile:4: recipe for target 'all' failed
make: *** [all] Error 2
i am using ubuntu 18.04 without board for now. but it should compile, right?
Have you installed the Kernel headers? On Ubuntu run: sudo apt-get install linux-headers-$(uname -r)
Are You using the same makefile as I do? The path /home/Jonathan/Linux_Kernel/5.* Looks wrong.
@@johannes4gnu_linux96 Cool it seems to work! but i didn't get .ko file :
make -C /lib/modules/5.3.0-28-generic/build M=/home/jonathan/Linux_Kernel modules
make[1]: Entering directory '/usr/src/linux-headers-5.3.0-28-generic'
Building modules, stage 2.
MODPOST 0 modules
make[1]: Leaving directory '/usr/src/linux-headers-5.3.0-28-generic'
Update: it produces .ko file. i had spelling error in make file
@@Jonathan-ru9zl Good to hear it works.
Thanks for this video
I followed everything, but under output instead of the Hello Kernel i'm getting - can someone help?
Nice 😃👍🏻
thanks for the tutorials, i noticed that you have just burned the image in sd card and put that sd card on rpi board and access the board by ssh. and then just start writing the module on it ... i have found other videos where people first setup the yocto project under that they setup the QEMU and on that they start writing the device driver(this method is difficult). my main question is what method is used for which purpose because in both methods the goal is to write the device deriver.? please reply me as soon as possible.
Well, both methods have their advantages and disadvantages.
I choose to use a raspberry pi, because of the following reasons:
- a lot of people have RPis and know how to use them
- Good documentation
- Powerful computers (Mine has a Quad core A53 and 1GB of memory, which is a lot for an embedded device!)
- Full featured linux distro (with package manager, python, compiler, ... already available)
- RPi has all the important interfaces (SPI, GPIO, I2C, ...)
So, I choose the RPi because I wanted to use a comfortable computing platform, where I could just start to develop my Kernel Modules. I didn't want to use *Cross-Compiling* or *Virtualization with QMENU* because this are quite difficult topics and I just want to focus on the Kernel-Module-Programming. I believe for a beginner this approach is more straight forward and easier.
But if your target for your project is not a RPi, using your hardware, which is maybe not as powerful as the RPi, Yocto and Cross-Compiling is the way to go. Using Yocto has the advantages you can compile everything yourself (and apply some Kernel-Patches you are interested in, e.g. Realtime patches), you only have the programs onboard, you really need, you can use a custom Device Tree and your SD-Card Image will be a lot smaller then Raspbian. But going this way you loose a lot of comfort from the RPi, e.g. most Yoctos don't have a package manager as powerfull as apt, most of them don't have python or a compiler installed, ... With Yocto you really get a minimal Linux for an embedded system with all up- and downsides. You have to cross compile, because the compilation is to resource heavy for your tiny hardware, etc.
I would only use Virtualization with QMENU, if you don't have the hardware available and you want to start developing or if you have to debug a lot and you don't have a good (and most of the time expensive) JTAG-HW-Debugger.
So, for beginners, I would recommend to use a RPi. It will offer you a lot of comforts and a lot of interfaces. But if you don't mind the interfaces, you can develop Kernel modules on a normal x86-PC too! All of my tutorials, where I do not need I2C, SPI or GPIOs will work fine there too! Then, when you are more familiar with the Kernel-Programming and you have a project with a different Target-HW, then go the next step and start using Yocto, Cross-Compiling and Virtualization.
@@johannes4gnu_linux96 Thanks for your detailed answer which cleared my mind a lot, till now I have learned all of your device driver videos and from other channels, so I have written GPIO, USB, I2C and many more drivers. i have noticed that in all of that i just take any hardware attached with my rpi and start writing code. For example, In USB driver I attached my mobile phone with usb to rpi, and got the vendor id, product id with lsusb , and then wrote a driver to probe, disconnect and table, and get a minor number of device in output. and the same goes with the other drivers also. But I am unable to understand the need of writing drivers, in GPIO driver we can simply write c code or anything, what is the need of writing kernel device drivers. i have read my articles but I don't understand them completely, so I decided to ask you because your previous reply helped me a lot.
Hello!
On my system (postmarketOS edge), /lib/modules//build is absent. On my laptop however (x86_64), the directory exists. Does anyone here know how to resolve this issue?
One thing not obvious to me first time is that the file name my_module.c has to match the module name as my_module.o in the Makefile. I thought you could name the module something different to the c file and the Makefile was smart enough to collect all the *.c files in the $(PWD) folder.
It depends on your makefile. In the makefile, I am using in the video, I just set some variables and then call another makefile located in /lib/modules/$(shell uname -r)/build.
Here everything about how to build the module is defined.
The makefile is only smart enough to build every c file it finds in the folder, if you specify it in the makefile. In the case of our makefile it isn't.
Hey, really informative video. Is the module run with ring 0 permissions.
I keep getting missing separator errors even though I am using proper tabs (no spaces).
Thanks for this videos, wiiiiiiiiiiiiiiiiiiiiiiiiiiiiiiii
9:16 What exactly are you pointing to when you have the following path -C /lib/modules/$(shell uname -r)/build? I'm getting a "no such file or directory"
It resovles to something similar like this:
/lib/modules/6.10.5-arch1-1/build
If you are missing the build folder, you have to install the linux kernel headders, e.g. with "sudo apt install raspberrypi-linux-headers" on a raspberry pi.
Thanks!
how to Add a Linux kernel module, which is loaded dynamically and also load statically??
It was so explanatory
Thanks 👍
I see that he works with a Raspberry Pi, I can follow the videos and do the practices in my coputer x86_64 with Linux?, or it is necessary to use a Raspberry?
Thanks for sharing these videos.
Most of the videos should work except for those in which I access pheripherals Like GPIOs, I2C, SPI, ... These are normally not so easy accessable on a Standard PC Like on the Raspberry Pi.
@@johannes4gnu_linux96 Thank you very much, I will continue watching your videos.
Great Video! Can you make one on hooking write syscall on a file and instead of writing it direct the text to /dev/null or something similar causing the person not able to write to the file?
I don't understand what you mean here.But you can find videos where I am overloading syscalls for device files in my playlist.
make[1]: *** /lib/modules/4.19.94-ti-r42/build: No such file or directory. Stop.
could not insert module main.ko: Invalid module format,
any idea on how to deal with this error
It look like the kernel modules are not installed. Are you using a TI EVM or beaglebone with (Yocto) Linux?
For the raspberry pi maybe this helps: ua-cam.com/video/RJr0c4dpKxI/v-deo.html
Thanks
Hey, first of all, thanks very much for the playlist. I have a problem when I try to insert the same module you have shown in the video. I am getting the error "Invalid module format" from insmod, any help is appreciated, thanks!
You are welcome.
To help you, I need more information:
- What is your target (Raspberry Pi)?
- Are you doing the compiling on the target (on the RPi) or are you using cross compiling?
- If you are on a RPi, have you followed the tips shown here: ua-cam.com/video/RJr0c4dpKxI/v-deo.html
I know this error from doing cross compiling and the Kernel Version on my Target didn't match with the Kernel I used to compile my sources.
@@johannes4gnu_linux96 Oh okay, I am actually on Ubuntu virtual machine. I looked around before I commented here, I am sure the kernel I am running and the target kernel during compilation is the same, because the output of uname -r command matches.
having error
make -C /lib/modules/5.10.63-v7+/build M=/home/pi/Programming/Linux_Driver_Tutorial modules
make[1]: *** /lib/modules/5.10.63-v7+/build: No such file or directory. Stop.
make: *** [Makefile:3: all] Error 2
ua-cam.com/video/RJr0c4dpKxI/v-deo.html
This should solve the problem
thx
Hi Johannes ,Is this tutorial work for RPI 5?
Yes, all of my driver tutorial videos (if they are not outdated like the first SPI and I2C videos) should be compile-able on any Linux system. That's one of the tasks of an operating system to give us a hardware abstraction layer. When you come to drivers accessing hardware (e.g. accessing GPIOs) maybe you have to change something (e.g. pin numbers), but in general it should work.
good video however I was not able to go beyond compiling the module
-------------
make -C /lib/modules/5.10.63-v7+/build M=/home/pi/kernel_work modules
make[1]: *** /lib/modules/5.10.63-v7+/build: No such file or directory. Stop.
make: *** [Makefile:4: all] Error 2
-----------
Apparently headers do not exist for my raspbian kernel
Thank you.
Run this Commando:
sudo apt install raspberrypi-kernel-headers
This should solve the problem.
@@johannes4gnu_linux96 thanks
Reading package lists... Done
Building dependency tree
Reading state information... Done
raspberrypi-kernel-headers is already the newest version (1:1.20210831-3~buster).
0 upgraded, 0 newly installed, 0 to remove and 51 not upgraded.
@@imranrajjad7028 i m having same problem help me
make -C /lib/modules/5.10.63-v7+/build M=/home/pi/Programming/Linux_Driver_Tutorial modules
make[1]: *** /lib/modules/5.10.63-v7+/build: No such file or directory. Stop.
make: *** [Makefile:3: all] Error 2
@@johannes4gnu_linux96
error still continue :(
make -C /lib/modules//build M=/home/pi/hello modules
make[1]: *** /lib/modules//build: No such file or directory. Stop.
make: *** [Makefile:4: all] Error 2
Hello sir, Thanks for your sharing. I'm a newbie, and I'm facing this error while building the driver. Could you please help me?
root@beaglebone:~/Lesson_1# ls
hello_world.c Makefile
root@beaglebone:~/Lesson_1# make
make -C /lib/modules/4.19.94-ti-r42/build M=/root/Lesson_1 modules
make[1]: *** /lib/modules/4.19.94-ti-r42/build: No such file or directory. Stop.
make: *** [Makefile:4: all] Error 2
I think my lib should be updated with something, could you please share me a command to update, I try to google but cannot find the result.
Thank you very much.
make: * [Makefile:4: all] Error 2
ua-cam.com/video/RJr0c4dpKxI/v-deo.html
This should solve the problem
bro how to overclock
hii, i'm getting this kind of error.
dani@raspberrypi:~/ldd$ make -C /lib/modules/5.15.32+/build/ M=$PWD modules
make: Entering directory '/usr/lib/modules/5.15.32+/build'
make: *** No rule to make target 'modules'. Stop.
make: Leaving directory '/usr/lib/modules/5.15.32+/build'
please help
Looks Like the Kernel headers are not installed correctly. Watch this: ua-cam.com/video/RJr0c4dpKxI/v-deo.html
I try to do the same action, but in respond of 'dmesg | teil' I recieve an audit messages, that type=1400, apparmer='denied' and something
Hello, can I put your video on a Chinese website blibli ? I would indicate the source of your course
Yes, if you indicate the source it is ok from my side.
@@johannes4gnu_linux96 thank you , no problem!