这里介绍安卓源码如何编译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/