0

Im a newbie building my first Linux Kernel Module. So the problem is that I built a raspberry kernel 64bit version 5.4 on Ubuntu22 and use make to create a helloworld_rpi4.ko file. The helloworld_rpi4.ko file is created successfully and then I scp it to the raspberrypi OS (rpi3-b-plus, 64bit-aarch64) that I use on QEMU-6.2. When I tried to sudo insmod it on the raspberry OS it gave me this error:

`insmod: ERROR: could not insert module helloworld_rpi4.ko: Invalid module format
`

My Makefile:

obj-m += helloworld_rpi4.o  

KERNEL_DIR ?= $(HOME)/linux



all:

    make -C $(KERNEL_DIR) \

        ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- \

        M=$(PWD) modules



clean:

    make -C $(KERNEL_DIR) \

        ARCH=arm64 CROSS_COMPILE=aarch64-linux-gnu- \

        M=$(PWD) clean



deploy:

    scp -P 5555 *.ko pi@localhost:/home/pi/.

My source code:

#include <linux/module.h>



static int __init hello_init(void)

{

    pr_info("Hello world init\n");

    return 0;

}



static void __exit hello_exit(void)

{

    pr_info("Hello world exit\n");

}



module_init(hello_init);

module_exit(hello_exit);



MODULE_LICENSE("GPL");

MODULE_AUTHOR("Alberto Liberal <aliberal@arroweurope.com>");

MODULE_DESCRIPTION("This is a print out Hello World module");

I used this code for installing cross_compile: sudo apt install crossbuild-essential-arm64

I tried to uname -r on the raspberry os and the version of its kernel is:

**5.4.51-v8+**. Then I modinfo the hellowolrd_rpi4.ko and the vermagic: 5.4.83-v8+ SMP preempt mod_unload modversions aarch64. So the kernel that I built and the one on raspberry is the same but I don't know why it still gave me the error above. Any helps of you would be appreciate!

David
  • 1
  • Does this answer your question? [Linux modules version error "Invalid module format"](https://stackoverflow.com/questions/30341496/linux-modules-version-error-invalid-module-format) – stark Jul 12 '23 at 10:31
  • Thanks for your help but sorry @stark, it's not. – David Jul 12 '23 at 14:14

1 Answers1

-2

The "Invalid module format" error typically occurs when there is a mismatch between the kernel version used to build the module and the kernel version running on the target system. In your case, the kernel versions appear to be different.

You mentioned that you built the kernel on Ubuntu 22 using version 5.4, but the Raspberry Pi's kernel version is 5.4.51-v8+. It seems that there might be a mismatch in the patch level or configuration between the two kernel versions. You can modify your module code as follows:

#include <linux/module.h>

static int __init hello_init(void)
{
    pr_info("Hello world init\n");
    return 0;
}

static void __exit hello_exit(void)
{
    pr_info("Hello world exit\n");
}

module_init(hello_init);
module_exit(hello_exit);

MODULE_LICENSE("GPL");
MODULE_AUTHOR("Alberto Liberal <aliberal@arroweurope.com>");
MODULE_DESCRIPTION("This is a print out Hello World module");
MODULE_VERSION("1.0");

Recompile the module and try loading it again on the Raspberry Pi.

  • Thanks for your help, but after modifying the error still stays the same. – David Jul 12 '23 at 14:06
  • Maybe it's about the configuration. I installed QEMU and Raspberry OS on [link](https://github.com/farabimahmud/emulate-raspberry-pi3-in-qemu). I don't know how to configure that match the version of raspberry OS (raspberrypi-3-b+) – David Jul 12 '23 at 14:10
  • MauriceMoss - Your seven answers this month appear likely to have been entirely or partially written by AI (e.g., ChatGPT). Please be aware that [posting AI-generated content is not allowed here](//meta.stackoverflow.com/q/421831). If you used an AI tool to assist with any answer, I would encourage you to delete it. – NotTheDr01ds Jul 12 '23 at 15:06
  • **Readers should review this answer carefully and critically, as AI-generated information often contains fundamental errors and misinformation.** If you observe quality issues and/or have reason to believe that this answer was generated by AI, please leave feedback accordingly. – NotTheDr01ds Jul 12 '23 at 15:06