Android4.0Sd卡移植之使用vold自动挂载sd卡

时间:2021-09-11 14:22:14

在cap631平台上移植android4.0,发现内核驱动没有任何问题,能够读写,当总不能挂载。

后来发现是因为自动挂载需要vold的支持。vold程序负责检查内核的 sysfs 文件系统,发现有SD卡插入后,自动挂载。 在文件系统目录 /system/etc/  下有一个vold.fstab 文件,该文件是系统与用户硬件平台的交互接口,用户根据自己的平台来配置这个文件,里面内容比较简单,只需要你指定sys下vold程序需要查询的文件夹路径,当SD卡插入设备后,这个路径下会产生相应的文件,vold能检查到。 在system/core/rootdir/etc/目录下有一个文件叫做vold.fstab,这个文件是一个教我们写vold.fstab的模板。模板内容如下:

## Vold 2.0 Generic fstab

## - San Mehat (san@android.com)

##

#######################

## Regular device mount

##

## Format: dev_mount <label><mount_point> <part> <sysfs_path1...>

## label        - Label for the volume

## mount_point  - Where the volume will be mounted

## part         - Partition # (1 based), or 'auto' forfirst usable partition.

## <sysfs_path> - List of sysfs pathsto source devices

######################

## Example of a standard sdcard mount forthe emulator / Dream

# Mounts the first usable partition of thespecified device

dev_mount sdcard/mnt/sdcard auto /devices/platform/goldfish_mmc.0/devices/platform/msm_sdcc.2/mmc_host/mmc1

## Example of a dual card setup

# dev_mount left_sdcard  /sdcard1 auto /devices/platform/goldfish_mmc.0/devices/platform/msm_sdcc.2/mmc_host/mmc1

# dev_mount right_sdcard /sdcard2  auto /devices/platform/goldfish_mmc.1/devices/platform/msm_sdcc.3/mmc_host/mmc1

## Example of specifying a specificpartition for mounts

# dev_mount sdcard /sdcard 2/devices/platform/goldfish_mmc.0 /devices/platform/msm_sdcc.2/mmc_host/mmc1

关键的一句话就是用红笔标出的这句话。其展示了vold.fstab的模板:

dev_mount       <label>     <mount_point>     <part>       <sysfs_path1...>

dev_mount:命令

<label> :标签

<mount_point>: 挂载点

<part> :子分区

<sysfs_path1...>:设备在sysfs文件系统下的路径(可多个)

注意:这里的各个参数之间要么全部用空格键隔开,要么都用tab键给隔开,因为他们在解析时是按照这种方法来解析的。

在/system/vold的目录下有一个main.cpp文件,里面的static
intprocess_config(VolumeManager *vm) 函数指出了在系统启动时,vold的会去那个目录下查询sd卡的挂载点,从函数我们看出是在这里:/etc/vold.fstab。

