From eaf3070816d3ef28aa12c66422b71d414c75017c Mon Sep 17 00:00:00 2001 From: zeljkoavramovic <29905129+zeljkoavramovic@users.noreply.github.com> Date: Mon, 5 Oct 2020 12:24:18 +0200 Subject: [PATCH 1/6] Add tutorial how to compile kernel with can-j1939 module --- can-j1939-install-kernel-module.md | 162 +++++++++++++++++++++++++++++ 1 file changed, 162 insertions(+) create mode 100644 can-j1939-install-kernel-module.md diff --git a/can-j1939-install-kernel-module.md b/can-j1939-install-kernel-module.md new file mode 100644 index 0000000..3c157fe --- /dev/null +++ b/can-j1939-install-kernel-module.md @@ -0,0 +1,162 @@ +# can-j1939 kernel module installation # + + + +### Problem + +You already have **can0** or **vcan0** up and working, **can-utils** downloaded and compiled to **~/can/can-utils** and you can send and receive frames without problems. However, when you want to bring up **can-j1939** you get error like this: + +```bash +avra@vm-debian:~/can/can-utils$ sudo modprobe can-j1939 +modprobe: FATAL: Module can-j1939 not found in directory /lib/modules/5.7.0.0.bpo.2-amd64 +``` + +and also this: + +```bash +avra@vm-debian:~/can/can-utils$ testj1939 +testj1939: socket(j1939): Protocol not supported +``` + + + +### Solution + +Above errors mean that **can-j1939** was not enabled in your kernel and you need to do it manually. There are several ways to do it. Any Linux kernel since 5.4 has **can-j1939** module, but you will probably want to install fresher version, which leads to downloading kernel sources, enabling **can-j1939** module, recompiling kernel and installing it. I will be using Debian 10.5 x64 (buster testing) virtual machine. + + + +#### 1. Download kernel source #### + +We will download Debian patched kernel 5.8. First update your sources + +```avra@vm-debian:~$ sudo apt update``` + +and then see if what Debian pathed kernel source versions are available + +``` +avra@vm-debian:~$ apt-cache search linux-source +linux-source-4.19 - Linux kernel source for version 4.19 with Debian patches +linux-source - Linux kernel source (meta-package) +linux-source-5.4 - Linux kernel source for version 5.4 with Debian patches +linux-source-5.5 - Linux kernel source for version 5.5 with Debian patches +linux-source-5.6 - Linux kernel source for version 5.6 with Debian patches +linux-source-5.7 - Linux kernel source for version 5.7 with Debian patches +linux-source-5.8 - Linux kernel source for version 5.8 with Debian patches +``` + +If kernel 5.8 does not show in the list (I can see it since I have already upgraded my 4.19 kernel to backported 5.7), then you will need to add backports to your sources list. Here is how my **/etc/apt/sources.list** looks like (you will need to append at least last line to yours): + +``` +deb http://security.debian.org/debian-security buster/updates main contrib +deb-src http://security.debian.org/debian-security buster/updates main contrib + +deb http://deb.debian.org/debian/ buster main contrib non-free +deb-src http://deb.debian.org/debian/ buster main contrib non-free + +deb http://deb.debian.org/debian buster-backports main contrib non-free +``` + +After adding backports try **sudo apt update** again, and **apt-cache search linux-source** should now show kernel 5.8 in the list, so you can install it's source package + +``` +sudo apt install linux-source-5.8 +``` + +and unpack it +``` +avra@vm-debian:/usr/src$ cd /usr/src +avra@vm-debian:/usr/src$ sudo tar -xaf linux-source-5.8.tar.xz +avra@vm-debian:/usr/src$ cd linux-source-5.8 +``` + + + +#### 2. Add can-j1939 module to kernel #### + +First we need some packages for **menuconfig** + +```sudo apt-get install libncurses5 libncurses5-dev``` + +copy and use our old configuration to run **menuconfig** + +``` +avra@vm-debian:/usr/src/linux-source-5.8$ sudo cp /boot/config-$(uname -r) .config +avra@vm-debian:/usr/src/linux-source-5.8$ sudo make menuconfig +``` + +where we enable SAE J1939 kernel module as shown + +``` + - Networking Support + - Can bus subsystem support + - SAE J1939 +``` + +Now edit **/usr/src/linux-source-5.8/.config**, find below key and change it as this +``` +CONFIG_SYSTEM_TRUSTED_KEYS="" +``` + +and save it. + + + +#### 3. Compile and install kernel and modules + +We will have to download necessary packages + +```sudo apt install build-essential libssl-dev libelf-dev bison flex``` + +compile kernel (using threads to make it faster) + +```avra@vm-debian:/usr/src/linux-source-5.8$ sudo make -j $(nproc)``` + +install + +``` +avra@vm-debian:/usr/src/linux-source-5.8$ sudo make modules_install +avra@vm-debian:/usr/src/linux-source-5.8$ sudo make install +``` + +and update grub + +``` +avra@vm-debian:/usr/src/linux-source-5.8$ sudo update-grub +avra@vm-debian:/usr/src/linux-source-5.8$ sudo reboot +``` + +Check if installation is correct with + +``` +sudo modprobe can-j1939 +``` + +and if you get no error then you can enjoy **can-j1939**. If you get some error then you might check if this alternative command works: + +``` sudo insmod /lib/modules/5.8.10/kernel/net/can/j1939/can-j1939.ko``` + +If it does then all you need to do is + +```sudo depmod -av``` + +reboot once, and **modprobe** command from the above should finally work now. + + + +#### 4. Install headers if needed + +You can have a problem with header file, to check that go in the file **/usr/include/linux/can.h** + +``` +nano /usr/include/linux/can.h +``` + +If in the struct **sockaddr_can** you don’t see **j1939**, then header files did not upgrade, so you need to do this manually + +``` +sudo cp /usr/src/linux-source-5.8/include/uapi/linux/can.h /usr/include/linux/can.h +sudo cp /usr/src/linux-source-5.8/include/uapi/linux/can/j1939.h /usr/include/linux/can/ +``` + +This is the minimum for compiling some **J1939** code, but you might want to upgrade other header files as well. That's up to you. Enjoy! \ No newline at end of file From 54935c2ece7aa8709a7eb40f00a833324c93675d Mon Sep 17 00:00:00 2001 From: zeljkoavramovic <29905129+zeljkoavramovic@users.noreply.github.com> Date: Mon, 5 Oct 2020 16:04:03 +0200 Subject: [PATCH 2/6] cosmetic changes --- can-j1939-install-kernel-module.md | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/can-j1939-install-kernel-module.md b/can-j1939-install-kernel-module.md index 3c157fe..ae8b26b 100644 --- a/can-j1939-install-kernel-module.md +++ b/can-j1939-install-kernel-module.md @@ -22,7 +22,7 @@ testj1939: socket(j1939): Protocol not supported ### Solution -Above errors mean that **can-j1939** was not enabled in your kernel and you need to do it manually. There are several ways to do it. Any Linux kernel since 5.4 has **can-j1939** module, but you will probably want to install fresher version, which leads to downloading kernel sources, enabling **can-j1939** module, recompiling kernel and installing it. I will be using Debian 10.5 x64 (buster testing) virtual machine. +Above errors mean that **can-j1939** was not enabled in your kernel and you need to compile it manually. There are several ways to do it. Any Linux kernel since 5.4 has **can-j1939** module, but you will probably want to install fresher version, which leads to downloading kernel sources, enabling **can-j1939** module, recompiling kernel and installing it. I will be using Debian 10.5 x64 (buster testing) virtual machine. @@ -30,7 +30,9 @@ Above errors mean that **can-j1939** was not enabled in your kernel and you need We will download Debian patched kernel 5.8. First update your sources -```avra@vm-debian:~$ sudo apt update``` +``` +avra@vm-debian:~$ sudo apt update +``` and then see if what Debian pathed kernel source versions are available @@ -45,7 +47,7 @@ linux-source-5.7 - Linux kernel source for version 5.7 with Debian patches linux-source-5.8 - Linux kernel source for version 5.8 with Debian patches ``` -If kernel 5.8 does not show in the list (I can see it since I have already upgraded my 4.19 kernel to backported 5.7), then you will need to add backports to your sources list. Here is how my **/etc/apt/sources.list** looks like (you will need to append at least last line to yours): +If kernel 5.8 does not show in your linux-sources list (it shows in mine since I have already upgraded stock 4.19 kernel to backported 5.7), then you will need to add backports to your sources list. Here is how my **/etc/apt/sources.list** looks like (you will need to append at least last line to yours): ``` deb http://security.debian.org/debian-security buster/updates main contrib @@ -76,7 +78,9 @@ avra@vm-debian:/usr/src$ cd linux-source-5.8 First we need some packages for **menuconfig** -```sudo apt-get install libncurses5 libncurses5-dev``` +``` +sudo apt-get install libncurses5 libncurses5-dev +``` copy and use our old configuration to run **menuconfig** @@ -106,11 +110,15 @@ and save it. We will have to download necessary packages -```sudo apt install build-essential libssl-dev libelf-dev bison flex``` +``` +sudo apt install build-essential libssl-dev libelf-dev bison flex +``` compile kernel (using threads to make it faster) -```avra@vm-debian:/usr/src/linux-source-5.8$ sudo make -j $(nproc)``` +``` +avra@vm-debian:/usr/src/linux-source-5.8$ sudo make -j $(nproc) +``` install @@ -134,11 +142,15 @@ sudo modprobe can-j1939 and if you get no error then you can enjoy **can-j1939**. If you get some error then you might check if this alternative command works: -``` sudo insmod /lib/modules/5.8.10/kernel/net/can/j1939/can-j1939.ko``` +``` +sudo insmod /lib/modules/5.8.10/kernel/net/can/j1939/can-j1939.ko +``` If it does then all you need to do is -```sudo depmod -av``` +``` +sudo depmod -av +``` reboot once, and **modprobe** command from the above should finally work now. @@ -159,4 +171,4 @@ sudo cp /usr/src/linux-source-5.8/include/uapi/linux/can.h /usr/include/linux/ca sudo cp /usr/src/linux-source-5.8/include/uapi/linux/can/j1939.h /usr/include/linux/can/ ``` -This is the minimum for compiling some **J1939** code, but you might want to upgrade other header files as well. That's up to you. Enjoy! \ No newline at end of file +This is the minimum for compiling some **J1939** code, but you might want to upgrade other header files as well. That's up to you. Enjoy! From 7655e8af307293e4a64b9b4d6451e98bdce9f98a Mon Sep 17 00:00:00 2001 From: zeljkoavramovic <29905129+zeljkoavramovic@users.noreply.github.com> Date: Tue, 6 Oct 2020 08:50:27 +0200 Subject: [PATCH 3/6] improvement by adding debian backports to separate sources list --- can-j1939-install-kernel-module.md | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/can-j1939-install-kernel-module.md b/can-j1939-install-kernel-module.md index ae8b26b..5c0eff6 100644 --- a/can-j1939-install-kernel-module.md +++ b/can-j1939-install-kernel-module.md @@ -47,7 +47,13 @@ linux-source-5.7 - Linux kernel source for version 5.7 with Debian patches linux-source-5.8 - Linux kernel source for version 5.8 with Debian patches ``` -If kernel 5.8 does not show in your linux-sources list (it shows in mine since I have already upgraded stock 4.19 kernel to backported 5.7), then you will need to add backports to your sources list. Here is how my **/etc/apt/sources.list** looks like (you will need to append at least last line to yours): +If kernel 5.8 does not show in your linux-sources list (it shows in mine since I have already upgraded stock 4.19 kernel to backported 5.7), then you will need to add backports to your sources list. It is best to do it like this + +``` +echo 'deb http://deb.debian.org/debian buster-backports main contrib' | sudo tee -a /etc/apt/sources.list.d/debian-backports.list +``` + +Just in case you have any problems with packages, or you want to have everything in a single list, here is what my **/etc/apt/sources.list** looks like (you will need to append at least last line to yours) ``` deb http://security.debian.org/debian-security buster/updates main contrib @@ -56,7 +62,7 @@ deb-src http://security.debian.org/debian-security buster/updates main contrib deb http://deb.debian.org/debian/ buster main contrib non-free deb-src http://deb.debian.org/debian/ buster main contrib non-free -deb http://deb.debian.org/debian buster-backports main contrib non-free +deb http://deb.debian.org/debian buster-backports main contrib ``` After adding backports try **sudo apt update** again, and **apt-cache search linux-source** should now show kernel 5.8 in the list, so you can install it's source package From 1e2197b8573a12ce2bd51f8c0ded22455aafc22c Mon Sep 17 00:00:00 2001 From: zeljkoavramovic <29905129+zeljkoavramovic@users.noreply.github.com> Date: Tue, 6 Oct 2020 12:34:44 +0200 Subject: [PATCH 4/6] better explanation for adding backports --- can-j1939-install-kernel-module.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/can-j1939-install-kernel-module.md b/can-j1939-install-kernel-module.md index 5c0eff6..6241f2f 100644 --- a/can-j1939-install-kernel-module.md +++ b/can-j1939-install-kernel-module.md @@ -53,7 +53,7 @@ If kernel 5.8 does not show in your linux-sources list (it shows in mine since I echo 'deb http://deb.debian.org/debian buster-backports main contrib' | sudo tee -a /etc/apt/sources.list.d/debian-backports.list ``` -Just in case you have any problems with packages, or you want to have everything in a single list, here is what my **/etc/apt/sources.list** looks like (you will need to append at least last line to yours) +Alternatively, in case you have problems with installation of some packages, or you just want to have everything in a single list, here is what my **/etc/apt/sources.list** looks like (you will need to append at least last line to yours) ``` deb http://security.debian.org/debian-security buster/updates main contrib From 056cfca9aa7f33d7637bfc98fd61c0f04570f926 Mon Sep 17 00:00:00 2001 From: zeljkoavramovic <29905129+zeljkoavramovic@users.noreply.github.com> Date: Tue, 6 Oct 2020 13:21:01 +0200 Subject: [PATCH 5/6] fixed typos --- can-j1939-install-kernel-module.md | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/can-j1939-install-kernel-module.md b/can-j1939-install-kernel-module.md index 6241f2f..fa9c476 100644 --- a/can-j1939-install-kernel-module.md +++ b/can-j1939-install-kernel-module.md @@ -34,7 +34,7 @@ We will download Debian patched kernel 5.8. First update your sources avra@vm-debian:~$ sudo apt update ``` -and then see if what Debian pathed kernel source versions are available +and then see what versions of Debian pathed kernel source are available ``` avra@vm-debian:~$ apt-cache search linux-source @@ -47,13 +47,13 @@ linux-source-5.7 - Linux kernel source for version 5.7 with Debian patches linux-source-5.8 - Linux kernel source for version 5.8 with Debian patches ``` -If kernel 5.8 does not show in your linux-sources list (it shows in mine since I have already upgraded stock 4.19 kernel to backported 5.7), then you will need to add backports to your sources list. It is best to do it like this +If kernel 5.8 does not show in your linux-sources list (it shows above in mine since I have already upgraded stock 4.19 kernel to backported 5.7), then you will need to add backports to your sources list. It is best to do it like this ``` echo 'deb http://deb.debian.org/debian buster-backports main contrib' | sudo tee -a /etc/apt/sources.list.d/debian-backports.list ``` -Alternatively, in case you have problems with installation of some packages, or you just want to have everything in a single list, here is what my **/etc/apt/sources.list** looks like (you will need to append at least last line to yours) +Alternatively, or in case you have problems with installation of some packages, or you just want to have everything in a single list, here is what my **/etc/apt/sources.list** looks like (you will need to append at least last line to yours) ``` deb http://security.debian.org/debian-security buster/updates main contrib @@ -65,7 +65,7 @@ deb-src http://deb.debian.org/debian/ buster main contrib non-free deb http://deb.debian.org/debian buster-backports main contrib ``` -After adding backports try **sudo apt update** again, and **apt-cache search linux-source** should now show kernel 5.8 in the list, so you can install it's source package +After adding backports in one way or another, try **sudo apt update** again, and after that **apt-cache search linux-source** should show kernel 5.8 in the list, so you can install it's source package ``` sudo apt install linux-source-5.8 @@ -73,7 +73,7 @@ sudo apt install linux-source-5.8 and unpack it ``` -avra@vm-debian:/usr/src$ cd /usr/src +avra@vm-debian:~$ cd /usr/src avra@vm-debian:/usr/src$ sudo tar -xaf linux-source-5.8.tar.xz avra@vm-debian:/usr/src$ cd linux-source-5.8 ``` @@ -103,12 +103,12 @@ where we enable SAE J1939 kernel module as shown - SAE J1939 ``` -Now edit **/usr/src/linux-source-5.8/.config**, find below key and change it as this +Now edit **/usr/src/linux-source-5.8/.config**, find CONFIG_SYSTEM_TRUSTED_KEYS, change it as following ``` CONFIG_SYSTEM_TRUSTED_KEYS="" ``` -and save it. +and save the file. @@ -158,23 +158,23 @@ If it does then all you need to do is sudo depmod -av ``` -reboot once, and **modprobe** command from the above should finally work now. +reboot once, and **modprobe** command from the above should finally work. #### 4. Install headers if needed -You can have a problem with header file, to check that go in the file **/usr/include/linux/can.h** +You might have a problem with headers not being updated. To check that open file **/usr/include/linux/can.h** with ``` nano /usr/include/linux/can.h ``` -If in the struct **sockaddr_can** you don’t see **j1939**, then header files did not upgrade, so you need to do this manually +If in the struct **sockaddr_can** you don’t see **j1939**, then header files did not upgrade and you need to do it manually ``` sudo cp /usr/src/linux-source-5.8/include/uapi/linux/can.h /usr/include/linux/can.h sudo cp /usr/src/linux-source-5.8/include/uapi/linux/can/j1939.h /usr/include/linux/can/ ``` -This is the minimum for compiling some **J1939** code, but you might want to upgrade other header files as well. That's up to you. Enjoy! +That is the minimum for compiling some **J1939** C code, but you might want to upgrade other header files as well. That's up to you. Enjoy! From e9146850545788b7c8c71c1d6fd53549ca95725c Mon Sep 17 00:00:00 2001 From: zeljkoavramovic <29905129+zeljkoavramovic@users.noreply.github.com> Date: Tue, 6 Oct 2020 13:38:48 +0200 Subject: [PATCH 6/6] fixed grammar --- can-j1939-install-kernel-module.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/can-j1939-install-kernel-module.md b/can-j1939-install-kernel-module.md index fa9c476..7de99a3 100644 --- a/can-j1939-install-kernel-module.md +++ b/can-j1939-install-kernel-module.md @@ -34,7 +34,7 @@ We will download Debian patched kernel 5.8. First update your sources avra@vm-debian:~$ sudo apt update ``` -and then see what versions of Debian pathed kernel source are available +and then look at available Debian patched kernel source packages ``` avra@vm-debian:~$ apt-cache search linux-source