Hello world kernel module for Android 13 i.MX 8MQ

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Hello world kernel module for Android 13 i.MX 8MQ

1,076 Views
builinhhh
Contributor II

Hello everyone, i'm a beginner of android embedded. i'm using i.MX8 MQ EVK board to build Android and i want write a helloworld kernel module to test. But i have some problems when compiler.
I don't know how to config and build kernel module. Can you help me indicate steps to do that?

Labels (1)
0 Kudos
5 Replies

999 Views
JorgeCas
NXP TechSupport
NXP TechSupport

Hello, I hope you are doing well.

Could you please share me the steps you are following to replicate the error?

Best regards.

0 Kudos

986 Views
builinhhh
Contributor II

I created 2 files: hello.c, Android.bp
This is code of Hello.c

// hello.c
#include <linux/module.h>
#include <linux/kernel.h>

int init_module(void) {
printk(KERN_INFO "Hello, kernel!\n");
return 0;
}

void cleanup_module(void) {
printk(KERN_INFO "Goodbye, kernel!\n");
}

 

This is code of Android.bp

cc_library {
name: "hello_kernel_module",
srcs: [
"hello.c",
],
arch: ["x86_64"],
}

Then I used mm to make file but it didn'n success.

I try alternative way by Makefile, but I think my Makefile is not true. 
this is Makefile and Hello.c

Hello.c

//kernel/hello_module/hello.c
#include <linux/module.h> /* Needed by all modules */
#include <linux/kernel.h> /* Needed for KERN_INFO */
#include <linux/init.h> /* Needed for the macros */
 
///< The license type -- this affects runtime behavior
MODULE_LICENSE("GPL");
 
///< The author -- visible when you use modinfo
MODULE_AUTHOR("ABC");
 
///< The description -- see modinfo
MODULE_DESCRIPTION("Hello World");
 
static int __init hello_start(void)
{
printk(KERN_INFO "Loading hello module...\n");
printk(KERN_INFO "Hello world\n");
return 0;
}
 
static void __exit hello_end(void)
{
printk(KERN_INFO "Goodbye Mr.\n");
}
 
module_init(hello_start);
module_exit(hello_end);
 
This is Makefile
obj-m += hello.o
PWD := $(shell pwd)
CLANG := /opt/prebuilt-android-clang/clang-r450784e/bin
KERNEL := /home/ubuntu/android_build/out/target/product/evk_8mq/obj/KERNEL_OBJ
LD = /opt/prebuilt-android-clang/clang-r450784e/bin/ld.lld
all:
make LD=$(LD) ARCH=arm64 CC=$(CLANG) -C $(KERNEL) M=$(PWD) modules
 
clean:
make -C $(KERNEL) M=$(PWD) clean
 
When i used Makefile, i didn't know what is tool to compile, that's reason Makefile error.
The first, i want to compile Hello.c, with output have Hello.ko
Then I want insmod Hello.ko to kernel through adb. Can you help me?
0 Kudos

898 Views
JorgeCas
NXP TechSupport
NXP TechSupport

Hello,

*** When i used Makefile, i didn't know what is tool to compile, that's reason Makefile error.

On the path where is Hello.c and your make file, run the next command:

make all

If you get errors, please share them.

Best regards.

0 Kudos

878 Views
builinhhh
Contributor II

when i used make all, this error is "this kernel was built by clang-450784e, your hello.c was built by gcc"
so file .ko was not compatible version kernel in android board"

0 Kudos

859 Views
JorgeCas
NXP TechSupport
NXP TechSupport

Got it.

Could you please share more about which Linux version are you using to compile?

I am having some issues to compile it since there are missing some packages for my current Linux version.

Best regards.

0 Kudos