static intprocess_config(VolumeManager *vm) {

FILE *fp;

int n = 0;

char line[255];

if (!(fp =fopen("/etc/vold.fstab", "r"))) {

return -1;

}

while(fgets(line, sizeof(line), fp)) {

const char *delim = " \t";

char *save_ptr;

char *type, *label, *mount_point,*mount_flags, *sysfs_path;

int flags;

...................................................

}

我们又从\system\core\rootdir目录下的init.rc中发现:

# Backwardcompatibility

symlink /system/etc /etc

symlink /sys/kernel/debug /d

/etc目录已经被符号链接到了/system/etc,所以我们只要把vold.fstab配置文件发到/system/etc目录下就行了。但是如何在最后生成的system.img镜像中包含vold.fstab,即android系统的/system/etc目录下会有vold.fstab文件呢?答案是要把我们配置好的vold.fstab文件发到此目录下:device\cyit\cap631(这个目录因平台的差异会不同,个人根据自己的实际情况找到就行了,实在不行可以通过find命令找到vold.fstab目录)然后参照上面提到的模板去配置此文件,我所使用的平台下是红色部分:

# Copyright (c) 2011, Code Aurora Forum. Allrights reserved.

#

# Redistribution and use in source and binaryforms, with or without

# modification, are permitted provided that thefollowing conditions are

# met:

#     *Redistributions of source code must retain the above copyright

#      notice, this list of conditions and the following disclaimer.

#     *Redistributions in binary form must reproduce the above

#      copyright notice, this list of conditions and the following

#      disclaimer in the documentation and/or other materials provided

#      with the distribution.

#     *Neither the name of Code Aurora Forum, Inc. nor the names of its

#      contributors may be used to endorse or promote products derived

#      from this software without specific prior written permission.

#

# THIS SOFTWARE IS PROVIDED "AS IS"AND ANY EXPRESS OR IMPLIED

# WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THEIMPLIED WARRANTIES OF

# MERCHANTABILITY, FITNESS FOR A PARTICULARPURPOSE AND NON-INFRINGEMENT

# ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER ORCONTRIBUTORS

# BE LIABLE FOR ANY DIRECT, INDIRECT,INCIDENTAL, SPECIAL, EXEMPLARY, OR

# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOTLIMITED TO, PROCUREMENT OF

# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,DATA, OR PROFITS; OR

# BUSINESS INTERRUPTION) HOWEVER CAUSED AND ONANY THEORY OF LIABILITY,

# WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING NEGLIGENCE

# OR OTHERWISE) ARISING IN ANY WAY OUT OF THEUSE OF THIS SOFTWARE, EVEN

# IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

dev_mount sdcard /mnt/sdcard auto/devices/platform/c6310-sd.1/mmc_host/mmc0

此处提醒下:

如果你把sd卡插入开发板上,在/dev/block/ 目录下面多出几个设备节点,证明sd卡的驱动成功加载,我的目录下面会形成mmcblk0  和 mmcblk0p1 节点,注意:这两个节点的意思,mmcblk0代表第一个sd卡设备,mmcblk0p1代表第一个SD卡设备的第一个分区。用户不能把/dev/block/mmcblk0挂载到文件系统中,而是把这个SD卡的分区挂载到文件系统中,如下:

# mount -t vfat  /dev/block/mmcblk0     /sdcard/

<3>FAT: utf8 is not a recommended IO charset for FAT filesystems,filesystem wil

l be case sensitive!

FAT: utf8 is not a recommended IO charset for FAT filesystems, filesystem willb

e case sensitive!

<7>mmc0: starting CMD17 arg 00000000 flags 000000b5

<7>mmc0:     blksz 512 blocks 1 flags 00000200 tsac 100 msnsac 0

<7>sdhci [sdhci_irq()]: *** mmc0 got interrupt: 0x00000001

<7>sdhci [sdhci_irq()]: *** mmc0 got interrupt: 0x00000002

<7>mmc0: req done (CMD17): 0: 00000900 00000000 00000000 00000000

<7>mmc0:     512 bytes transferred:0

mount: Invalid argument

挂载 mmcblk0  会出现这样的错误提示,而挂载 mmcblk0p1 会成功,当然在android下面不应该手动挂载,否则在上层应用软件中会出现找不到SD卡的情况.

写好了文件我们还要去在AndroidBoard.mk中去配置它:

file :=$(TARGET_OUT)/etc/vold.fstab

ALL_PREBUILT+= $(file)

$(file): $(LOCAL_PATH)/vold.fstab | $(ACP)

$(transform-prebuilt-to-target)

具体如何配置根据自己的平台。

最后我们需要重新make一下,注意是make,不是make
xxx,make完以后我们会在\out\target\product\cap631\system\etc目录下发现一个vold.fstab文件,这就说明在system.img里会包含这个文件了。接下来,我们把make好的镜像烧到手机板子上,通过adb
shell进入系统,ls /systyem/etc 会发现我们的vold.fstab文件,这样我们就可以了。