android之VOLD:staging目录作用与ASEC文件 -总结[通俗易懂]

android之VOLD:staging目录作用与ASEC文件 -总结[通俗易懂]/mnt/secure/staging来看一下mountVol代码 int Volume::mountVol() {    int rc = 0;    char errmsg[255];    const char *mountPath;         char devicePath[255];                sprintf(device

大家好,又见面了,我是你们的朋友全栈君。

  1. /mnt/secure/staging

    来看一下mountVol代码

     int Volume::mountVol() {

        int rc = 0;

        char errmsg[255];

        const char *mountPath;

     

            char devicePath[255];

            

            sprintf(devicePath, “/dev/block/vold/%d:%d”, MAJOR(mDevNodeIndex),

                    MINOR(mDevNodeIndex));//得到设备节点,:/dev/block/vold/8:1

         

            SLOGI(“%s being considered for volume %s …major : %d minor: %d\n”, devicePath, getLabel(),

             MAJOR(mDevNodeIndex),MINOR(mDevNodeIndex));

        

            errno = 0;

            setState(Volume::State_Checking);//设置状态为checking整型为3

        

            // TODO: find a way to read the filesystem ID

            bool isFatFs = true;

            bool isNtfsFS = true;

             //检查设备格式是否为Fat32

            if (Fat::check(devicePath)) {

                if (errno == ENODATA) {

                    SLOGW(“%s does not contain a FAT filesystem\n”, devicePath);

                    isFatFs = false;

                } else {

                  errno = EIO;

                  /* Badness – abort the mount */

                  SLOGE(“%s failed FS checks (%s)”, devicePath, strerror(errno));

                  setState(Volume::State_Idle);

                  return –1;

                }

            }

     

            //创建挂载目录

           // create mountpoint

            if (mkdir(getMountpoint(), 0755)) {

                if (errno != EEXIST) {

                    SLOGE(“Failed to create mountpoint %s (%s)”, getMountpoint(), strerror(errno));

                    return –1;

                }

            }

        

            /*

             * Mount the device on our internal staging mountpoint so we can

             * muck with it before exposing it to non priviledged users.

             */

            errno = 0;

            //如果为sdcard则挂载到/mnt/secure/staging,否则挂载到挂载点————1 为什么?

             if(!strcmp(getLabel(),“sdcard”))

                mountPath=“/mnt/secure/staging”;

            else

                mountPath=getMountpoint();

             //接下来就是不同格式不同的挂载,这里支持两种格式:fat32,Ntfs

            if ( isFatFs ) {

                if (Fat::doMount(devicePath,mountPath, falsefalse100010150702true)) {

                    SLOGE(“%s failed to mount via VFAT (%s)\n”, devicePath, strerror(errno));

                    

                    isFatFs = false;

                }

                isNtfsFS = false;

            }

            

            if ( isNtfsFS ) {

                if (Ntfs::doMount(devicePath, mountPath, true)) {

                    SLOGE(“%s failed to mount via NTFS (%s)\n”, devicePath, strerror(errno));

                    isNtfsFS = false;

                }

            }

        

            if ( !isFatFs && !isNtfsFS ) {

                // unsupported filesystem

                return –1;

            }

            

            SLOGI(“Device %s, target %s mounted @ /mnt/secure/staging”, devicePath, getMountpoint());

            

            

            if ( !strcmp(getLabel(), “sdcard”) ) {

                

                protectFromAutorunStupidity();

        

                if (createBindMounts()) {

    2.createBindMounts的作用有三点:

    1.确认android_secure目录存在;2.挂载tmpfs ,目的是把该目录变成一个虚拟的分区,达到隐藏android_secure目录的目的,(为什么tmpfs有这个作用,请查看linux tmpfs3)挂载/mnt/secure/staging/android_secure/mnt/secure/asec

     

                    SLOGE(“Failed to create bindmounts (%s)”, strerror(errno));

                    umount(“/mnt/secure/staging”);

                    setState(Volume::State_Idle);

                    return –1;

                }

            }

        

            /*—3.为什么现在才移到目标挂载目录?

             * Now that the bindmount trickery is done, atomically move the

             * whole subtree to expose it to non priviledged users.

             * 如果为sdcard则将/mnt/secure/staging 目录移动到挂载点,并将该目录unmount

             */

            if(!strcmp(getLabel(),“sdcard”)){

              if (doMoveMount(“/mnt/secure/staging”, getMountpoint(), false)) {

                  SLOGE(“Failed to move mount (%s)”, strerror(errno));

                  umount(“/mnt/secure/staging”);

                  setState(Volume::State_Idle);

                   return –1;

              }

           }

            setState(Volume::State_Mounted);//设置状态到MountService

            mCurrentlyMountedKdev = mDevNodeIndex;

                    

            return 0;

     

    1.VOLD:/mnt/secure/staging作用是什么?

    1)首先看android_secure的作用

    android的官方解释:

    vold: Stage the mounting of media to hide the ASEC imagefile() directory

      In order to protect the ‘/android_secure‘ directory on VFAT removable media
    from being mucked with by 3rd party applications on the device, we hide the
    directory with a read-only, zero-sized tmpfs mounted on-top. A reference to the
    hidden directory is kept by a bind-mount which is mounted at a location which
    only root can access.

    为了保护在VFAT可移动媒体上的/ android_secure目录,避免被在android设备上的第三方应用程序搞乱,我们隐藏一个只读的大小为零的tmpfs的目录安装在最上层。的参照隐藏目录保持绑定安装,安装在一个位置只有root可以访问


    Staging consists ofStaging 的步骤):
      1. Mount checked media at a secure location (/mnt/secure/staging)

                挂载被检查过的存储媒体(也就是SDcard)到一个安全的位置(到/mnt/secure/staging

      2. Ensure /android_secure exists on the media, (creating if it doesnt)

              确保“/ android_secure”在存在该存储媒体(也就是SDcard)上(如果它不存在的,就创建它)

      3. Bind-mount /mnt/secure/staging/android_secure -> /mnt/secure/asec
         (where only root can access it)

    通过Volume::createBindMounts()

             绑定挂载的存储媒体(也就是SDcard)/mnt/secure/staging/android_secure/mnt/secure/asec 也就是(ASEC imagefile

         (只有root可以访问它)

     

      4. Mount an RDONLY zero-sized tmpfs over /mnt/secure/staging/android_secure

          挂载一个RDONLY的,且零大小的tmpfs到/mnt/secure/staging/android_secure

    为什么要mount一个tempfs文件系统呢?因为tmpfs的优势:没有块设备,只存在内存,速度快

    也就是把该目录作为一个虚拟的分区(应为有了文件系统),后面就可以通过mount(SEC_STG_SECIMGDIR, SEC_ASECDIR, “”, MS_BIND, NULL)

       /mnt/secure/staging/android_secure/mnt/secure/asec 

     

    5. Atomically move /mnt/secure/staging to the publicly accessable storage
         directory (/mnt/sdcard)

    原子移动doMoveMount/mnt/secure/staging到的可以让应用程序公开访问的存储目录(/mnt/sdcard

     

    也就是临时目录staging的作用就是为了保护android_secure。

     

    2)其次 android_secure的来源是什么?也就是为什么要把保护它android_secure

    先来看看历史:

    Google Android手机的软件为了安全性和稳定性都是默认安装到手机内存里,但是手机内存有限,所以我们会做app2sd操作,来让我们安装的软件放到sd卡上,这个操作是需要rom的支持的。

        Android 2.2 可以将手机程序安装在外置的sd卡上,也就是我们平常所说的app2sd。但是,官方的app2sd非常鸡肋,需要软件自身支持安装在内存卡上才可以,也就是说用官方的app2sd,要把程序安装在内存卡上,并不是我们使用者说了算,而是软件开发者说了算。经测试安装60多个软件,其中仅有可怜的5个程序能使用官方的app2sd安装在内存卡上。所以,官方的这个app2sd就是忽悠人的。当然,现在很多第三方ROM都自带了第三方的app2sd,可以将任何程序都安装在sd卡上。

    ——-应用程序相关的系统目录:

    /system 存放的是rom的信息;/system/app 存放rom本身附带的软件即系统软件;/system/data 存放/system/app 中核心系统软件的数据文件信息。

     /data 存放的是用户的软件信息(非自带rom安装的软件);

    /data/app 存放用户安装的软件;

    /data/data 存放所有软件(包括/system/app /data/app /mnt/asec中装的软件)的一些libxml文件等数据信息;

    /data/dalvik-cache 存放程序的缓存文件,这里的文件都是可以删除的。

    ———应用程序相关的数据目录:

    那么app2sd 的应用程序数据需要哪些关键的文件夹来保存呢?

    用户程序安装到到sd卡上后,其内容可能分散到:/mnt/asec , /mnt/secure , /data/data

    我们最关注“/mnt/asec 目录和 /mnt/secure 目录。所以当SD卡挂载于手机时,/mnt/sdcard/.android_secure 目录会被映射到/mnt/asec 目录和 /mnt/secure 目录。其中/mnt/asec 目录中主要是程序的安装目录,包括其执行文件和lib文件等;而/mnt/secure 目录中就存放程序加密后的档案。 ”例如:

    android之VOLD:staging目录作用与ASEC文件 -总结[通俗易懂]

    解密档案:

    android之VOLD:staging目录作用与ASEC文件 -总结[通俗易懂]

     

    另外注意:

    就是说,在/mnt路径下看到的/mnt/asec目录和/mnt/secure目录并不是真正存在在手机内存或者sd卡的分区挂载目录,他们本省只是根文件系统初始化的时候创建的两个目录,它们只是/mnt/sdcard/.android_secure目录的一个影像而已(也就是挂载点)

    怎么看出来呢? 很简单,打开手机的mass storage。如下:

    android之VOLD:staging目录作用与ASEC文件 -总结[通俗易懂]

    在通过ADB查看/mnt/asec目录和/mnt/secure 就发现是空的目录:

    android之VOLD:staging目录作用与ASEC文件 -总结[通俗易懂]

     

    3)什么时候Bind-mount /mnt/secure/staging/android_secure -> /mnt/secure/asec

    也就是/mnt/secure/staging/android_secure 会被mount/mnt/secure/asec

    const char *Volume::SEC_STG_SECIMGDIR = “/mnt/secure/staging/android_secure“;
    + * Path to where *only* root can access asec imagefiles
    +const char *Volume::SEC_ASECDIR       = “/mnt/secure/asec“;

     

    通过Volume::createBindMounts

    {

    ……………

    /*
    +     * Bind mount /mnt/secure/staging/android_secure -> /mnt/secure/asec so we’ll
    +     * have a root only accessable mountpoint for it.
    +     */
    +    if (mount(SEC_STG_SECIMGDIR, SEC_ASECDIR, “”, MS_BIND, NULL)) {

    +        LOGE(“Failed to bind mount points %s -> %s (%s)”,
    +                SEC_STG_SECIMGDIR, SEC_ASECDIR, strerror(errno));
    +        return -1;
    +    }

     

    …………………

    }

    执行完之后:sdcard/.android_secure目录下的*.asec文件就被mount/mnt/secure/asec:如下

    android之VOLD:staging目录作用与ASEC文件 -总结[通俗易懂]

     

    4mount完之后/mnt/secure/asec的文件如何解析到/mnt/asec

    int VolumeManager::mountAsec(const char *id, const char *key, int ownerUid) {

    607    char asecFileName[255];
    608    char mountPoint[255];
    609
    610    snprintf(asecFileName, sizeof(asecFileName), “%s/%s.asec”, Volume::SEC_ASECDIR, id);//SEC_ASECDIR=/mnt/secure/asec
    611    snprintf(mountPoint, sizeof(mountPoint), “%s/%s”, Volume::ASECDIR, id);//ASECDIR=/mnt/asec

    …..

     

     

    总结为什么药先mount sdcard/mnt/secure/staging/原因:,

    “那也就是说android_secure存放的是安装在SDcard的应用程序的的加密档案。那么在挂载的过程需要被保护起来,避免在挂载过程,应用程序访问该档案而被破坏,我觉得就是一个读写的互斥问题。”

     

     

    2.关于 ASEC文件

    What is an asec File?

    Filetype Android Secure Application File

    File used by Froyo, the version 2.2 release of the Android mobile operating system; stores mobile application data using proprietary encryption; saved to the.android_secure folder of a device’s SD card; can be run with the Android SDKemulator.

    The secure ASEC format allows applications to exist on mobile devices without being modified or corrupted by other programs.

    References

  2. filefacts.net Files with the .asec file e…
  3. fileinfo.com File used by Froyo, the ver…

    How to Open .ASEC Files? Program(s) that open .ASEC files

    Mac OS:

    Google Android SDK download

    Windows:

    Google Android SDK download

    via fileinfo.com

     

    源文档 <http://asec.filebio.com/asec-file-extension>

     

     

     

     

     

     

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/162121.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • python aes ecb_python代码封装加密

    python aes ecb_python代码封装加密前言AES加密的模式有很多种,下面来介绍ECB模式的加密解密importbase64fromCrypto.CipherimportAESclassAESECB:def__init

    2022年7月31日
    5
  • 深度学习(十五)基于DCNN的人脸特征点定位-CVPR 2013

    深度学习(十五)基于DCNN的人脸特征点定位-CVPR 2013基于DCNN的人脸特征点定位原文地址:http://blog.csdn.net/hjimce/article/details/49955149作者:hjimce一、相关理论本篇博文主要讲解2013年CVPR的一篇利用深度学习做人脸特征点定位的经典paper:《DeepConvolutionalNetworkCascadeforFacialPoint

    2022年5月29日
    35
  • 台式机通过网线连接笔记本上网「建议收藏」

    台式机通过网线连接笔记本上网「建议收藏」2019独角兽企业重金招聘Python工程师标准>>>…

    2022年6月26日
    43
  • Unity 协程(Coroutine)原理与用法详解「建议收藏」

    Unity 协程(Coroutine)原理与用法详解「建议收藏」前言:协程在Unity中是一个很重要的概念,我们知道,在使用Unity进行游戏开发时,一般不考虑多线程,那么如果处理一些并发的需求呢,Unity给我们提供了协程这种方式为啥在Unity中不考虑多线程因为在Unity中,只能在主线程中获取物体的组件、方法关于协程1,什么是协程协程,从字面意义上理解就是协助程序的意思,我们在主任务进行的同时,需要一些分支任务配合工作来达到最终的效果,这就是协程的概念:举个例子,在场景加载的时候,如果你的场景很复杂,那么加载过程就有可能使得画面卡顿,我们不

    2022年6月15日
    100
  • c语言里void什么作用,C语言中void是什么意思?

    c语言里void什么作用,C语言中void是什么意思?C语言中void是什么意思?C语言中“void”表示为无类型,相应的“void*”为无类型指针,常用在程序编写中对定义函数的参数类型、返回值、函数中指针类型进行声明,其作用是对函数返回和参数的进行限定。C语言关键字auto:声明自动变量break:跳出当前循环case:开关语句分支char:声明字符型变量或函数返回值类型const:声明只读变量continue:结束当前循环,开始下一轮循环…

    2022年5月12日
    55
  • frameset的使用

    frameset的使用才代码可以是frameset居中

    2025年6月17日
    5

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号