因业务需求,需要在MTK 6768开机启动过程中运行一个shell脚本,下面内容是我实现的方法:
一、源码中添加自己的shell脚本
--- /dev/null
+++ b/alps/device/mediatek/vendor/common/oad08open.sh
@@ -0,0 +1,3 @@
+#!/vendor/bin/sh
+echo "ZZQ Running i2cset command"
+i2cset -y 5 0x1d 0x52 0x07 b
二、脚本编译到系统中
--- a/alps/device/mediatek/vendor/common/device.mk
+++ b/alps/device/mediatek/vendor/common/device.mk
@@ -3503,6 +3503,9 @@ endif
# Enable modprobe on second stage by shell script
PRODUCT_COPY_FILES += device/mediatek/vendor/common/init.insmod.sh:$(TARGET_COPY_OUT_VENDOR)/bin/init.insmod.sh
+#OAD08 open shell
+PRODUCT_COPY_FILES += device/mediatek/vendor/common/oad08open.sh:$(TARGET_COPY_OUT_VENDOR)/bin/oad08open.sh
+
# Header libs for libvcodecdrv
PRODUCT_PACKAGES += libvcodecdrv_header_stub
三、添加SELinux 策略
--- a/alps/device/mediatek/sepolicy/basic/non_plat/file_contexts
+++ b/alps/device/mediatek/sepolicy/basic/non_plat/file_contexts
@@ -778,6 +778,9 @@
# Purpose: kernel modules
/vendor/bin/init\.insmod\.sh u:object_r:init_insmod_sh_exec:s0
+#OAD08 open shell
+/vendor/bin/oad08open\.sh u:object_r:oad08open_sh_exec:s0
+
# GMS requirement
/vendor/bin/chipinfo u:object_r:chipinfo_exec:s0
--- /dev/null
+++ b/alps/device/mediatek/sepolicy/basic/non_plat/oad08open_sh.te
@@ -0,0 +1,21 @@
+type oad08open_sh, domain;
+type oad08open_sh_exec, exec_type, vendor_file_type, file_type;
+
+init_daemon_domain(oad08open_sh)
+
+allow oad08open_sh vendor_toolbox_exec:file rx_file_perms;
+allow oad08open_sh self:capability sys_module;
+allow oad08open_sh vendor_file:system module_load;
+allow oad08open_sh kernel:key search;
+allow oad08open_sh device:chr_file { read };
+allow oad08open_sh device:chr_file { open };
+allow oad08open_sh device:chr_file { ioctl };
+
+
+# Date : WK20.46
+# Purpose : modprobe need proc_modules
+allow oad08open_sh proc_modules:file r_file_perms;
+
+# Date : WK20.46
+# Purpose : Set the vendor.all.modules.ready property
+set_prop(oad08open_sh, vendor_mtk_device_prop)
/*
allow oad08open_sh device:chr_file { read };
allow oad08open_sh device:chr_file { open };
allow oad08open_sh device:chr_file { ioctl };
/*
这部分根据自己脚本实际内容添加,可以通过启动日志,看缺少啥,添加啥
实例:
[425:logd.auditd]type=1400 audit(1725072881.804:63): avc: denied { ioctl } for comm="i2cset" path="/dev/i2c-5" dev="tmpfs" ino=16402 ioctlcmd=0x703 scontext=u:r:oad08open_sh:s0 tcontext=u:object_r:device:s0 tclass=chr_file permissive=0
进程:oad08open_sh 类型:device 类型类别:chr_file 缺少权限:ioctl
四、添加启动服务
--- a/alps/device/mediatek/mt6768/init.mt6768.rc
+++ b/alps/device/mediatek/mt6768/init.mt6768.rc
@@ -1060,3 +1060,12 @@ service osi /system/bin/osi
oneshot
#seclabel u:r:osi:s0
+on property:dev.bootcomplete=1
+ start oad08open
+
+service oad08open /vendor/bin/oad08open.sh
+ class main
+ user root
+ group root
+ oneshot
补充:
oad08open 指的是服务名,而后的路径则是shell执行文件在系统中的位置
class 指定服务的类名。命名类中的所有服务必须一起启动和停止。如果未通过class选项指定服务,则将其视为“默认”类。
user root 执行此服务前,请更改为用户名。当前默认为root
group root 执行此服务之前,请更改为groupname。除了第一个必需的其他组名外,还用于设置流程的其他组(带有setgroups())。当前默认为root。
oneshot 退出服务时不要重新启动它。当然如果你的程序是循环的则会一直执行
disabled 该服务不会自动从其类开始。必须通过名称显式启动(即 start)。