Android 4.4 KitKat 支持 u 盘功能

Android U 盘功能实现和分析


u 盘功能实现结果:

Android 4.4 KitKat 支持 u 盘功能

u 盘会当成 usb storage 在 Settings Storage 里面显示。


准备工作

内核需支持 usb host,需支持 FUSE 文件系统,在 FS 选项里面配置即可(CONFIG_FUSE_FS=y)。


Android 的配置

配置 init.<board>.rc

  1. mkdir /storage/udisk0 0000 system system  

  2. mkdir /mnt/media_rw/udisk0 0700 media_rw media_rw  


  1. service fuse_udisk0 /system/bin/sdcard -u 1023 -g 1023 -w 1023 -d /mnt/media_rw/udisk0 /storage/udisk0  

  2.     class late_start  

  3.     disabled  


配置 fstab.<board>

  1. /block/sda      /mnt/media_rw/udisk0  vfat  defaults  voldmanaged=udisk0:auto  


配置 overlay

  1. <storage android:mountPoint="/storage/udisk0"  

  2.           android:storageDescription="@string/storage_usb"  

  3.           android:primary="false"  

  4.           android:removable="true" />  


挂载过程:

  1. /dev/block/vold/8:1 /mnt/media_rw/udisk0 vfat rw,dirsync,nosuid,nodev,noexec,relatime,uid=1023,gid=1023,fmask=0007,dmask=0007,allow_utime=0020,codepage=437,iocharset=iso8859-1,shortname=mixed,utf8,errors=remount-ro 0 0  

  2. /dev/fuse /storage/udisk0 fuse rw,nosuid,nodev,relatime,user_id=1023,group_id=1023,default_permissions,allow_other 0 0  


  1. root@android :/ # logcat -s Vold MountService  

  2. --------- beginning of /dev/log/main  

  3. --------- beginning of /dev/log/system  

  4. I/Vold    (  820): Vold 2.1 (the revenge) firing up  

  5. D/Vold    (  820): Volume sdcard1 state changing -1 (Initializing) -> 0 (No-Media)  

  6. D/Vold    (  820): Volume sdcard1 state changing -1 (Initializing) -> 0 (No-Media)  

  7. D/Vold    (  820): Volume udisk0 state changing -1 (Initializing) -> 0 (No-Media)  

  8. D/Vold    (  820): Volume udisk0 state changing 0 (No-Media) -> 2 (Pending)  

  9. D/Vold    (  820): Volume udisk0 state changing 2 (Pending) -> 1 (Idle-Unmounted)  

  10. D/Vold    (  820): Volume sdcard1 state changing 0 (No-Media) -> 2 (Pending)  

  11. D/Vold    (  820): Volume sdcard1 state changing 2 (Pending) -> 1 (Idle-Unmounted)  

  12. D/MountService( 1587): got storage path: /storage/sdcard0 description: Internal storage primary: true removable: false emulated: true mtpReserve: 50 allowMassStorage: false maxFileSize: 0  

  13. D/MountService( 1587): addVolumeLocked() StorageVolume:  

  14. D/MountService( 1587):     mStorageId=65537 mPath=/storage/emulated/0 mDescriptionId=17040662   

  15. D/MountService( 1587):     mPrimary=true mRemovable=false mEmulated=true mMtpReserveSpace=50   

  16. D/MountService( 1587):     mAllowMassStorage=false mMaxFileSize=0 mOwner=UserHandle{0} mUuid=null   

  17. D/MountService( 1587):     mUserLabel=null mState=null   

  18. D/MountService( 1587): got storage path: /storage/sdcard1 description: SD card primary: false removable: true emulated: false mtpReserve: 0 allowMassStorage: false maxFileSize: 4294967296  

  19. D/MountService( 1587): addVolumeLocked() StorageVolume:  

  20. D/MountService( 1587):     mStorageId=0 mPath=/storage/sdcard1 mDescriptionId=17040663 mPrimary=false   

  21. D/MountService( 1587):     mRemovable=true mEmulated=false mMtpReserveSpace=0 mAllowMassStorage=false   

  22. D/MountService( 1587):     mMaxFileSize=4294967296 mOwner=null mUuid=null mUserLabel=null mState=null   

  23. D/MountService( 1587): got storage path: /storage/udisk0 description: USB storage primary: false removable: true emulated: false mtpReserve: 0 allowMassStorage: false maxFileSize: 0  

  24. D/MountService( 1587): addVolumeLocked() StorageVolume:  

  25. D/MountService( 1587):     mStorageId=0 mPath=/storage/udisk0 mDescriptionId=17040664 mPrimary=false   

  26. D/MountService( 1587):     mRemovable=true mEmulated=false mMtpReserveSpace=0 mAllowMassStorage=false   

  27. D/MountService( 1587):     mMaxFileSize=0 mOwner=null mUuid=null mUserLabel=null mState=null   

  28. W/MountService( 1587): Duplicate state transition (unmounted -> unmounted) for /storage/sdcard1  

  29. D/MountService( 1587): volume state changed for /storage/sdcard1 (unmounted -> removed)  

  30. W/MountService( 1587): Duplicate state transition (unmounted -> unmounted) for /storage/udisk0  

  31. I/Vold    (  820): /dev/block/vold/8:1 being considered for volume udisk0  

  32. D/Vold    (  820): Volume udisk0 state changing 1 (Idle-Unmounted) -> 3 (Checking)  

  33. D/MountService( 1587): volume state changed for /storage/udisk0 (unmounted -> checking)  

  34. D/MountService( 1587): sendStorageIntent Intent { act=android.intent.action.MEDIA_CHECKING dat=file:///storage/udisk0 (has extras) } to UserHandle{-1}  

  35. I/Vold    (  820): Filesystem check completed OK  

  36. D/Vold    (  820): blkid identified as /dev/block/vold/8:1: UUID="402E-0EE3" TYPE="vfat"   

  37. D/Vold    (  820): Volume udisk0 state changing 3 (Checking) -> 4 (Mounted)  

  38. D/MountService( 1587): volume state changed for /storage/udisk0 (checking -> mounted)  

  39. D/MountService( 1587): sendStorageIntent Intent { act=android.intent.action.MEDIA_MOUNTED dat=file:///storage/udisk0 (has extras) } to UserHandle{-1}  

  40. W/MountService( 1587): Duplicate state transition (mounted -> mounted) for /storage/emulated/0  


Vold 解析 fstab 文件

在 Android 4.4 中, Vold 会解析 vold.<hardware> 文件

  1. property_get("ro.hardware", propbuf, "");  

  2.  snprintf(fstab_filename, sizeof(fstab_filename), FSTAB_PREFIX"%s", propbuf);  

  3.   

  4.  fstab = fs_mgr_read_fstab(fstab_filename);  

  5.  if (!fstab) {  

  6.      SLOGE("failed to open %s\n", fstab_filename);  

  7.      return -1;  

  8.  }  


其中 #define FSTAB_PREFIX "/fstab."  所以,配置的 fatab 文件必须放在根目录, Vold 是 android 的后台进程,将会一直监听 fatab 里面标记为 voldmanaged 的项目

如果没有标记,则略过。没有标记的项目。可以交给 init 的 mount 命令处理,但是不会监听,所以你可以将recovery 和 cache 目录这样只需启动是挂载的目录项放在 fatab

里面交给 init 的 mount 命令处理,例如三星 device/samsung/manta/fstab.manta 

  1. # Android fstab file.  

  2. #<src>                                                  <mnt_point>         <type>    <mnt_flags and opt  

  3. ions>                       <fs_mgr_flags>  

  4. # The filesystem that contains the filesystem checker binary (typically /system) cannot  

  5. # specify MF_CHECK, and must come before any filesystems that do specify MF_CHECK  

  6.   

  7.   

  8. /dev/block/platform/dw_mmc.0/by-name/system     /system             ext4      ro  

  9.                             wait  

  10. /dev/block/platform/dw_mmc.0/by-name/cache      /cache              ext4      noatime,nosuid,nodev,nombl  

  11. k_io_submit,errors=panic    wait,check  

  12. /dev/block/platform/dw_mmc.0/by-name/userdata   /data               ext4      noatime,nosuid,nodev,nombl  

  13. k_io_submit,errors=panic    wait,check,encryptable=/dev/block/platform/dw_mmc.0/by-name/metadata  

  14. /dev/block/platform/dw_mmc.0/by-name/efs        /factory            ext4      noatime,nosuid,nodev,ro  

  15.                             wait  

  16. /dev/block/platform/dw_mmc.0/by-name/boot       /boot               emmc      defaults  

  17.                             defaults  

  18. /dev/block/platform/dw_mmc.0/by-name/recovery   /recovery           emmc      defaults  

  19.                             defaults  

  20. /dev/block/platform/dw_mmc.0/by-name/misc       /misc               emmc      defaults  

  21.                             defaults  

  22. /dev/block/mmcblk0boot0                         /bootloader         emmc      defaults  

  23.                             defaults  


在系统启动调用 mount :

  1. on fs  

  2.     mkdir /factory 0775 radio radio  

  3.   

  4.     mount_all /fstab.manta  


同理, init 的 mount 也会忽略标记为 voldmanaged 的项目

init 的 mount 命令与 vold 的 mount 管理大有不同,两者不能混用。


MountService 与 Vold 联合挂载 u 盘

MountService 与 Vold 会建立 socket 通信,具体的挂载操作都是下发命令,交给 Vold 去完成。 MountService 会解析我们配置的 storage_list.xml 

  1. private void readStorageListLocked() {  

  2.      mVolumes.clear();  

  3.      mVolumeStates.clear();  

  4.   

  5.      Resources resources = mContext.getResources();  

  6.   

  7.      int id = com.android.internal.R.xml.storage_list;  

  8.      XmlResourceParser parser = resources.getXml(id);  

  9.      AttributeSet attrs = Xml.asAttributeSet(parser);  

  10.   

  11.      try {  

  12.          XmlUtils.beginDocument(parser, TAG_STORAGE_LIST);  

  13.          while (true) {  

  14.              XmlUtils.nextElement(parser);  


Vold 获取到 list 里面的设备变化时将会发送 state change event, MountService 在 onEvent 处理各种不同 state ,其中获取到设备插入,将会执行挂载操作:

  1. if (code == VoldResponseCode.VolumeDiskInserted) {  

  2.     new Thread("MountService#VolumeDiskInserted") {  

  3.         @Override  

  4.         public void run() {  

  5.             try {  

  6.                 int rc;  

  7.                 if ((rc = doMountVolume(path)) != StorageResultCode.OperationSucceeded) {  

  8.                     Slog.w(TAG, String.format("Insertion mount failed (%d)", rc));  

  9.                 }  

  10.             } catch (Exception ex) {  

  11.                 Slog.w(TAG, "Failed to mount media on insertion", ex);  

  12.             }  

  13.         }  

  14.     }.start();  


doMountVolume  只是发送 mount 命令给 Vold 

  1. private int doMountVolume(String path) {  

  2.      int rc = StorageResultCode.OperationSucceeded;  

  3.   

  4.      final StorageVolume volume;  

  5.      synchronized (mVolumesLock) {  

  6.          volume = mVolumesByPath.get(path);  

  7.      }  

  8.   

  9.      if (DEBUG_EVENTS) Slog.i(TAG, "doMountVolume: Mouting " + path);  

  10.      try {  

  11.          mConnector.execute("volume""mount", path);  


最终 Vold 里面执行相应的 mount 操作,我们的 u 盘是 vfat 格式,我只测试了这个格式, Linux 下的格式没有测试,因为 Vold 只支持 fat 和 ext4

  1. int VolumeManager::mountVolume(const char *label) {  

  2.     Volume *v = lookupVolume(label);  

  3.   

  4.     if (!v) {  

  5.         errno = ENOENT;  

  6.         return -1;  

  7.     }  

  8.   

  9.     return v->mountVol();  

  10. }  


  1. if (Fat::doMount(devicePath, getMountpoint(), falsefalsefalse,  

  2.          AID_MEDIA_RW, AID_MEDIA_RW, 0007true)) {  

  3.      SLOGE("%s failed to mount via VFAT (%s)\n", devicePath, strerror(errno));  

  4.      continue;  



转载于:https://my.oschina.net/u/994235/blog/376799