安卓源码编译ko文件到设备img,并在开机阶段自动加载

这里介绍安卓源码如何编译ko文件到设备img,并在开机阶段自动加载。

1,复制ko文件并准备mk文件和.rc文件

我们要使用lunch sdk_car_md_x86_64-trunk_staging-userdebug编译出的模拟器设备来加载ko模块。

所以我们要在对应的device设备下进行配置:

1),cd android源码/device/generic/car/

2),创建helloworld文件夹和init.helloworld.rc文件,

并把ko文件复制到helloworld文件夹下。

文件树结构如下(sepolicy和helloworld.te不是必须):

bash 复制代码
device/generic/car$ tree helloworld/
helloworld/
├── helloworld.ko
├── init.helloworld.rc
└── sepolicy
└── helloworld.te

init.helloworld.rc内容如下:

bash 复制代码
on early-init
    exec u:r:vendor_modprobe:s0 -- /vendor/bin/modprobe -a -d \
        /vendor/lib/modules helloworld

2,修改device相关mk文件

修改device/generic/car/sdk_car_md_x86_64.mk,将rc文件打包到img中。

bash 复制代码
+PRODUCT_COPY_FILES += \
+    device/generic/car/helloworld/init.helloworld.rc:$(TARGET_COPY_OUT_VENDOR)/etc/init/init.helloworld.rc

修改device/generic/car/emulator_car64_x86_64/BoardConfig.mk文件,将ko文件打包到img中。

bash 复制代码
device/generic/car$ git diff emulator_car64_x86_64/BoardConfig.mk
diff --git a/emulator_car64_x86_64/BoardConfig.mk b/emulator_car64_x86_64/BoardConfig.mk
index 625fb0e..d225180 100644
--- a/emulator_car64_x86_64/BoardConfig.mk
+++ b/emulator_car64_x86_64/BoardConfig.mk
@@ -24,6 +24,14 @@ ifeq (true,$(ENABLE_CAR_USB_PASSTHROUGH))
 include device/generic/car/emulator/usbpt/BoardConfig.mk
 endif
 
+BOARD_VENDOR_KERNEL_MODULES += device/generic/car/helloworld/helloworld.ko
+
 # Override BOARD_SUPER_PARTITION_SIZE to increase the mounted system partition.
 BOARD_SUPER_PARTITION_SIZE := 5856296960

3,重新make编译安卓源码,并运行模拟器进行验证

通过adb shell lsmod确认helloworld模块已开机自动加载

通过adb shell file /dev/hello_char确认驱动文件节点存在

4, Tips

注意如果需要将ko文件编译进system分区,则需要进行部分修改,以及配置selinux。

汇总如下:

bash 复制代码
emulator_car64_x86_64/BoardConfig.mk修改:
+ BOARD_SYSTEM_KERNEL_MODULES += device/generic/car/helloworld/helloworld.ko
+ BOARD_SYSTEM_KERNEL_MODULES_LOAD += helloworld

rc文件内容:
+ on early-init
+    insmod /system/lib/modules/helloworld.ko

sdk_car_md_x86_64.mk修改:
+SYSTEM_EXT_PRIVATE_SEPOLICY_DIRS += \
+        device/generic/car/helloworld/sepolicy/
相关推荐
针叶4 小时前
Google Play加固保护导致的崩溃
android·安全·google
执明wa6 小时前
Android Studio 项目目录结构全方位详解
android·ide·android studio
__Witheart__7 小时前
Android编译错误:Soong阶段因缺失res目录导致panic (Iwlan模块)
android
酿情师8 小时前
逆向exe文件:CRT 初始化流程详细分析
android·软件构建·逆向·re·crt‘
问心无愧05139 小时前
ctf show web入门71
android·前端·笔记
夜勤月10 小时前
AQS 与 ThreadPoolExecutor 深度拆解:JDK 高并发底层设计精髓
android·java·开发语言
Yeyu10 小时前
Android 卡顿诊断 SDK:从痛点出发的设计思考
android
上天_去_做颗惺星 EVE_BLUE11 小时前
Ubuntu Android 虚拟机安装使用教程
android·linux·测试工具·ubuntu·安卓
我命由我1234511 小时前
Android 开发问题:Could not find com.github.PicnicSupermarket:FingerPaintView:1.2.
android·github·android studio·安卓·android jetpack·android-studio·android runtime