为Android添加开机启动脚本
2015-05-15 10:13
239 查看
摘要:
本文介绍了一种在Android 4.2.2源码中添加、修改文件或者代码,来达到使Android在启动时,执行位于/system/etc/目录下的shell脚本文件的方法。
由于平台不同,可能细节上多有差异,但是大体方式应该是相同的。
最近在做项目的过程中,遇到了这么一个需求,需要在Android(4.2.2)启动时候执行以下命令,命令的具体含义不再解释:
mount -t usbfs none /proc/bus/usb
最初做法是将其加入init.rc文件中,但是发现此方法行不通,原因到现在也未查明,希望知道原因的朋友能够留言告知,不胜感激o(∩_∩)o 。
接着查阅相关资料,发现将命令写入一个sh文件中,之后在开机的时候执行该sh文件,同样能够达到效果,于是新建了一个sh文件usbfs.sh,内容如下:
#! /system/bin/sh
mount -t usbfs none /proc/bus/usb
这里需要注意的是,操作该文件(包括新建、编辑)时尽量在Linux环境下,不要在Windows下。我开始做这步的时候是在Win下进行内容编译,之后将文件拷贝至Android源码相应位置的。结果编译完成,镜像烧写后发现死活都不执行,或者报错,cat了一下发现内容里有多余的字符。
接下来的操作,目的就很明确了。
第一、我们需要Android在编译的时候,将这个文件拷贝至输出目录相应的位置,并且最终添加到镜像中去。
第二、我们需要Android在启动的时候,执行这个sh文件。
围绕以上两点,我们开展下一步的工作。
新增的sh文件,我的存放路径为device/hisilicon/bigfish/etc/usbfs.sh。不同平台的话,文件路径可能略有差异,我的是海思平台的源码包。具体怎么定,按你自己的实际情况来。
出于安全考虑,我准备将该文件拷贝至Android system分区下的etc目录中,即目标地址为:/system/etc/。
OK,做好以上两点,接下来就是添加相应的拷贝动作了。这个动作需要自己添加的吗?当然,大部分情况下Android在编译的时候是不会自动添加你新增的文件的。动作在哪里添加的呢?
在这里----device/hisilicon/Hi3716CV200/device.mk文件中,当然这个也是不同平台略有差异,打开该文件,我们就能看到以下内容了(摘抄):
#
# Copyright (C) 2011 The Android Open-Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
PRODUCT_AAPT_PREF_CONFIG := xhdpi
PRODUCT_CHARACTERISTICS := tablet
PRODUCT_COPY_FILES := \
frameworks/native/data/etc/android.hardware.wifi.xml:system/etc/permissions/android.hardware.wifi.xml \
frameworks/native/data/etc/android.hardware.wifi.direct.xml:system/etc/permissions/android.hardware.wifi.direct.xml \
frameworks/native/data/etc/android.hardware.usb.accessory.xml:system/etc/permissions/android.hardware.usb.accessory.xml
PRODUCT_COPY_FILES += \
device/hisilicon/bigfish/etc/init.rc:root/init.rc \
device/hisilicon/bigfish/etc/init.hidolphin.rc:root/init.hidolphin.rc \
device/hisilicon/bigfish/etc/ueventd.bigfish.rc:root/ueventd.bigfish.rc \
device/hisilicon/bigfish/etc/media_codecs.xml:/system/etc/media_codecs.xml \
device/hisilicon/bigfish/etc/media_profiles.xml:/system/etc/media_profiles.xml \
device/hisilicon/bigfish/etc/tablet_core_hardware.xml:system/etc/permissions/tablet_core_hardware.xml \
device/hisilicon/Hi3716CV200/etc/init.Hi3716CV200.rc:root/init.bigfish.rc \
device/hisilicon/Hi3716CV200/etc/init.Hi3716CV200.sh:system/etc/init.bigfish.sh
我们需要做的,就是将以下内容添加到上述文件合适的位置:
PRODUCT_COPY_FILES += \
device/hisilicon/bigfish/etc/usbfs.sh:system/etc/usbfs.sh
“:”前面是文件源路径,后面的是目的路径。
这样,Android在执行编译的时候就会把新增文件拷贝至相应的目标路径去了,拷贝动作已经实现。
如何添加启动动作?大部分人或许都知道,那就是在init.rc文件中添加上就是了,网上也有一些此类的介绍,我是这么做的:
init.rc文件末尾处加入以下内容(不再详述,不懂的自己翻书或者爬网查)
service mount-usbfs /system/etc/usbfs.sh
class main
user root
group root
oneshot
之后编译系统,烧写,启动,观察启动log,发现确实执行了该sh文件,但是却报了一个“权限不足”的提示,ll了一下usbfs.sh文件,发现权限是644,没有执行权限。
OK,没有执行权限,给他添加上执行权限就是了,同样是在init.rc文件中,添加以下内容:
chown root shell /system/etc/usbfs.sh
chmod 0550 /system/etc/usbfs.sh
添加了执行的权限,这次应该没有问题了吧,网上很多介绍也是这么干的。
之后又是编译、烧写、启动...漫长的过程(┬_┬),Android系统级开发,要有耐心啊。
观察启动的log,居然还是“权限不足”!!!怎么回事,难道没有生效?ll了一下usbfs.sh文件,发现权限居然还是644,说明刚才的赋权限是没有效果的。
为什么?接着查,在查看init.rc的过程中,发现了以下内容:
mount ext4 ext4@system /system ro
原来system分区是以只读的形式进行挂载的,忽略这点了。以只读形式挂载,再怎么赋权限,也是徒劳啊。
突然又发现,与usbfs.sh在同一个目录的init.bigfish.sh,权限是正常的,并且该文件的源文件与sh文件同样都在一个目录里。那么,这说明Android在编译过程中,除了拷贝以外,应该还有一个赋权限的动作。
基于以上思路,继续进行查找。功夫不负有心人,还真被我找到了,确实存在这么一个动作,它在哪里呢?
在这里:system/core/include/private/android_filesystem_config.h,其中有个结构体做如下定义:
/* Rules for files.
** These rules are applied based on "first match", so they
** should start with the most specific path and work their
** way up to the root. Prefixes ending in * denotes wildcard
** and will allow partial matches.
*/
static struct fs_path_config android_files[] = {
{ 00440, AID_ROOT, AID_SHELL, "system/etc/init.goldfish.rc" },
{ 00550, AID_ROOT, AID_SHELL, "system/etc/init.goldfish.sh" },
{ 00550, AID_ROOT, AID_SHELL, "system/etc/init.bigfish.sh" },
{ 00440, AID_ROOT, AID_SHELL, "system/etc/init.trout.rc" },
{ 00550, AID_ROOT, AID_SHELL, "system/etc/init.ril" },
{ 00550, AID_ROOT, AID_SHELL, "system/etc/init.testmenu" },
{ 00550, AID_DHCP, AID_SHELL, "system/etc/dhcpcd/dhcpcd-run-hooks" },
{ 00550, AID_DHCP, AID_SHELL, "system/etc/dhclient*" },
{ 00440, AID_BLUETOOTH, AID_BLUETOOTH, "system/etc/dbus.conf" },
{ 00444, AID_RADIO, AID_AUDIO, "system/etc/AudioPara4.csv" },
{ 00555, AID_ROOT, AID_ROOT, "system/etc/ppp/*" },
{ 00555, AID_ROOT, AID_ROOT, "system/etc/rc.*" },
{ 00644, AID_SYSTEM, AID_SYSTEM, "data/app/*" },
{ 00644, AID_MEDIA_RW, AID_MEDIA_RW, "data/media/*" },
{ 00644, AID_SYSTEM, AID_SYSTEM, "data/app-private/*" },
{ 00644, AID_APP, AID_APP, "data/data/*" },
/* the following two files are INTENTIONALLY set-gid and not set-uid.
* Do not change. */
{ 02755, AID_ROOT, AID_NET_RAW, "system/bin/ping" },
{ 04750, AID_ROOT, AID_INET, "system/bin/netcfg" }, //modified by lwf 20141008,for resolve a bug when DHCP enable.
//called by BrowserEthernetClientX::submitParameters().
/* the following five files are INTENTIONALLY set-uid, but they
* are NOT included on user builds. */
{ 06755, AID_ROOT, AID_ROOT, "system/xbin/su" },
{ 06755, AID_ROOT, AID_ROOT, "system/xbin/librank" },
{ 06755, AID_ROOT, AID_ROOT, "system/xbin/procrank" },
{ 06755, AID_ROOT, AID_ROOT, "system/xbin/procmem" },
{ 06755, AID_ROOT, AID_ROOT, "system/xbin/tcpdump" },
{ 04770, AID_ROOT, AID_RADIO, "system/bin/pppd-ril" },
/* the following file is INTENTIONALLY set-uid, and IS included
* in user builds. */
{ 06750, AID_ROOT, AID_SHELL, "system/bin/run-as" },
{ 00755, AID_ROOT, AID_SHELL, "system/bin/*" },
{ 00755, AID_ROOT, AID_ROOT, "system/lib/valgrind/*" },
{ 00755, AID_ROOT, AID_SHELL, "system/xbin/*" },
{ 00755, AID_ROOT, AID_SHELL, "system/vendor/bin/*" },
{ 00750, AID_ROOT, AID_SHELL, "sbin/*" },
{ 00755, AID_ROOT, AID_ROOT, "bin/*" },
{ 00750, AID_ROOT, AID_SHELL, "init*" },
{ 00750, AID_ROOT, AID_SHELL, "charger*" },
{ 00750, AID_ROOT, AID_SHELL, "sbin/fs_mgr" },
{ 00640, AID_ROOT, AID_SHELL, "fstab.*" },
{ 00644, AID_ROOT, AID_ROOT, 0 },
};
从行9可以看到,此处重新定义了init.bigfish.sh的权限,OK,我们应该只需要加入以下内容,就可以了:
{ 00550, AID_ROOT, AID_SHELL, "system/etc/usbfs.sh" },
保存,编译,启动。。。果然可以了!
个人感觉,我这种做法是比较遵循Android规则的一种方法。可能存在一些更加简单、直接的方法,我在爬网文过程中也看到过,比如更改system分区的挂载方式,由只读变为读写等等,但是这样的方法,你自己感觉合适吗?
当然,如果你有更好的方法,希望能够留言分享,不胜感激o(∩_∩)o 。
本文介绍了一种在Android 4.2.2源码中添加、修改文件或者代码,来达到使Android在启动时,执行位于/system/etc/目录下的shell脚本文件的方法。
由于平台不同,可能细节上多有差异,但是大体方式应该是相同的。
最近在做项目的过程中,遇到了这么一个需求,需要在Android(4.2.2)启动时候执行以下命令,命令的具体含义不再解释:
mount -t usbfs none /proc/bus/usb
最初做法是将其加入init.rc文件中,但是发现此方法行不通,原因到现在也未查明,希望知道原因的朋友能够留言告知,不胜感激o(∩_∩)o 。
接着查阅相关资料,发现将命令写入一个sh文件中,之后在开机的时候执行该sh文件,同样能够达到效果,于是新建了一个sh文件usbfs.sh,内容如下:
#! /system/bin/sh
mount -t usbfs none /proc/bus/usb
这里需要注意的是,操作该文件(包括新建、编辑)时尽量在Linux环境下,不要在Windows下。我开始做这步的时候是在Win下进行内容编译,之后将文件拷贝至Android源码相应位置的。结果编译完成,镜像烧写后发现死活都不执行,或者报错,cat了一下发现内容里有多余的字符。
接下来的操作,目的就很明确了。
第一、我们需要Android在编译的时候,将这个文件拷贝至输出目录相应的位置,并且最终添加到镜像中去。
第二、我们需要Android在启动的时候,执行这个sh文件。
围绕以上两点,我们开展下一步的工作。
一、实现编译时执行对此文件的拷贝。
新增的sh文件,我的存放路径为device/hisilicon/bigfish/etc/usbfs.sh。不同平台的话,文件路径可能略有差异,我的是海思平台的源码包。具体怎么定,按你自己的实际情况来。
出于安全考虑,我准备将该文件拷贝至Android system分区下的etc目录中,即目标地址为:/system/etc/。
OK,做好以上两点,接下来就是添加相应的拷贝动作了。这个动作需要自己添加的吗?当然,大部分情况下Android在编译的时候是不会自动添加你新增的文件的。动作在哪里添加的呢?
在这里----device/hisilicon/Hi3716CV200/device.mk文件中,当然这个也是不同平台略有差异,打开该文件,我们就能看到以下内容了(摘抄):
#
# Copyright (C) 2011 The Android Open-Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
PRODUCT_AAPT_PREF_CONFIG := xhdpi
PRODUCT_CHARACTERISTICS := tablet
PRODUCT_COPY_FILES := \
frameworks/native/data/etc/android.hardware.wifi.xml:system/etc/permissions/android.hardware.wifi.xml \
frameworks/native/data/etc/android.hardware.wifi.direct.xml:system/etc/permissions/android.hardware.wifi.direct.xml \
frameworks/native/data/etc/android.hardware.usb.accessory.xml:system/etc/permissions/android.hardware.usb.accessory.xml
PRODUCT_COPY_FILES += \
device/hisilicon/bigfish/etc/init.rc:root/init.rc \
device/hisilicon/bigfish/etc/init.hidolphin.rc:root/init.hidolphin.rc \
device/hisilicon/bigfish/etc/ueventd.bigfish.rc:root/ueventd.bigfish.rc \
device/hisilicon/bigfish/etc/media_codecs.xml:/system/etc/media_codecs.xml \
device/hisilicon/bigfish/etc/media_profiles.xml:/system/etc/media_profiles.xml \
device/hisilicon/bigfish/etc/tablet_core_hardware.xml:system/etc/permissions/tablet_core_hardware.xml \
device/hisilicon/Hi3716CV200/etc/init.Hi3716CV200.rc:root/init.bigfish.rc \
device/hisilicon/Hi3716CV200/etc/init.Hi3716CV200.sh:system/etc/init.bigfish.sh
我们需要做的,就是将以下内容添加到上述文件合适的位置:
PRODUCT_COPY_FILES += \
device/hisilicon/bigfish/etc/usbfs.sh:system/etc/usbfs.sh
“:”前面是文件源路径,后面的是目的路径。
这样,Android在执行编译的时候就会把新增文件拷贝至相应的目标路径去了,拷贝动作已经实现。
二、添加启动动作,使Android在启动时候执行。
如何添加启动动作?大部分人或许都知道,那就是在init.rc文件中添加上就是了,网上也有一些此类的介绍,我是这么做的:
init.rc文件末尾处加入以下内容(不再详述,不懂的自己翻书或者爬网查)
service mount-usbfs /system/etc/usbfs.sh
class main
user root
group root
oneshot
之后编译系统,烧写,启动,观察启动log,发现确实执行了该sh文件,但是却报了一个“权限不足”的提示,ll了一下usbfs.sh文件,发现权限是644,没有执行权限。
OK,没有执行权限,给他添加上执行权限就是了,同样是在init.rc文件中,添加以下内容:
chown root shell /system/etc/usbfs.sh
chmod 0550 /system/etc/usbfs.sh
添加了执行的权限,这次应该没有问题了吧,网上很多介绍也是这么干的。
之后又是编译、烧写、启动...漫长的过程(┬_┬),Android系统级开发,要有耐心啊。
观察启动的log,居然还是“权限不足”!!!怎么回事,难道没有生效?ll了一下usbfs.sh文件,发现权限居然还是644,说明刚才的赋权限是没有效果的。
为什么?接着查,在查看init.rc的过程中,发现了以下内容:
mount ext4 ext4@system /system ro
原来system分区是以只读的形式进行挂载的,忽略这点了。以只读形式挂载,再怎么赋权限,也是徒劳啊。
突然又发现,与usbfs.sh在同一个目录的init.bigfish.sh,权限是正常的,并且该文件的源文件与sh文件同样都在一个目录里。那么,这说明Android在编译过程中,除了拷贝以外,应该还有一个赋权限的动作。
基于以上思路,继续进行查找。功夫不负有心人,还真被我找到了,确实存在这么一个动作,它在哪里呢?
在这里:system/core/include/private/android_filesystem_config.h,其中有个结构体做如下定义:
/* Rules for files.
** These rules are applied based on "first match", so they
** should start with the most specific path and work their
** way up to the root. Prefixes ending in * denotes wildcard
** and will allow partial matches.
*/
static struct fs_path_config android_files[] = {
{ 00440, AID_ROOT, AID_SHELL, "system/etc/init.goldfish.rc" },
{ 00550, AID_ROOT, AID_SHELL, "system/etc/init.goldfish.sh" },
{ 00550, AID_ROOT, AID_SHELL, "system/etc/init.bigfish.sh" },
{ 00440, AID_ROOT, AID_SHELL, "system/etc/init.trout.rc" },
{ 00550, AID_ROOT, AID_SHELL, "system/etc/init.ril" },
{ 00550, AID_ROOT, AID_SHELL, "system/etc/init.testmenu" },
{ 00550, AID_DHCP, AID_SHELL, "system/etc/dhcpcd/dhcpcd-run-hooks" },
{ 00550, AID_DHCP, AID_SHELL, "system/etc/dhclient*" },
{ 00440, AID_BLUETOOTH, AID_BLUETOOTH, "system/etc/dbus.conf" },
{ 00444, AID_RADIO, AID_AUDIO, "system/etc/AudioPara4.csv" },
{ 00555, AID_ROOT, AID_ROOT, "system/etc/ppp/*" },
{ 00555, AID_ROOT, AID_ROOT, "system/etc/rc.*" },
{ 00644, AID_SYSTEM, AID_SYSTEM, "data/app/*" },
{ 00644, AID_MEDIA_RW, AID_MEDIA_RW, "data/media/*" },
{ 00644, AID_SYSTEM, AID_SYSTEM, "data/app-private/*" },
{ 00644, AID_APP, AID_APP, "data/data/*" },
/* the following two files are INTENTIONALLY set-gid and not set-uid.
* Do not change. */
{ 02755, AID_ROOT, AID_NET_RAW, "system/bin/ping" },
{ 04750, AID_ROOT, AID_INET, "system/bin/netcfg" }, //modified by lwf 20141008,for resolve a bug when DHCP enable.
//called by BrowserEthernetClientX::submitParameters().
/* the following five files are INTENTIONALLY set-uid, but they
* are NOT included on user builds. */
{ 06755, AID_ROOT, AID_ROOT, "system/xbin/su" },
{ 06755, AID_ROOT, AID_ROOT, "system/xbin/librank" },
{ 06755, AID_ROOT, AID_ROOT, "system/xbin/procrank" },
{ 06755, AID_ROOT, AID_ROOT, "system/xbin/procmem" },
{ 06755, AID_ROOT, AID_ROOT, "system/xbin/tcpdump" },
{ 04770, AID_ROOT, AID_RADIO, "system/bin/pppd-ril" },
/* the following file is INTENTIONALLY set-uid, and IS included
* in user builds. */
{ 06750, AID_ROOT, AID_SHELL, "system/bin/run-as" },
{ 00755, AID_ROOT, AID_SHELL, "system/bin/*" },
{ 00755, AID_ROOT, AID_ROOT, "system/lib/valgrind/*" },
{ 00755, AID_ROOT, AID_SHELL, "system/xbin/*" },
{ 00755, AID_ROOT, AID_SHELL, "system/vendor/bin/*" },
{ 00750, AID_ROOT, AID_SHELL, "sbin/*" },
{ 00755, AID_ROOT, AID_ROOT, "bin/*" },
{ 00750, AID_ROOT, AID_SHELL, "init*" },
{ 00750, AID_ROOT, AID_SHELL, "charger*" },
{ 00750, AID_ROOT, AID_SHELL, "sbin/fs_mgr" },
{ 00640, AID_ROOT, AID_SHELL, "fstab.*" },
{ 00644, AID_ROOT, AID_ROOT, 0 },
};
从行9可以看到,此处重新定义了init.bigfish.sh的权限,OK,我们应该只需要加入以下内容,就可以了:
{ 00550, AID_ROOT, AID_SHELL, "system/etc/usbfs.sh" },
保存,编译,启动。。。果然可以了!
后话
个人感觉,我这种做法是比较遵循Android规则的一种方法。可能存在一些更加简单、直接的方法,我在爬网文过程中也看到过,比如更改system分区的挂载方式,由只读变为读写等等,但是这样的方法,你自己感觉合适吗?
当然,如果你有更好的方法,希望能够留言分享,不胜感激o(∩_∩)o 。
相关文章推荐
- (OK) Android 添加 开机启动 脚本
- 为Android添加开机启动脚本
- 为Android添加开机启动脚本
- Android 8.0 添加开机启动脚本
- 为Android添加开机启动脚本
- 为Android添加开机启动脚本
- (OK) 为Android添加开机启动脚本
- 为 Android 8.0 添加开机启动脚本
- 关于添加开机启动的脚本问题
- ubuntu添加开机启动脚本
- Ubuntu下添加开机启动脚本
- Android中添加启动脚本
- 【centos7】添加开机启动服务/脚本
- ubuntu 添加开机启动脚本
- 为Android添加一个开机完成后执行的脚本
- Android添加一个开机完成后执行的脚本
- Ubuntu下添加开机启动脚本
- Ubuntu下添加开机启动脚本
- Android系统启动时添加运行脚本
- Ubuntu下添加开机启动脚本