前言
- 开发板型号:RK_EVB7_RK3588_LP4…_V11
- 目标:在开发板上随开机自启动脚本,带起二进制程序,并完备一些其他系统功能。
- 简介:本文自启动脚本run.sh唯一的作用就是拉起二进制程序demo;demo是简单的hello_world程序,使用android-ndk-r18 toolchain编译而成。
开启ADB功能
- 默认的evb7_dtsi关闭了USB 3.0 host,因此无法通过Type-C接口连接到本地设备设备。通过修改
kernel-5.10/arch/arm64/boot/dts/rockchip/rk3588-evb7-v11.dtsi
文件使能adb。&usbhost3_0 { - status = "disabled"; + status = "okay"; };&usbhost_dwc3_0 { - status = "disabled"; + status = "okay"; };
添加Android native层服务
在系统添加执行目录
- 在
device/rockchip/common/
添加your/bin/
,将可执行程序demo
和对应的启动脚本run.sh
放在下面。 run.sh
代码如下。#! /system/bin/sh/vendor/app/your/bin/demo
- 修改
device/rockchip/common/device.mk
。添加如下代码。Andorid 8.0以上,新建应用须放在/vendor目录。PRODUCT_COPY_FILES += \device/rockchip/common/your/bin/demo:vendor/app/your/bin/demo:mode=755 \device/rockchip/common/your/bin/run.sh:vendor/app/your/bin/run.sh:mode=755
- 烧录系统后,将出现/vendor/app/your目录,以及目录下bin/的可执行文件。
在只读分区修改文件属性
- vondor分区为只读分区,修改文件属性只能通过修改config.fs方式达成。
- 添加
device/rockchip/common/config.fs
文件。文件内容如下。[vendor/app/your/bin/run.sh] mode: 0755 user: AID_SYSTEM group: AID_SYSTEM caps: WAKE_ALARM[vendor/app/your/bin/demo] mode: 0755 user: AID_SYSTEM group: AID_SYSTEM caps: WAKE_ALARM
- 在
device/rockchip/common/BoardConfig.mk
添加如下代码。TARGET_FS_CONFIG_GEN := device/rockchip/common/config.fs
添加Native层服务,并随开机自启动
-
修改
device/rockchip/common/rootdir/init.rockchip.rc
,在文件末尾添加如下代码。service demo /vendor/app/your/bin/run.shclass mainuser rootgroup rootoneshoton property:sys.boot_completed=1start demo
-
配置SELinux权限
- 修改
device/rockchip/common/sepolicy/vendor/file_contexts
,在末尾添加下面的代码。注意file_contexts最后一行一定是空行,否则编译不过!/vendor/app/your/bin/run.sh u:object_r:demo_exec:s0 /vendor/app/your/bin/demo u:object_r:demo_exec:s0
- 新建
device/rockchip/common/sepolicy/vendor/demo.te
,添加代码。type demo_domain, domain; type demo_exec, exec_type, vendor_file_type, file_type; type demo_domain_exec, exec_type, vendor_file_type, file_type;init_daemon_domain(demo_domain)allow demo_domain demo_exec:file { getattr open read execute map execute_no_trans }; allow demo_domain demo_domain_exec:file { entrypoint open read execute getattr map }; allow demo_domain postinstall_mnt_dir:dir { getattr }; allow demo_domain system_data_root_file:dir { read };allow init demo_domain_exec:file { getattr open read execute map }; allow init demo_domain:process transition;allow shell demo_exec:file { execute read open execute_no_trans map getattr }; allow shell postinstall_mnt_dir:dir { getattr };allow init-insmod-sh demo_exec:file { execute_no_trans };
- 在
device/rockchip/common/sepolicy/vendor/init-insmod-sh.te
添加如下代码。allow init-insmod-sh postinstall_mnt_dir:dir { getattr }; allow init-insmod-sh system_data_root_file:dir { read };
- 修改
-
验证服务是否开启
- 烧录后,通过
pgrep demo
查看进程PID。进一步的,可以通过ps -ef | grep demo
查看进程细节内容。 - 如果没有,通过
logcat | grep -E 'run.sh|demo'
查看日志,如出现avc denied
字样,需根据对应日志修改demo.te
。
- 烧录后,通过
通过audit2allow工具自动生成政策文件
- audit2allow是一个非常有用的工具,可以帮助你自动生成SELinux策略语句。
- 安装audit2allow。
sudo apt install policycoreutils
- 抓取和权限相关的log指令,并重定向保存至文件(例如:avcTest.txt):
adb logcat -b all | grep "avc" > ./avcTest.txt
- 将保存相关的log的文件复制到ubuntu里面,使用命令:
audit2allow -i avcTest.txt -o avc.te