Home | History | Annotate | Download | only in storage
      1 /*
      2  * Copyright (C) 2010 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 
     17 #define LOG_TAG "IMountService"
     18 
     19 #include <storage/IMountService.h>
     20 #include <binder/Parcel.h>
     21 
     22 namespace android {
     23 
     24 enum {
     25     TRANSACTION_registerListener = IBinder::FIRST_CALL_TRANSACTION,
     26     TRANSACTION_unregisterListener,
     27     TRANSACTION_isUsbMassStorageConnected,
     28     TRANSACTION_setUsbMassStorageEnabled,
     29     TRANSACTION_isUsbMassStorageEnabled,
     30     TRANSACTION_mountVolume,
     31     TRANSACTION_unmountVolume,
     32     TRANSACTION_formatVolume,
     33     TRANSACTION_getStorageUsers,
     34     TRANSACTION_getVolumeState,
     35     TRANSACTION_createSecureContainer,
     36     TRANSACTION_finalizeSecureContainer,
     37     TRANSACTION_destroySecureContainer,
     38     TRANSACTION_mountSecureContainer,
     39     TRANSACTION_unmountSecureContainer,
     40     TRANSACTION_isSecureContainerMounted,
     41     TRANSACTION_renameSecureContainer,
     42     TRANSACTION_getSecureContainerPath,
     43     TRANSACTION_getSecureContainerList,
     44     TRANSACTION_shutdown,
     45     TRANSACTION_finishMediaUpdate,
     46     TRANSACTION_mountObb,
     47     TRANSACTION_unmountObb,
     48     TRANSACTION_isObbMounted,
     49     TRANSACTION_getMountedObbPath,
     50 };
     51 
     52 class BpMountService: public BpInterface<IMountService>
     53 {
     54 public:
     55     BpMountService(const sp<IBinder>& impl)
     56         : BpInterface<IMountService>(impl)
     57     {
     58     }
     59 
     60     virtual void registerListener(const sp<IMountServiceListener>& listener)
     61     {
     62         Parcel data, reply;
     63         data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
     64         data.writeStrongBinder(listener->asBinder());
     65         if (remote()->transact(TRANSACTION_registerListener, data, &reply) != NO_ERROR) {
     66             LOGD("registerListener could not contact remote\n");
     67             return;
     68         }
     69         int32_t err = reply.readExceptionCode();
     70         if (err < 0) {
     71             LOGD("registerListener caught exception %d\n", err);
     72             return;
     73         }
     74     }
     75 
     76     virtual void unregisterListener(const sp<IMountServiceListener>& listener)
     77     {
     78         Parcel data, reply;
     79         data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
     80         data.writeStrongBinder(listener->asBinder());
     81         if (remote()->transact(TRANSACTION_unregisterListener, data, &reply) != NO_ERROR) {
     82             LOGD("unregisterListener could not contact remote\n");
     83             return;
     84         }
     85         int32_t err = reply.readExceptionCode();
     86         if (err < 0) {
     87             LOGD("unregisterListener caught exception %d\n", err);
     88             return;
     89         }
     90     }
     91 
     92     virtual bool isUsbMassStorageConnected()
     93     {
     94         Parcel data, reply;
     95         data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
     96         if (remote()->transact(TRANSACTION_isUsbMassStorageConnected, data, &reply) != NO_ERROR) {
     97             LOGD("isUsbMassStorageConnected could not contact remote\n");
     98             return false;
     99         }
    100         int32_t err = reply.readExceptionCode();
    101         if (err < 0) {
    102             LOGD("isUsbMassStorageConnected caught exception %d\n", err);
    103             return false;
    104         }
    105         return reply.readInt32() != 0;
    106     }
    107 
    108     virtual void setUsbMassStorageEnabled(const bool enable)
    109     {
    110         Parcel data, reply;
    111         data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
    112         data.writeInt32(enable != 0);
    113         if (remote()->transact(TRANSACTION_setUsbMassStorageEnabled, data, &reply) != NO_ERROR) {
    114             LOGD("setUsbMassStorageEnabled could not contact remote\n");
    115             return;
    116         }
    117         int32_t err = reply.readExceptionCode();
    118         if (err < 0) {
    119             LOGD("setUsbMassStorageEnabled caught exception %d\n", err);
    120             return;
    121         }
    122     }
    123 
    124     virtual bool isUsbMassStorageEnabled()
    125     {
    126         Parcel data, reply;
    127         data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
    128         if (remote()->transact(TRANSACTION_isUsbMassStorageEnabled, data, &reply) != NO_ERROR) {
    129             LOGD("isUsbMassStorageEnabled could not contact remote\n");
    130             return false;
    131         }
    132         int32_t err = reply.readExceptionCode();
    133         if (err < 0) {
    134             LOGD("isUsbMassStorageEnabled caught exception %d\n", err);
    135             return false;
    136         }
    137         return reply.readInt32() != 0;
    138     }
    139 
    140     int32_t mountVolume(const String16& mountPoint)
    141     {
    142         Parcel data, reply;
    143         data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
    144         data.writeString16(mountPoint);
    145         if (remote()->transact(TRANSACTION_mountVolume, data, &reply) != NO_ERROR) {
    146             LOGD("mountVolume could not contact remote\n");
    147             return -1;
    148         }
    149         int32_t err = reply.readExceptionCode();
    150         if (err < 0) {
    151             LOGD("mountVolume caught exception %d\n", err);
    152             return err;
    153         }
    154         return reply.readInt32();
    155     }
    156 
    157     int32_t unmountVolume(const String16& mountPoint, const bool force)
    158     {
    159         Parcel data, reply;
    160         data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
    161         data.writeString16(mountPoint);
    162         data.writeInt32(force ? 1 : 0);
    163         if (remote()->transact(TRANSACTION_unmountVolume, data, &reply) != NO_ERROR) {
    164             LOGD("unmountVolume could not contact remote\n");
    165             return -1;
    166         }
    167         int32_t err = reply.readExceptionCode();
    168         if (err < 0) {
    169             LOGD("unmountVolume caught exception %d\n", err);
    170             return err;
    171         }
    172         return reply.readInt32();
    173     }
    174 
    175     int32_t formatVolume(const String16& mountPoint)
    176     {
    177         Parcel data, reply;
    178         data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
    179         data.writeString16(mountPoint);
    180         if (remote()->transact(TRANSACTION_formatVolume, data, &reply) != NO_ERROR) {
    181             LOGD("formatVolume could not contact remote\n");
    182             return -1;
    183         }
    184         int32_t err = reply.readExceptionCode();
    185         if (err < 0) {
    186             LOGD("formatVolume caught exception %d\n", err);
    187             return err;
    188         }
    189         return reply.readInt32();
    190     }
    191 
    192     int32_t getStorageUsers(const String16& mountPoint, int32_t** users)
    193     {
    194         Parcel data, reply;
    195         data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
    196         data.writeString16(mountPoint);
    197         if (remote()->transact(TRANSACTION_getStorageUsers, data, &reply) != NO_ERROR) {
    198             LOGD("getStorageUsers could not contact remote\n");
    199             return -1;
    200         }
    201         int32_t err = reply.readExceptionCode();
    202         if (err < 0) {
    203             LOGD("getStorageUsers caught exception %d\n", err);
    204             return err;
    205         }
    206         const int32_t numUsers = reply.readInt32();
    207         *users = (int32_t*)malloc(sizeof(int32_t)*numUsers);
    208         for (int i = 0; i < numUsers; i++) {
    209             **users++ = reply.readInt32();
    210         }
    211         return numUsers;
    212     }
    213 
    214     int32_t getVolumeState(const String16& mountPoint)
    215     {
    216         Parcel data, reply;
    217         data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
    218         data.writeString16(mountPoint);
    219         if (remote()->transact(TRANSACTION_getVolumeState, data, &reply) != NO_ERROR) {
    220             LOGD("getVolumeState could not contact remote\n");
    221             return -1;
    222         }
    223         int32_t err = reply.readExceptionCode();
    224         if (err < 0) {
    225             LOGD("getVolumeState caught exception %d\n", err);
    226             return err;
    227         }
    228         return reply.readInt32();
    229     }
    230 
    231     int32_t createSecureContainer(const String16& id, const int32_t sizeMb, const String16& fstype,
    232             const String16& key, const int32_t ownerUid)
    233     {
    234         Parcel data, reply;
    235         data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
    236         data.writeString16(id);
    237         data.writeInt32(sizeMb);
    238         data.writeString16(fstype);
    239         data.writeString16(key);
    240         data.writeInt32(ownerUid);
    241         if (remote()->transact(TRANSACTION_createSecureContainer, data, &reply) != NO_ERROR) {
    242             LOGD("createSecureContainer could not contact remote\n");
    243             return -1;
    244         }
    245         int32_t err = reply.readExceptionCode();
    246         if (err < 0) {
    247             LOGD("createSecureContainer caught exception %d\n", err);
    248             return err;
    249         }
    250         return reply.readInt32();
    251     }
    252 
    253     int32_t finalizeSecureContainer(const String16& id)
    254     {
    255         Parcel data, reply;
    256         data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
    257         data.writeString16(id);
    258         if (remote()->transact(TRANSACTION_finalizeSecureContainer, data, &reply) != NO_ERROR) {
    259             LOGD("finalizeSecureContainer couldn't call remote\n");
    260             return -1;
    261         }
    262         int32_t err = reply.readExceptionCode();
    263         if (err < 0) {
    264             LOGD("finalizeSecureContainer caught exception %d\n", err);
    265             return err;
    266         }
    267         return reply.readInt32();
    268     }
    269 
    270     int32_t destroySecureContainer(const String16& id)
    271     {
    272         Parcel data, reply;
    273         data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
    274         data.writeString16(id);
    275         if (remote()->transact(TRANSACTION_destroySecureContainer, data, &reply) != NO_ERROR) {
    276             LOGD("destroySecureContainer couldn't call remote");
    277             return -1;
    278         }
    279         int32_t err = reply.readExceptionCode();
    280         if (err < 0) {
    281             LOGD("destroySecureContainer caught exception %d\n", err);
    282             return err;
    283         }
    284         return reply.readInt32();
    285     }
    286 
    287     int32_t mountSecureContainer(const String16& id, const String16& key, const int32_t ownerUid)
    288     {
    289         Parcel data, reply;
    290         data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
    291         data.writeString16(id);
    292         data.writeString16(key);
    293         data.writeInt32(ownerUid);
    294         if (remote()->transact(TRANSACTION_mountSecureContainer, data, &reply) != NO_ERROR) {
    295             LOGD("mountSecureContainer couldn't call remote");
    296             return -1;
    297         }
    298         int32_t err = reply.readExceptionCode(); // What to do...
    299         if (err < 0) {
    300             LOGD("mountSecureContainer caught exception %d\n", err);
    301             return err;
    302         }
    303         return reply.readInt32();
    304     }
    305 
    306     int32_t unmountSecureContainer(const String16& id, const bool force)
    307     {
    308         Parcel data, reply;
    309         data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
    310         data.writeString16(id);
    311         data.writeInt32(force ? 1 : 0);
    312         if (remote()->transact(TRANSACTION_getSecureContainerPath, data, &reply) != NO_ERROR) {
    313             LOGD("unmountSecureContainer couldn't call remote");
    314             return -1;
    315         }
    316         int32_t err = reply.readExceptionCode(); // What to do...
    317         if (err < 0) {
    318             LOGD("unmountSecureContainer caught exception %d\n", err);
    319             return err;
    320         }
    321         return reply.readInt32();
    322     }
    323 
    324     bool isSecureContainerMounted(const String16& id)
    325     {
    326         Parcel data, reply;
    327         data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
    328         data.writeString16(id);
    329         if (remote()->transact(TRANSACTION_isSecureContainerMounted, data, &reply) != NO_ERROR) {
    330             LOGD("isSecureContainerMounted couldn't call remote");
    331             return false;
    332         }
    333         int32_t err = reply.readExceptionCode(); // What to do...
    334         if (err < 0) {
    335             LOGD("isSecureContainerMounted caught exception %d\n", err);
    336             return false;
    337         }
    338         return reply.readInt32() != 0;
    339     }
    340 
    341     int32_t renameSecureContainer(const String16& oldId, const String16& newId)
    342     {
    343         Parcel data, reply;
    344         data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
    345         data.writeString16(oldId);
    346         data.writeString16(newId);
    347         if (remote()->transact(TRANSACTION_renameSecureContainer, data, &reply) != NO_ERROR) {
    348             LOGD("renameSecureContainer couldn't call remote");
    349             return -1;
    350         }
    351         int32_t err = reply.readExceptionCode(); // What to do...
    352         if (err < 0) {
    353             LOGD("renameSecureContainer caught exception %d\n", err);
    354             return err;
    355         }
    356         return reply.readInt32();
    357     }
    358 
    359     bool getSecureContainerPath(const String16& id, String16& path)
    360     {
    361         Parcel data, reply;
    362         data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
    363         data.writeString16(id);
    364         if (remote()->transact(TRANSACTION_getSecureContainerPath, data, &reply) != NO_ERROR) {
    365             LOGD("getSecureContainerPath couldn't call remote");
    366             return false;
    367         }
    368         int32_t err = reply.readExceptionCode(); // What to do...
    369         if (err < 0) {
    370             LOGD("getSecureContainerPath caught exception %d\n", err);
    371             return false;
    372         }
    373         path = reply.readString16();
    374         return true;
    375     }
    376 
    377     int32_t getSecureContainerList(const String16& id, String16*& containers)
    378     {
    379         Parcel data, reply;
    380         data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
    381         data.writeString16(id);
    382         if (remote()->transact(TRANSACTION_getSecureContainerList, data, &reply) != NO_ERROR) {
    383             LOGD("getSecureContainerList couldn't call remote");
    384             return -1;
    385         }
    386         int32_t err = reply.readExceptionCode();
    387         if (err < 0) {
    388             LOGD("getSecureContainerList caught exception %d\n", err);
    389             return err;
    390         }
    391         const int32_t numStrings = reply.readInt32();
    392         containers = new String16[numStrings];
    393         for (int i = 0; i < numStrings; i++) {
    394             containers[i] = reply.readString16();
    395         }
    396         return numStrings;
    397     }
    398 
    399     void shutdown(const sp<IMountShutdownObserver>& observer)
    400     {
    401         Parcel data, reply;
    402         data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
    403         data.writeStrongBinder(observer->asBinder());
    404         if (remote()->transact(TRANSACTION_shutdown, data, &reply) != NO_ERROR) {
    405             LOGD("shutdown could not contact remote\n");
    406             return;
    407         }
    408         int32_t err = reply.readExceptionCode();
    409         if (err < 0) {
    410             LOGD("shutdown caught exception %d\n", err);
    411             return;
    412         }
    413         reply.readExceptionCode();
    414     }
    415 
    416     void finishMediaUpdate()
    417     {
    418         Parcel data, reply;
    419         data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
    420         if (remote()->transact(TRANSACTION_finishMediaUpdate, data, &reply) != NO_ERROR) {
    421             LOGD("finishMediaUpdate could not contact remote\n");
    422             return;
    423         }
    424         int32_t err = reply.readExceptionCode();
    425         if (err < 0) {
    426             LOGD("finishMediaUpdate caught exception %d\n", err);
    427             return;
    428         }
    429         reply.readExceptionCode();
    430     }
    431 
    432     void mountObb(const String16& filename, const String16& key,
    433             const sp<IObbActionListener>& token, int32_t nonce)
    434     {
    435         Parcel data, reply;
    436         data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
    437         data.writeString16(filename);
    438         data.writeString16(key);
    439         data.writeStrongBinder(token->asBinder());
    440         data.writeInt32(nonce);
    441         if (remote()->transact(TRANSACTION_mountObb, data, &reply) != NO_ERROR) {
    442             LOGD("mountObb could not contact remote\n");
    443             return;
    444         }
    445         int32_t err = reply.readExceptionCode();
    446         if (err < 0) {
    447             LOGD("mountObb caught exception %d\n", err);
    448             return;
    449         }
    450     }
    451 
    452     void unmountObb(const String16& filename, const bool force,
    453             const sp<IObbActionListener>& token, const int32_t nonce)
    454     {
    455         Parcel data, reply;
    456         data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
    457         data.writeString16(filename);
    458         data.writeInt32(force ? 1 : 0);
    459         data.writeStrongBinder(token->asBinder());
    460         data.writeInt32(nonce);
    461         if (remote()->transact(TRANSACTION_unmountObb, data, &reply) != NO_ERROR) {
    462             LOGD("unmountObb could not contact remote\n");
    463             return;
    464         }
    465         int32_t err = reply.readExceptionCode();
    466         if (err < 0) {
    467             LOGD("unmountObb caught exception %d\n", err);
    468             return;
    469         }
    470     }
    471 
    472     bool isObbMounted(const String16& filename)
    473     {
    474         Parcel data, reply;
    475         data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
    476         data.writeString16(filename);
    477         if (remote()->transact(TRANSACTION_isObbMounted, data, &reply) != NO_ERROR) {
    478             LOGD("isObbMounted could not contact remote\n");
    479             return false;
    480         }
    481         int32_t err = reply.readExceptionCode();
    482         if (err < 0) {
    483             LOGD("isObbMounted caught exception %d\n", err);
    484             return false;
    485         }
    486         return reply.readInt32() != 0;
    487     }
    488 
    489     bool getMountedObbPath(const String16& filename, String16& path)
    490     {
    491         Parcel data, reply;
    492         data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
    493         data.writeString16(filename);
    494         if (remote()->transact(TRANSACTION_getMountedObbPath, data, &reply) != NO_ERROR) {
    495             LOGD("getMountedObbPath could not contact remote\n");
    496             return false;
    497         }
    498         int32_t err = reply.readExceptionCode();
    499         if (err < 0) {
    500             LOGD("getMountedObbPath caught exception %d\n", err);
    501             return false;
    502         }
    503         path = reply.readString16();
    504         return true;
    505     }
    506 };
    507 
    508 IMPLEMENT_META_INTERFACE(MountService, "IMountService");
    509 
    510 // ----------------------------------------------------------------------
    511 
    512 };
    513