4

My goal is it to cross-compile a kernel module for my raspberry pi. But I have some compatibility issues with my kernel sources on my pc and the version on my raspberry pi. My Pi uses this version:

root@Raspberry:~# uname -r
4.14.34-v7+

And if I compile the module on my host machine, I got it for this version:

name:           SimpleDriver
vermagic:       4.14.41-gadb282c SMP mod_unload ARMv7 p2v8

The problem is, that I can´t load this module, because of an invalid module format. How can I configure the kernel header on my host to match a specific version of my raspberry pi kernel?

Thanks!

Update:
I download the sources and follow your steps, but my cross-compiled module throws the error Invalid module format again. Everything seems ok if I check it with modinfo

Cross compiled:

filename:       /home/pi/Desktop/treiber/SimpleDriver.ko
description:    'Hello World' virtual device
license:        GPL
author:         Daniel Kampert
srcversion:     0CEACD8714A757B533779B7
depends:
name:           SimpleDriver
vermagic:       4.14.34-v7+ SMP mod_unload modversions ARMv7 p2v8

Makefile:

obj-m += SimpleDriver.o

# Current dir
PWD  := $(shell pwd)

# Kernel sources
KDIR := ${RASPBERRY_DIR}/Kernel/linux

all:
    make ARCH=$(ARCH) CROSS_COMPILE=$(CROSS_COMPILE) -C $(KDIR) M=$(PWD) modules

clean:
    make -C $(KDIR) M=$(PWD) clean

root@Raspberry:/home/pi/Desktop/treiber# insmod SimpleDriver.ko
insmod: ERROR: could not insert module SimpleDriver.ko: Invalid module format

I use the arm-rpi-4.9.3-linux-gnueabihf cross compiler arm-linux-gnueabihf-gcc from https://github.com/raspberrypi/tools

Compiled on the Pi:

root@Raspberry:/home/pi/Desktop/treiber# modinfo SimpleDriver.ko
filename:       /home/pi/Desktop/treiber/SimpleDriver.ko
description:    'Hello World' virtual device
license:        GPL
author:         Daniel Kampert
srcversion:     0CEACD8714A757B533779B7
depends:
name:           SimpleDriver
vermagic:       4.14.34-v7+ SMP mod_unload modversions ARMv7 p2v8

Makefile:

obj-m+=SimpleDriver.o

all:
        make -C /lib/modules/$(shell uname -r)/build M=${PWD} modules

clean:
        make -C /lib/modules/$(shell uname -r)/build M=${PWD} clean

What is wrong?

Thanks again!

4
  • 1
    Yeah, Raspbian seems to be a headache this way -- a normal distro would have the right build tree available. But looking here, they seem to have just created packages to match the kernel version used on Debian...which is not the one used actually used. TBH, I'd just recompile the kernel (it is actually an easy thing to cross-compile because it has no library dependencies). You could also have a look at this: raspberrypi.stackexchange.com/a/39346/5538
    – goldilocks
    Commented May 20, 2018 at 14:23
  • If you do want to trying building the whole thing, you'll need a .config. Use make bcm2709_defconfig for the multi-core models. Note if you are not using a multicore model, you can't cross-compile using a distro toolchain. They're ARMv7. The single core models are ARMv6.
    – goldilocks
    Commented May 20, 2018 at 14:31
  • Thank you for help. That hint with compiling the kernel was helpful. Everything works fine after compiling the kernel with this guide raspberrypi.org/documentation/linux/kernel/building.md I can compile the module on my host machine and after this, the module gets loaded by the raspberry pi. So the problem is solved. Thank you everyone!
    – Kampi
    Commented May 20, 2018 at 15:27
  • What was the solution when the vermagic fields matched but you still got the error Invalid module format? I'm trying to cross-compile a module for Bookworm. uname -r shows 6.1.0-rpi6-rpi-v7. I checked out the version of the Raspbian Linux kernel that corresponds to the commit for Linux 6.1 in the make file, however when I build my module modinfo shows vermagic: 6.1.0-v7+ Commented Nov 19, 2023 at 15:42

1 Answer 1

2

If you look at the Pi kernel source -- it is distinct from the mainline "vanilla" kernel -- on github, you can see by the top level Makefile the current stable is 4.14.41. The most frequent change in that Makefile is the version number, so if you look at its commit history you can find the first commit using 4.14.34. To find the latest release for that version, start with the next one (4.14.35), "Browse files", and click through "releases" at the top. The latest one will actually be for 4.14.34 (since this is the first commit for 4.14.35, which at that point has no release version -- note not all versions have releases either), and if you follow the commit index (a six digit hex value next to the .zip and .tar.gz options), you can "Browse files" again to get to the top of the tree, from April 16th, and double check the Makefile (it is 4.14.34).

https://github.com/raspberrypi/linux/tree/f70eae405b5d75f7c41ea300b9f790656f99a203

That's the source you want to use. You should then run make bcm2709_defconfig to get the v7+ configuration.

3
  • Thank you for that explanation. I´m quite nooby with git anymore. Can you also tell me how I can download this version with git? I think I will get the current version (4.14.41) if I use a simple clone for the repository
    – Kampi
    Commented May 19, 2018 at 19:04
  • You might as well download the zip. In any case, if you use the "clone or download" from that page you'll get 4.14.34. It is ~175 MB.
    – goldilocks
    Commented May 19, 2018 at 19:31
  • Ok, that seems like a solution. I found a way with git: git fetch origin f70eae405b5d75f7c41ea300b9f790656f99a203 git checkout f70eae405b5d75f7c41ea300b9f790656f99a203
    – Kampi
    Commented May 19, 2018 at 19:32

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.