레이블이 kernel module인 게시물을 표시합니다. 모든 게시물 표시
레이블이 kernel module인 게시물을 표시합니다. 모든 게시물 표시

2014년 1월 28일 화요일

How to upload a kernel module on Android emulator


Because Android system use linux as it's kernel, you can run your code in kernel mode by make and load a kernel module on Android.

This post simply explain how to build and load a kernel module on Android emulator.

Name of virtual device for default Android emulator is goldfish. So, in this post, I will call the kernel for Android emulator as goldfish kernel.

0. Environment
Development device / software version I used for this post is:

  • ubuntu 12.04 desktop 64bit
  • git 1.7.9.5
  • AOSP master branch synced someday between 4.2.1_r1 and 4.2.2


1. Build & Use goldfish kernel
goldfish kernel doesn't support module in default. So, you should modify the setting to support module, build again with the setting, and use the rebuilt kernel.

1.1. Get kernel source code for Android emulator
$ git clone https://android.googlesource.com/kernel/goldfish
$ cd goldfish
$ git checkout android-goldfish-2.6.29
(You can pick which branch to checkout as you want. You can see which branch is available using $ git branch -r command)

1.2. Get prebuilt project
For cross-compile, you should get AOSP's prebuilt project.
$ git clone https://android.googlesource.com/platform/prebuilt

1.3. kernel build configuration setting
You can set linux kernel build setting as you want. The setting information is saved as .config file in source code directory. But, the source code you got in above step have no .config file.
Make default goldfish kernel build configuration file with the command below:
$ make goldfish_defconfig

Now, you can see .config file. Or, you can get the configuration of already running emulator with command below.
$ adb pull /proc/config.gz
$ gunzip config.gz
$ mv config .config

1.4. Modify configuration to support module
Open .config file you generated and add next lines:
CONFIG_MODULES=y
CONFIG_MODULES_FORCE_LOAD=y
CONFIG_MODULES_UNLOAD=y
CONFIG_MODULES_FORCE_UNLOAD=y

If you don't set CONFIG_MODULE_UNLOAD, you can't unload moudle after loading. Be careful.

1.5. Build goldfish kernel
You can just follow s.android.com's same contents. In summary,
$ ARCH=arm CROSS_COMPILE=<path to prebuilt we downloaded above>/linux-x86/toolchain/arm-eabi-4.4.3/bin/arm-eabi- make -j2

The <path to prebuild we downloaded above> should be alternated with the path to prebuild you got from 1.2 section.
After build process, You can see the kernel image, zImage file.

1.6. Use the built kernel with emulator
You can specify which kernel the emulator will use with next command:
$ emulator -kernel <path of kernel image>
-show-kernel and -verbose option may be helpful in some way.

2. Build Android kernel module
Now, let's code and build kernel module for Android.
2.1. Source code for kernel module
Make simple file like below in name android_module.c

I will not explain about how the code works precisely in this post because it's not this post's object.
Simply, the source code will print "Hello android kernel..." when loaded on, "Goodbye android kernel..." message when it unloaded from kernel on kernel log.
You may change the content of source code as you want.

2.2. Write Makefile for the kernel module build process
Write Makefile with content below:
-C option at 4, 7line should point the path to kernel source code.


2.3. Build kernel module
Just set ARCH, CROSS_COMPILE environmental variable and do make. It's essentially same with kernel compile command.
$ ARCH=arm CROSS_COMPILE=<path to prebuilt we downloaded above>/linux-x86/toolchain/arm-eabi-4.4.3/bin/arm-eabi- make

If build success, you can see android_module.ko file generated.

