2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #define LOG_TAG "IMountService"
19 #include <storage/IMountService.h>
20 #include <binder/Parcel.h>
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,
45 TRANSACTION_finishMediaUpdate,
47 TRANSACTION_unmountObb,
48 TRANSACTION_isObbMounted,
49 TRANSACTION_getMountedObbPath,
50 TRANSACTION_isExternalStorageEmulated,
51 TRANSACTION_decryptStorage,
52 TRANSACTION_encryptStorage,
55 class BpMountService: public BpInterface<IMountService>
58 BpMountService(const sp<IBinder>& impl)
59 : BpInterface<IMountService>(impl)
63 virtual void registerListener(const sp<IMountServiceListener>& listener)
66 data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
67 data.writeStrongBinder(IInterface::asBinder(listener));
68 if (remote()->transact(TRANSACTION_registerListener, data, &reply) != NO_ERROR) {
69 ALOGD("registerListener could not contact remote\n");
72 int32_t err = reply.readExceptionCode();
74 ALOGD("registerListener caught exception %d\n", err);
79 virtual void unregisterListener(const sp<IMountServiceListener>& listener)
82 data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
83 data.writeStrongBinder(IInterface::asBinder(listener));
84 if (remote()->transact(TRANSACTION_unregisterListener, data, &reply) != NO_ERROR) {
85 ALOGD("unregisterListener could not contact remote\n");
88 int32_t err = reply.readExceptionCode();
90 ALOGD("unregisterListener caught exception %d\n", err);
95 virtual bool isUsbMassStorageConnected()
98 data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
99 if (remote()->transact(TRANSACTION_isUsbMassStorageConnected, data, &reply) != NO_ERROR) {
100 ALOGD("isUsbMassStorageConnected could not contact remote\n");
103 int32_t err = reply.readExceptionCode();
105 ALOGD("isUsbMassStorageConnected caught exception %d\n", err);
108 return reply.readInt32() != 0;
111 virtual void setUsbMassStorageEnabled(const bool enable)
114 data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
115 data.writeInt32(enable != 0);
116 if (remote()->transact(TRANSACTION_setUsbMassStorageEnabled, data, &reply) != NO_ERROR) {
117 ALOGD("setUsbMassStorageEnabled could not contact remote\n");
120 int32_t err = reply.readExceptionCode();
122 ALOGD("setUsbMassStorageEnabled caught exception %d\n", err);
127 virtual bool isUsbMassStorageEnabled()
130 data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
131 if (remote()->transact(TRANSACTION_isUsbMassStorageEnabled, data, &reply) != NO_ERROR) {
132 ALOGD("isUsbMassStorageEnabled could not contact remote\n");
135 int32_t err = reply.readExceptionCode();
137 ALOGD("isUsbMassStorageEnabled caught exception %d\n", err);
140 return reply.readInt32() != 0;
143 int32_t mountVolume(const String16& mountPoint)
146 data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
147 data.writeString16(mountPoint);
148 if (remote()->transact(TRANSACTION_mountVolume, data, &reply) != NO_ERROR) {
149 ALOGD("mountVolume could not contact remote\n");
152 int32_t err = reply.readExceptionCode();
154 ALOGD("mountVolume caught exception %d\n", err);
157 return reply.readInt32();
160 int32_t unmountVolume(const String16& mountPoint, const bool force, const bool removeEncryption)
163 data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
164 data.writeString16(mountPoint);
165 data.writeInt32(force ? 1 : 0);
166 data.writeInt32(removeEncryption ? 1 : 0);
167 if (remote()->transact(TRANSACTION_unmountVolume, data, &reply) != NO_ERROR) {
168 ALOGD("unmountVolume could not contact remote\n");
171 int32_t err = reply.readExceptionCode();
173 ALOGD("unmountVolume caught exception %d\n", err);
176 return reply.readInt32();
179 int32_t formatVolume(const String16& mountPoint)
182 data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
183 data.writeString16(mountPoint);
184 if (remote()->transact(TRANSACTION_formatVolume, data, &reply) != NO_ERROR) {
185 ALOGD("formatVolume could not contact remote\n");
188 int32_t err = reply.readExceptionCode();
190 ALOGD("formatVolume caught exception %d\n", err);
193 return reply.readInt32();
196 int32_t getStorageUsers(const String16& mountPoint, int32_t** users)
199 data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
200 data.writeString16(mountPoint);
201 if (remote()->transact(TRANSACTION_getStorageUsers, data, &reply) != NO_ERROR) {
202 ALOGD("getStorageUsers could not contact remote\n");
205 int32_t err = reply.readExceptionCode();
207 ALOGD("getStorageUsers caught exception %d\n", err);
210 int32_t numUsersI = reply.readInt32();
213 ALOGW("Number of users is negative: %d\n", numUsersI);
216 numUsers = static_cast<uint32_t>(numUsersI);
218 *users = (int32_t*)malloc(sizeof(int32_t)*numUsers);
219 for (size_t i = 0; i < numUsers; i++) {
220 **users++ = reply.readInt32();
222 return static_cast<int32_t>(numUsers);
225 int32_t getVolumeState(const String16& mountPoint)
228 data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
229 data.writeString16(mountPoint);
230 if (remote()->transact(TRANSACTION_getVolumeState, data, &reply) != NO_ERROR) {
231 ALOGD("getVolumeState could not contact remote\n");
234 int32_t err = reply.readExceptionCode();
236 ALOGD("getVolumeState caught exception %d\n", err);
239 return reply.readInt32();
242 int32_t createSecureContainer(const String16& id, const int32_t sizeMb, const String16& fstype,
243 const String16& key, const int32_t ownerUid)
246 data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
247 data.writeString16(id);
248 data.writeInt32(sizeMb);
249 data.writeString16(fstype);
250 data.writeString16(key);
251 data.writeInt32(ownerUid);
252 if (remote()->transact(TRANSACTION_createSecureContainer, data, &reply) != NO_ERROR) {
253 ALOGD("createSecureContainer could not contact remote\n");
256 int32_t err = reply.readExceptionCode();
258 ALOGD("createSecureContainer caught exception %d\n", err);
261 return reply.readInt32();
264 int32_t finalizeSecureContainer(const String16& id)
267 data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
268 data.writeString16(id);
269 if (remote()->transact(TRANSACTION_finalizeSecureContainer, data, &reply) != NO_ERROR) {
270 ALOGD("finalizeSecureContainer couldn't call remote\n");
273 int32_t err = reply.readExceptionCode();
275 ALOGD("finalizeSecureContainer caught exception %d\n", err);
278 return reply.readInt32();
281 int32_t destroySecureContainer(const String16& id)
284 data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
285 data.writeString16(id);
286 if (remote()->transact(TRANSACTION_destroySecureContainer, data, &reply) != NO_ERROR) {
287 ALOGD("destroySecureContainer couldn't call remote");
290 int32_t err = reply.readExceptionCode();
292 ALOGD("destroySecureContainer caught exception %d\n", err);
295 return reply.readInt32();
298 int32_t mountSecureContainer(const String16& id, const String16& key, const int32_t ownerUid)
301 data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
302 data.writeString16(id);
303 data.writeString16(key);
304 data.writeInt32(ownerUid);
307 if (remote()->transact(TRANSACTION_mountSecureContainer, data, &reply) != NO_ERROR) {
308 ALOGD("mountSecureContainer couldn't call remote");
311 int32_t err = reply.readExceptionCode(); // What to do...
313 ALOGD("mountSecureContainer caught exception %d\n", err);
316 return reply.readInt32();
319 int32_t unmountSecureContainer(const String16& id, const bool force)
322 data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
323 data.writeString16(id);
324 data.writeInt32(force ? 1 : 0);
325 if (remote()->transact(TRANSACTION_getSecureContainerPath, data, &reply) != NO_ERROR) {
326 ALOGD("unmountSecureContainer couldn't call remote");
329 int32_t err = reply.readExceptionCode(); // What to do...
331 ALOGD("unmountSecureContainer caught exception %d\n", err);
334 return reply.readInt32();
337 bool isSecureContainerMounted(const String16& id)
340 data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
341 data.writeString16(id);
342 if (remote()->transact(TRANSACTION_isSecureContainerMounted, data, &reply) != NO_ERROR) {
343 ALOGD("isSecureContainerMounted couldn't call remote");
346 int32_t err = reply.readExceptionCode(); // What to do...
348 ALOGD("isSecureContainerMounted caught exception %d\n", err);
351 return reply.readInt32() != 0;
354 int32_t renameSecureContainer(const String16& oldId, const String16& newId)
357 data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
358 data.writeString16(oldId);
359 data.writeString16(newId);
360 if (remote()->transact(TRANSACTION_renameSecureContainer, data, &reply) != NO_ERROR) {
361 ALOGD("renameSecureContainer couldn't call remote");
364 int32_t err = reply.readExceptionCode(); // What to do...
366 ALOGD("renameSecureContainer caught exception %d\n", err);
369 return reply.readInt32();
372 bool getSecureContainerPath(const String16& id, String16& path)
375 data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
376 data.writeString16(id);
377 if (remote()->transact(TRANSACTION_getSecureContainerPath, data, &reply) != NO_ERROR) {
378 ALOGD("getSecureContainerPath couldn't call remote");
381 int32_t err = reply.readExceptionCode(); // What to do...
383 ALOGD("getSecureContainerPath caught exception %d\n", err);
386 path = reply.readString16();
390 int32_t getSecureContainerList(const String16& id, String16*& containers)
393 data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
394 data.writeString16(id);
395 if (remote()->transact(TRANSACTION_getSecureContainerList, data, &reply) != NO_ERROR) {
396 ALOGD("getSecureContainerList couldn't call remote");
399 int32_t err = reply.readExceptionCode();
401 ALOGD("getSecureContainerList caught exception %d\n", err);
404 const int32_t numStrings = reply.readInt32();
405 containers = new String16[numStrings];
406 for (int i = 0; i < numStrings; i++) {
407 containers[i] = reply.readString16();
412 void shutdown(const sp<IMountShutdownObserver>& observer)
415 data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
416 data.writeStrongBinder(IInterface::asBinder(observer));
417 if (remote()->transact(TRANSACTION_shutdown, data, &reply) != NO_ERROR) {
418 ALOGD("shutdown could not contact remote\n");
421 int32_t err = reply.readExceptionCode();
423 ALOGD("shutdown caught exception %d\n", err);
426 reply.readExceptionCode();
429 void finishMediaUpdate()
432 data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
433 if (remote()->transact(TRANSACTION_finishMediaUpdate, data, &reply) != NO_ERROR) {
434 ALOGD("finishMediaUpdate could not contact remote\n");
437 int32_t err = reply.readExceptionCode();
439 ALOGD("finishMediaUpdate caught exception %d\n", err);
442 reply.readExceptionCode();
445 void mountObb(const String16& rawPath, const String16& canonicalPath, const String16& key,
446 const sp<IObbActionListener>& token, int32_t nonce)
449 data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
450 data.writeString16(rawPath);
451 data.writeString16(canonicalPath);
452 data.writeString16(key);
453 data.writeStrongBinder(IInterface::asBinder(token));
454 data.writeInt32(nonce);
455 if (remote()->transact(TRANSACTION_mountObb, data, &reply) != NO_ERROR) {
456 ALOGD("mountObb could not contact remote\n");
459 int32_t err = reply.readExceptionCode();
461 ALOGD("mountObb caught exception %d\n", err);
466 void unmountObb(const String16& filename, const bool force,
467 const sp<IObbActionListener>& token, const int32_t nonce)
470 data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
471 data.writeString16(filename);
472 data.writeInt32(force ? 1 : 0);
473 data.writeStrongBinder(IInterface::asBinder(token));
474 data.writeInt32(nonce);
475 if (remote()->transact(TRANSACTION_unmountObb, data, &reply) != NO_ERROR) {
476 ALOGD("unmountObb could not contact remote\n");
479 int32_t err = reply.readExceptionCode();
481 ALOGD("unmountObb caught exception %d\n", err);
486 bool isObbMounted(const String16& filename)
489 data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
490 data.writeString16(filename);
491 if (remote()->transact(TRANSACTION_isObbMounted, data, &reply) != NO_ERROR) {
492 ALOGD("isObbMounted could not contact remote\n");
495 int32_t err = reply.readExceptionCode();
497 ALOGD("isObbMounted caught exception %d\n", err);
500 return reply.readInt32() != 0;
503 bool getMountedObbPath(const String16& filename, String16& path)
506 data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
507 data.writeString16(filename);
508 if (remote()->transact(TRANSACTION_getMountedObbPath, data, &reply) != NO_ERROR) {
509 ALOGD("getMountedObbPath could not contact remote\n");
512 int32_t err = reply.readExceptionCode();
514 ALOGD("getMountedObbPath caught exception %d\n", err);
517 path = reply.readString16();
521 int32_t decryptStorage(const String16& password)
524 data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
525 data.writeString16(password);
526 if (remote()->transact(TRANSACTION_decryptStorage, data, &reply) != NO_ERROR) {
527 ALOGD("decryptStorage could not contact remote\n");
530 int32_t err = reply.readExceptionCode();
532 ALOGD("decryptStorage caught exception %d\n", err);
535 return reply.readInt32();
538 int32_t encryptStorage(const String16& password)
541 data.writeInterfaceToken(IMountService::getInterfaceDescriptor());
542 data.writeString16(password);
543 if (remote()->transact(TRANSACTION_encryptStorage, data, &reply) != NO_ERROR) {
544 ALOGD("encryptStorage could not contact remote\n");
547 int32_t err = reply.readExceptionCode();
549 ALOGD("encryptStorage caught exception %d\n", err);
552 return reply.readInt32();
556 IMPLEMENT_META_INTERFACE(MountService, "IMountService")
558 // ----------------------------------------------------------------------