最近在做项目的过程中,遇到了这么一个需求,需要在Android(4.2.2)启动时候执行以下命令,命令的具体含义不再解释:


  1. mount -t usbfs none /proc/bus/usb


最初做法是将其加入init.rc文件中,但是发现此方法行不通,原因到现在也未查明,希望知道原因的朋友能够留言告知,不胜感激o(∩_∩)o 。

接着查阅相关资料,发现将命令写入一个sh文件中,之后在开机的时候执行该sh文件,同样能够达到效果,于是新建了一个sh文件usbfs.sh,内容如下:

1. #! /system/bin/sh
2. 
3.  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文件中,当然这个也是不同平台略有差异,打开该文件,我们就能看到以下内容了(摘抄):

1. #
2. # Copyright (C) 2011 The Android Open-Source Project
3. #
4. # Licensed under the Apache License, Version 2.0 (the "License");
5. # you may not use this file except in compliance with the License.
6. # You may obtain a copy of the License at
7. #
8. #      http://www.apache.org/licenses/LICENSE-2.0
9. #
10. # Unless required by applicable law or agreed to in writing, software
11. # distributed under the License is distributed on an "AS IS" BASIS,
12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13. # See the License for the specific language governing permissions and
14. # limitations under the License.
15. #
16.  PRODUCT_AAPT_PREF_CONFIG := xhdpi
17. 
18.  PRODUCT_CHARACTERISTICS := tablet
19. 
20.  PRODUCT_COPY_FILES := \
21.      frameworks/native/data/etc/android.hardware.wifi.xml:system/etc/permissions/android.hardware.wifi.xml \
22.      frameworks/native/data/etc/android.hardware.wifi.direct.xml:system/etc/permissions/android.hardware.wifi.direct.xml \
23.      frameworks/native/data/etc/android.hardware.usb.accessory.xml:system/etc/permissions/android.hardware.usb.accessory.xml
24. 
25.  PRODUCT_COPY_FILES += \
26.      device/hisilicon/bigfish/etc/init.rc:root/init.rc \
27.      device/hisilicon/bigfish/etc/init.hidolphin.rc:root/init.hidolphin.rc \
28.      device/hisilicon/bigfish/etc/ueventd.bigfish.rc:root/ueventd.bigfish.rc \
29.      device/hisilicon/bigfish/etc/media_codecs.xml:/system/etc/media_codecs.xml \
30.      device/hisilicon/bigfish/etc/media_profiles.xml:/system/etc/media_profiles.xml \
31.      device/hisilicon/bigfish/etc/tablet_core_hardware.xml:system/etc/permissions/tablet_core_hardware.xml \
32.      device/hisilicon/Hi3716CV200/etc/init.Hi3716CV200.rc:root/init.bigfish.rc \
33.      device/hisilicon/Hi3716CV200/etc/init.Hi3716CV200.sh:system/etc/init.bigfish.sh


我们需要做的,就是将以下内容添加到上述文件合适的位置:


  1. PRODUCT_COPY_FILES += \
  2.         device/hisilicon/bigfish/etc/usbfs.sh:system/etc/usbfs.sh


“:”前面是文件源路径,后面的是目的路径。

这样,Android在执行编译的时候就会把新增文件拷贝至相应的目标路径去了,拷贝动作已经实现。

 

二、添加启动动作,使Android在启动时候执行。

 

如何添加启动动作?大部分人或许都知道,那就是在init.rc文件中添加上就是了,网上也有一些此类的介绍,我是这么做的:

init.rc文件末尾处加入以下内容(不再详述,不懂的自己翻书查)

1.  service mount-usbfs /system/etc/usbfs.sh
2. class
3.      user root
4.      group root
5.      oneshot


之后编译系统,烧写,启动,观察启动log,发现确实执行了该sh文件,但是却报了一个“权限不足”的提示,ll了一下usbfs.sh文件,发现权限是644,没有执行权限。