3. Load kernel module
$ adb push android_module.ko /data/
$ adb shell
(Now, you are on Android's shell)
# insmod /data/android_module.ko

You can see the message Hello android kernel... on kernel log by using command like dmesg.

4. Unload kernel module
$ adb shell
# rmmod android_module

You can see the message Goodbye android kernel... on kernel log by using command like dmesg, too.


* You may encounter vermagic problem during module loading on some case.
In the case, article below may be helpful.
http://dry-kiss.blogspot.kr/2013/07/android-linux-module-vermagic.html


References:
http://linuxclues.blogspot.kr/2010/05/build-compile-linux-kernel-android.html
http://stackoverflow.com/questions/6282669/how-do-you-create-a-loadable-kernel-module-for-android
http://s.android.com/

2013년 7월 4일 목요일

android linux module 설치 시 vermagic 문제 해결

android에 kernel module 올리기(http://dry-kiss.blogspot.kr/search?updated-max=2013-07-04T11:23:00%2B09:00&max-results=1) 에 이어 작성한 글인데 올리지 않고 있다가 이제서야 포스팅.

android용 linux module 설치 시, module의 vermagic 이 맞지 않아 설치가 안되는 경우가 있다.

예를 들면 다음과 같은 메세지가 커널 메세지에 나오고, insmod에 실패한다.

version magic '2.6.29-12345 mod_unload ARMv7' should be '2.6.29-13579 mod_unload ARMv7'

이 경우, 커널 소스의 Makefile 의 최상단에 있는 EXTRAVERSION을 수정함으로써 vermagic의 리눅스 버전 다음 문자열을 바꿀 수 있다.

하지만, 이렇게 변경한 내용을 commit 하지 않으면 다음과 같이 기존 버전넘버와 -dirty 라는 이상한 문자가 붙게 되어 여전히 insmod 에 실패한다.
2.6.29abcd-ge3d684d-dirty mod_unload ARMv7

이는 scripts/setlocalversion 파일에서 git commit 되지 않은 변경사항이 있으면 자동으로 넣어주는 내용이다.

따라서, 해당 파일에서 version magic을 수정하는 부분을 주석 처리하거나, EXTRAVERSION을 변경한 내용을 커밋해 주면 된다.



http://forum.xda-developers.com/showthread.php?t=1236576
http://www.tldp.org/LDP/lkmpg/2.6/html/x380.html

2013년 3월 11일 월요일

android emulator에 kernel module 올리기


안드로이드는 커널로 리눅스를 사용하므로, 리눅스에서와 같이 커널 모듈을 만들어 올림으로써 커널 레벨로의 접근이 가능하다.

안드로이드 에뮬레이터에 커널 모듈을 만들어 올리는 간단한 방법을 기록해 둔다.

안드로이드용 에뮬레이터를 위한 가상 하드웨어의 이름은 goldfish다. 따라서, 이 글에서는 앞으로 안드로이드용 에뮬레이터의 커널을 goldfish 커널이라 부른다.

0. 환경
사용된 기기 / 소프트웨어 버전은 다음과 같다.
ubuntu 12.04 desktop 64bit
git 1.7.9.5

사용된 에뮬레이터의 안드로이드 버전:
4.2.1_r1과 4.2.2 사이 언젠가 sync 해둔 AOSP master branch.

1. goldfish 커널 빌드 & 사용
goldfish 커널은 기본적으로 모듈을 지원하지 않는다.
따라서 커널 소스코드를 받아 모듈을 지원하도록 설정을 수정, 빌드해 올리고 여기에 모듈을 올린다.

1.1. 안드로이드 에뮬레이터 커널 소스 받기
$ git clone https://android.googlesource.com/kernel/goldfish
$ git checkout android-goldfish-2.6.29
(checkout 하는 브랜치는 적당히. $ git branch -r 명령으로 사용 가능한 브랜치를 볼 수 있다)

1.2. prebuilt 소스 받기
크로스 컴파일을 위해 prebuilt 를 받아야 한다.
$ git clone https://android.googlesource.com/platform/prebuilt

1.3. kernel build configuration 설정
리눅스 커널은 빌드 설정을 다양하게 할 수 있으며, 이 정보는 소스코드 디렉토리의 .config 파일에 저장된다. 하지만, 앞서 받아온 소스파일에는 .config가 없다.
아래의 명령으로 goldfish를 위한 기본 설정을 만든다.
$ make goldfish_defconfig

이제 .config 파일이 만들어졌을 것이다.
또는, 다음의 명령으로 이미 돌고 있는 에뮬레이터의 설정을 가져올 수도 있다.

$ adb pull /proc/config.gz
$ gunzip config.gz
$ mv config .config

1.4. MODULE을 지원하는 방식으로 빌드되도록 설정
.config 파일을 열고 다음의 내용을 추가한다.
CONFIG_MODULES=y
CONFIG_MODULES_FORCE_LOAD=y
CONFIG_MODULES_UNLOAD=y
CONFIG_MODULES_FORCE_UNLOAD=y

CONFIG_MODULE_UNLOAD를 설정하지 않으면 모듈을 적재 후 내릴 수가 없다. 유의하자.

1.5. 안드로이드 에뮬레이터 커널 빌드
이건 그냥 s.android.com을 따라하면 되는데... 요약하자면,
$ ARCH=arm CROSS_COMPILE=<path to prebuilt we downloaded above>/linux-x86/toolchain/arm-eabi-4.4.3/bin/arm-eabi- make -j2
ARCH, CROSS_COMPILE은 따로 export ARCH=arm 과 같은 식으로 하고 마지막에 make만 하는 것도 당연히 가능...
CROSS_COMPILE 에 주는 prebuilt 위치는 앞서 1.2에서 받은 prebuilt임.

빌드가 끝나면 zImage로 커널 이미지가 나옴.

1.6. 에뮬레이터에서 새로 빌드한 커널을 사용
다음의 커맨드로 에뮬레이터가 사용할 커널을 지정 가능.
$ emulator -kernel <path of kernel image>
-show-kernel 과 -verbose 옵션을 추가로 주면 여러모로 편리하다.

2. 안드로이드용 커널 모듈 빌드
이제 안드로이드용 커널 모듈을 만들어보자.
2.1. 커널 모듈 소스코드
다음과 같이 android_module.c 파일을 생성


구체적인 linux kernel module 의 동작 원리는 이 글의 주제가 아니므로 스킵...
위 소스코드는 커널에 적재될 때 "Hello android kernel..." 메세지를, 커널에서 삭제될 때 "Goodbye android kernel..." 라는 메세지를 커널 로그 상에 뱉을 것이다.
실제 동작 코드는 각자의 목적에 맞게 바꾸면 되겠다.


2.2. 커널 모듈 빌드를 위한 Makefile 작성
다음과 같은 내용으로 Makefile을 생성.
4, 7라인의 -C 옵션의 값은 커널 소스가 있는 경로를 나타내야 한다.


2.3. 커널 모듈 빌드
다음과 같이 ARCH, CROSS_COMPILE 환경 변수 설정 후 make 하면 간단하게 끝.
커널 컴파일 할 때와 거의 같다.
$ ARCH=arm CROSS_COMPILE=<path to prebuilt we downloaded above>/linux-x86/toolchain/arm-eabi-4.4.3/bin/arm-eabi- make

같은 경로에 android_module.ko가 만들어질 것이다.

3. 커널 모듈 로드
$ adb push android_module.ko /data/
$ adb shell
(이제 android의 shell 위임)
# insmod /data/android_module.ko

dmesg 등의 커맨드로 커널 로그를 확인해 보면 Hello android kernel... 메세지가 보일 것이다.

4. 커널 모듈 언로드
$ adb shell
# rmmod android_module
역시 dmesg 등의 커맨드로 커널 로그를 확인해 보면 Goodbye android kernel... 메세지가 보일 것이다.


* 이 과정대로만 따라할 경우 vermagic에 의해 module load에서 문제를 겪는 경우가 생길 수 있다.
이 경우는 다음 글을 참고하면 된다.
http://dry-kiss.blogspot.kr/2013/07/android-linux-module-vermagic.html


참고 자료:
http://linuxclues.blogspot.kr/2010/05/build-compile-linux-kernel-android.html
http://stackoverflow.com/questions/6282669/how-do-you-create-a-loadable-kernel-module-for-android
http://s.android.com/