1,Android源码中添加C可执行程序测试自定义linux内核驱动程序
①创建相关文件
1)文件senrsl.c
- senrsl@senrsl-ubuntu:~$ cd android/source/WORKING_DIRECTORY/
- senrsl@senrsl-ubuntu:~/android/source/WORKING_DIRECTORY$ cd external/
- senrsl@senrsl-ubuntu:~/android/source/WORKING_DIRECTORY/external$ mkdir senrsl
- senrsl@senrsl-ubuntu:~/android/source/WORKING_DIRECTORY/external$ touch senrsl.csenrsl@senrsl-ubuntu:~/android/source/WORKING_DIRECTORY/external$ cd senrsl
- senrsl@senrsl-ubuntu:~/android/source/WORKING_DIRECTORY/external/senrsl$ touch senrsl.c
- senrsl@senrsl-ubuntu:~/android/source/WORKING_DIRECTORY/external/senrsl$ gedit senrsl.c
- senrsl@senrsl-ubuntu:~/android/source/WORKING_DIRECTORY/external/senrsl$ touch Android.mk
- senrsl@senrsl-ubuntu:~/android/source/WORKING_DIRECTORY/external/senrsl$ gedit Android.mk
- senrsl@senrsl-ubuntu:~/android/source/WORKING_DIRECTORY/external/senrsl$
- /**
- *
- *这个程序的作用中,打开/dev/senrsl文件,然后先读出/dev/senrsl文件中的值,接着写入值 5到/dev/senrsl中去,最后再次读出/dev/senrsl文件中的值,看看是否是我们刚才写入的值5。从/dev /senrsl文件读写的值实际上就是我们虚拟的硬件的寄存器val的值。
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <fcntl.h>
- #define DEVICE_NAME "/dev/senrsl"
- int main(int argc, char** argv)
- {
- int fd = -1;
- int val = 0;
- fd = open(DEVICE_NAME, O_RDWR);
- if(fd == -1) {
- printf("Failed to open device %s.\n", DEVICE_NAME);
- return -1;
- }
- printf("Read original value shi:\n");
- read(fd, &val, sizeof(val));
- printf("%d.\n\n", val);
- val = 5;
- printf("Write value %d to %s.\n\n", val, DEVICE_NAME);
- write(fd, &val, sizeof(val));
- printf("Read the value again shi:\n");
- read(fd, &val, sizeof(val));
- printf("%d.\n\n", val);
- close(fd);
- return 0;
- }
2)编译脚本Android.mk
②编译模块
- LOCAL_PATH := $(call my-dir)
- include $(CLEAR_VARS)
- LOCAL_MODULE_TAGS := optional
- LOCAL_MODULE := senrsl
- LOCAL_SRC_FILES := $(call all-subdir-c-files)
- #BUILD_EXECUTABLE表示我们要编译的是可执行程序。
- include $(BUILD_EXECUTABLE)
③重新打包system.img
- senrsl@senrsl-ubuntu:~/android/source/WORKING_DIRECTORY$ mmm ./external/senrsl/
- ============================================
- PLATFORM_VERSION_CODENAME=REL
- PLATFORM_VERSION=4.4.2
- TARGET_PRODUCT=aosp_arm
- TARGET_BUILD_VARIANT=eng
- TARGET_BUILD_TYPE=release
- TARGET_BUILD_APPS=
- TARGET_ARCH=arm
- TARGET_ARCH_VARIANT=armv7-a
- TARGET_CPU_VARIANT=generic
- HOST_ARCH=x86
- HOST_OS=linux
- HOST_OS_EXTRA=Linux-3.13.0-37-generic-x86_64-with-Ubuntu-14.04-trusty
- HOST_BUILD_TYPE=release
- BUILD_ID=KVT49L
- OUT_DIR=out
- ============================================
- make:进入目录'/home/senrsl/android/source /WORKING_DIRECTORY'
- Import includes file: out/target/product/generic/obj/EXECUTABLES/senrsl_intermediates/import_includes
- target thumb C: senrsl <= external/senrsl/senrsl.c
- target Executable: senrsl (out/target/product/generic/obj/EXECUTABLES/senrsl_intermediates/LINKED/senrsl)
- target Symbolic: senrsl (out/target/product/generic/symbols/system/bin/senrsl)
- Export includes file: external/senrsl/Android.mk -- out/target/product/generic/obj/EXECUTABLES/senrsl_intermediates/export_includes
- target Strip: senrsl (out/target/product/generic/obj/EXECUTABLES/senrsl_intermediates/senrsl)
- Install: out/target/product/generic/system/bin/senrsl
- make:离开目录"/home/senrsl/android/source /WORKING_DIRECTORY"
④启动测试
- senrsl@senrsl-ubuntu:~/android/source/WORKING_DIRECTORY$ make snod
- ============================================
- PLATFORM_VERSION_CODENAME=REL
- PLATFORM_VERSION=4.4.2
- TARGET_PRODUCT=aosp_arm
- TARGET_BUILD_VARIANT=eng
- TARGET_BUILD_TYPE=release
- TARGET_BUILD_APPS=
- TARGET_ARCH=arm
- TARGET_ARCH_VARIANT=armv7-a
- TARGET_CPU_VARIANT=generic
- HOST_ARCH=x86
- HOST_OS=linux
- HOST_OS_EXTRA=Linux-3.13.0-37-generic-x86_64-with-Ubuntu-14.04-trusty
- HOST_BUILD_TYPE=release
- BUILD_ID=KVT49L
- OUT_DIR=out
- ============================================
- build/core/Makefile:860: Warning: with dexpreopt enabled, you may need a full rebuild.
- make snod: ignoring dependencies
- Target system fs image: out/target/product/generic/system.img
- Running: mkuserimg.sh out/target/product/generic/system out/target/product/generic/system.img ext4 system 576716800 out/target/product/generic/root/file_contexts
- + echo 'in mkuserimg.sh PATH=out/host/linux-x86/bin/:/home/senrsl/java/jdk1.6.0_45/bin:/home/senrsl/android/source/WORKING_DIRECTORY/out/host/linux-x86/bin:/home/senrsl/android/source/WORKING_DIRECTORY/prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.7/bin:/home/senrsl/android/source/WORKING_DIRECTORY/prebuilts/gcc/linux-x86/arm/arm-eabi-4.7/bin:/home/senrsl/android/source/WORKING_DIRECTORY/prebuilts/gcc/linux-x86/mips/mipsel-linux-android-4.7/bin:/home/senrsl/android/source/WORKING_DIRECTORY/development/emulator/qtools:/home/senrsl/android/source/WORKING_DIRECTORY/prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.7/bin:/home/senrsl/android/source/WORKING_DIRECTORY/prebuilts/gcc/linux-x86/arm/arm-eabi-4.7/bin:/home/senrsl/android/source/WORKING_DIRECTORY/development/scripts:/home/senrsl/android/source/WORKING_DIRECTORY/prebuilts/devtools/tools:/home/senrsl/test/depot_tools:/home/senrsl/android/android-ndk-r10b:/home/senrsl/android/adt-bundle-linux-x86_64-20140702/sdk/tools:/home/senrsl/android/adt-bundl e-linux-x86_64-20140702/sdk/platform-tools:/home/senrsl/java/jdk1.6.0_45/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games'
- in mkuserimg.sh PATH=out/host/linux-x86/bin/:/home/senrsl/java/jdk1.6.0_45/bin:/home/senrsl/android/source/WORKING_DIRECTORY/out/host/linux-x86/bin:/home/senrsl/android/source/WORKING_DIRECTORY/prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.7/bin:/home/senrsl/android/source/WORKING_DIRECTORY/prebuilts/gcc/linux-x86/arm/arm-eabi-4.7/bin:/home/senrsl/android/source/WORKING_DIRECTORY/prebuilts/gcc/linux-x86/mips/mipsel-linux-android-4.7/bin:/home/senrsl/android/source/WORKING_DIRECTORY/development/emulator/qtools:/home/senrsl/android/source/WORKING_DIRECTORY/prebuilts/gcc/linux-x86/arm/arm-linux-androideabi-4.7/bin:/home/senrsl/android/source/WORKING_DIRECTORY/prebuilts/gcc/linux-x86/arm/arm-eabi-4.7/bin:/home/senrsl/android/source/WORKING_DIRECTORY/development/scripts:/home/senrsl/android/source/WORKING_DIRECTORY/prebuilts/devtools/tools:/home/senrsl/test/depot_tools:/home/senrsl/android/android-ndk-r10b:/home/senrsl/android/adt-bundle-linux-x86_64-20140702/sdk/tools:/home/senrsl/android/adt-bundl e-linux-x86_64-20140702/sdk/platform-tools:/home/senrsl/java/jdk1.6.0_45/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
- + ENABLE_SPARSE_IMAGE=
- + '[' out/target/product/generic/system = -s ']'
- + '[' 6 -ne 5 -a 6 -ne 6 ']'
- + SRC_DIR=out/target/product/generic/system
- + '[' '!' -d out/target/product/generic/system ']'
- + OUTPUT_FILE=out/target/product/generic/system.img
- + EXT_VARIANT=ext4
- + MOUNT_POINT=system
- + SIZE=576716800
- + FC=out/target/product/generic/root/file_contexts
- + case $EXT_VARIANT in
- + '[' -z system ']'
- + '[' -z 576716800 ']'
- + '[' -n out/target/product/generic/root/file_contexts ']'
- + FCOPT='-S out/target/product/generic/root/file_contexts'
- + MAKE_EXT4FS_CMD='make_ext4fs -S out/target/product/generic/root/file_contexts -l 576716800 -a system out/target/product/generic/system.img out/target/product/generic/system'
- + echo make_ext4fs -S out/target/product/generic/root/file_contexts -l 576716800 -a system out/target/product/generic/system.img out/target/product/generic/system
- make_ext4fs -S out/target/product/generic/root/file_contexts -l 576716800 -a system out/target/product/generic/system.img out/target/product/generic/system
- + make_ext4fs -S out/target/product/generic/root/file_contexts -l 576716800 -a system out/target/product/generic/system.img out/target/product/generic/system
- Creating filesystem with parameters:
- Size: 576716800
- Block size: 4096
- Blocks per group: 32768
- Inodes per group: 7040
- Inode size: 256
- Journal blocks: 2200
- Label:
- Blocks: 140800
- Block groups: 5
- Reserved block group size: 39
- Created filesystem with 1282/35200 inodes and 78283/140800 blocks
- + '[' 0 -ne 0 ']'
- out/target/product/generic/system.img maxsize=588791808 blocksize=2112 total=576716800 reserve=5947392
1)启动模拟器
- senrsl@senrsl-ubuntu:~/android/source/WORKING_DIRECTORY$ emulator -kernel /home/senrsl/android/source/kernel/goldfish/arch/arm/boot/zImage
2)测试
OK!
- senrsl@senrsl-ubuntu:~$ adb shell
- root@generic:/ # cd system/bin/
- root@generic:/system/bin # ./sen
- sendevent senrsl sensorservice
- root@generic:/system/bin # ./senrsl
- Read original value shi:
- 0.
- Write value 5 to /dev/senrsl.
- Read the value again shi:
- 5.
- root@generic:/system/bin #
2015年04月10日15:32:26
--
senRsl
2015年04月10日13:28:51
2015年04月10日13:28:51
没有评论 :
发表评论