OK,没有执行权限,给他添加上执行权限就是了,同样是在init.rc文件中,添加以下内容:

1.  chown root shell /system/etc/usbfs.sh
2.  chmod 0550 /system/etc/usbfs.sh


添加了执行的权限,这次应该没有问题了吧,网上很多介绍也是这么干的。

之后又是编译、烧写、启动...漫长的过程(┬_┬),Android系统级开发,要有耐心啊。

观察启动的log,居然还是“权限不足”!!!怎么回事,难道没有生效?ll了一下usbfs.sh文件,发现权限居然还是644,说明刚才的赋权限是没有效果的。

为什么?接着查,在查看init.rc的过程中,发现了以下内容:

1.  mount ext4 ext4@system /system ro


原来system分区是以只读的形式进行挂载的,忽略这点了。以只读形式挂载,再怎么赋权限,也是徒劳啊。

突然又发现,与usbfs.sh在同一个目录的init.bigfish.sh,权限是正常的,并且该文件的源文件与sh文件同样都在一个目录里。那么,这说明Android在编译过程中,除了拷贝以外,应该还有一个赋权限的动作。

基于以上思路,继续进行查找。功夫不负有心人,还真被我找到了,确实存在这么一个动作,它在哪里呢?

在这里:system/core/include/private/android_filesystem_config.h,其中有个结构体做如下定义:


1. /* Rules for files.
2. ** These rules are applied based on "first match", so they
3. ** should start with the most specific path and work their
4. ** way up to the root. Prefixes ending in * denotes wildcard
5. ** and will allow partial matches.
6. */
7. static struct
8. "system/etc/init.goldfish.rc"
9. "system/etc/init.goldfish.sh"
10. "system/etc/init.bigfish.sh"
11. "system/etc/init.trout.rc"
12. "system/etc/init.ril"
13. "system/etc/init.testmenu"
14. "system/etc/dhcpcd/dhcpcd-run-hooks"
15. "system/etc/dhclient*"
16. "system/etc/dbus.conf"
17. "system/etc/AudioPara4.csv"
18. "system/etc/ppp/*"
19. "system/etc/rc.*"
20. "data/app/*"
21. "data/media/*"
22. "data/app-private/*"
23. "data/data/*"
24.          /* the following two files are INTENTIONALLY set-gid and not set-uid.
25.           * Do not change. */
26. "system/bin/ping"
27. "system/bin/netcfg" }, //modified by lwf 20141008,for resolve a bug when DHCP enable.
28. //called by BrowserEthernetClientX::submitParameters().
29. /* the following five files are INTENTIONALLY set-uid, but they
30.      * are NOT included on user builds. */
31. "system/xbin/su"
32. "system/xbin/librank"
33. "system/xbin/procrank"
34. "system/xbin/procmem"
35. "system/xbin/tcpdump"
36. "system/bin/pppd-ril"
37. /* the following file is INTENTIONALLY set-uid, and IS included
38.          * in user builds. */
39. "system/bin/run-as"
40. "system/bin/*"
41. "system/lib/valgrind/*"
42. "system/xbin/*"
43. "system/vendor/bin/*"
44. "sbin/*"
45. "bin/*"
46. "init*"
47. "charger*"
48. "sbin/fs_mgr"
49. "fstab.*"
50.      { 00644, AID_ROOT,      AID_ROOT,       0 },
51.  };


从行9可以看到,此处重新定义了init.bigfish.sh的权限,OK,我们应该只需要加入以下内容,就可以了:

1. "system/etc/usbfs.sh"


保存,编译,启动。。。果然可以了!

 

后话

 

个人感觉,我这种做法是比较遵循Android规则的一种方法。可能存在一些更加简单、直接的方法,我在爬网文过程中也看到过,比如更改system分区的挂载方式,由只读变为读写等等,但是这样的方法,你自己感觉合适吗?

当然,如果你有更好的方法,希望能够留言分享,不胜感激o(∩_∩)o 。