安卓源码编译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/
相关推荐
三少爷的鞋9 小时前
Kotlin 协程,重点就在「协」这个字上
android·kotlin
好的佩奇9 小时前
Dart 之模式
android·flutter·dart
淡淡的香烟11 小时前
Flutter实现多语言切换
android·flutter·1024程序员节
AIsoft_868913 小时前
手机视频转文字工具清单:哪些APP支持录屏识别
android·音视频
_祝你今天愉快15 小时前
Paint Cavans高级绘制:[一]Paint画笔高级应用
android
@zulnger16 小时前
安卓进行稳定性测试
android·appium·1024程序员节
不吃凉粉17 小时前
android 副屏连接
android
OxYGC18 小时前
手机为什么能“自动跳过“开屏广告?李跳跳、GKD 与 iOS 代理软件原理盘点
android·ios·开屏广告·广告屏蔽
Android-Flutter1 天前
Compose 管理状态 详解
android·compose
缓慢更新1 天前
审批的加签与会签:多人意见怎么合
android·java·数据库·低代码·rxjava