小工具知识网

当前位置: 首页 > 行业知识 > RK3588 Android 12 系统内核开发+Native层脚本自启动+SELinux配置

行业知识

RK3588 Android 12 系统内核开发+Native层脚本自启动+SELinux配置

2026-11-15 17:28:31 来源:互联网转载

前言

  • 开发板型号: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: 0755user: AID_SYSTEMgroup: AID_SYSTEMcaps: WAKE_ALARM[vendor/app/your/bin/demo]mode: 0755user: AID_SYSTEMgroup: AID_SYSTEMcaps: 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.sh    class main    user root    group root    oneshoton property:sys.boot_completed=1    start 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

上一篇:裁决战歌怎么换服务器?

下一篇:揭秘OBG,一网打尽,秒懂其含义!