blob: 322f743a36977add61e99df014264104f6ee4b94 [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/*
2 * Copyright (C) 2007 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#include <hardware_legacy/IMountService.h>
Mathias Agopian07952722009-05-19 19:08:10 -070018#include <binder/BpBinder.h>
19#include <binder/IServiceManager.h>
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080020
21#include <stdio.h>
22#include <stdlib.h>
23#include <stdio.h>
24#include <string.h>
25#include <time.h>
26
27namespace android {
28
29static sp<IMountService> gMountService;
30
31static void init() {
32 sp<IServiceManager> sm = defaultServiceManager();
33 sp<IBinder> binder = sm->getService(String16("mount"));
34 gMountService = interface_cast<IMountService>(binder);
35 if (gMountService == 0) {
36 fprintf(stderr, "could not get MountService\n");
37 exit(1);
38 }
39}
40
41static bool isMounted(const char* mountPoint) {
42 char s[2000];
43 FILE *f = fopen("/proc/mounts", "r");
44 bool mounted = false;
45
46 while (fgets(s, sizeof(s), f))
47 {
48 char *c, *path = NULL;
49
50 for (c = s; *c; c++)
51 {
52 if (*c == ' ')
53 {
54 *c = 0;
55 path = c + 1;
56 break;
57 }
58 }
59
60 for (c = path; *c; c++)
61 {
62 if (*c == ' ')
63 {
64 *c = '\0';
65 break;
66 }
67 }
68
69 if (strcmp(mountPoint, path) == 0) {
70 mounted = true;
71 break;
72 }
73 }
74
75 fclose(f);
76 return mounted;
77}
78
79static void millisecondSleep(int milliseconds) {
80 struct timespec reqt, remt;
81 reqt.tv_sec = milliseconds / 1000;
82 reqt.tv_nsec = 1000000 * (milliseconds % 1000);
83 nanosleep(&reqt, &remt) ;
84
85}
86
87static int mount(const char* path) {
88 String16 string(path);
San Mehatcf7aa682010-01-15 10:05:37 -080089 gMountService->mountVolume(string);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080090
San Mehat9b0a5e52009-07-16 10:29:23 -070091 for (int i = 0; i < 60; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080092 if (isMounted(path)) {
93 return 0;
94 }
95 millisecondSleep(500);
96 }
97
98 fprintf(stderr, "failed to mount %s\n", path);
99 return -1;
100}
101
San Mehat36972292010-01-06 11:06:32 -0800102static int asec_create(const char *id, int sizeMb, const char *fstype,
103 const char *key, int ownerUid) {
104 String16 sId(id);
105 String16 sFstype(fstype);
106 String16 sKey(key);
107
San Mehat0f5525a2010-01-11 10:15:16 -0800108 String16 r = gMountService->createSecureContainer(sId, sizeMb, sFstype,
109 sKey, ownerUid);
San Mehat36972292010-01-06 11:06:32 -0800110 return 0;
111}
112
113static int asec_finalize(const char *id) {
114 String16 sId(id);
San Mehat0f5525a2010-01-11 10:15:16 -0800115 gMountService->finalizeSecureContainer(sId);
San Mehat36972292010-01-06 11:06:32 -0800116 return 0;
117}
118
119static int asec_destroy(const char *id) {
120 String16 sId(id);
San Mehat0f5525a2010-01-11 10:15:16 -0800121 gMountService->destroySecureContainer(sId);
San Mehat36972292010-01-06 11:06:32 -0800122 return 0;
123}
124
125static int asec_mount(const char *id, const char *key, int ownerUid) {
126 String16 sId(id);
127 String16 sKey(key);
San Mehat0f5525a2010-01-11 10:15:16 -0800128 gMountService->mountSecureContainer(sId, sKey, ownerUid);
San Mehat36972292010-01-06 11:06:32 -0800129 return 0;
130}
131
San Mehat9dba7092010-01-18 06:47:41 -0800132static void asec_unmount(const char *id) {
133 String16 sId(id);
134 gMountService->unmountSecureContainer(sId);
135}
136
San Mehat36972292010-01-06 11:06:32 -0800137static int asec_path(const char *id) {
138 String16 sId(id);
San Mehat0f5525a2010-01-11 10:15:16 -0800139 gMountService->getSecureContainerPath(sId);
San Mehat36972292010-01-06 11:06:32 -0800140 return 0;
141}
142
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143static int unmount(const char* path) {
144 String16 string(path);
San Mehatcf7aa682010-01-15 10:05:37 -0800145 gMountService->unmountVolume(string);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800146
San Mehat9b0a5e52009-07-16 10:29:23 -0700147 for (int i = 0; i < 20; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800148 if (!isMounted(path)) {
149 return 0;
150 }
151 millisecondSleep(500);
152 }
153
154 fprintf(stderr, "failed to unmount %s\n", path);
155 return -1;
156}
157
158static int format(const char* path) {
159 String16 string(path);
160
161 if (isMounted(path))
162 return -EBUSY;
San Mehatcf7aa682010-01-15 10:05:37 -0800163 gMountService->formatVolume(string);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800164
165 return 0;
166}
167
168static int umsEnable(bool enable) {
169 gMountService->setMassStorageEnabled(enable);
170 return 0;
171}
172
173};
174
175int main(int argc, char **argv)
176{
177 const char* command = (argc > 1 ? argv[1] : "");
178 const char* argument = (argc > 2 ? argv[2] : "");
179
180 if (strcmp(command, "mount") == 0) {
181 android::init();
182 return android::mount(argument);
183 } else if (strcmp(command, "format") == 0) {
184 android::init();
185 return android::format(argument);
186 } else if (strcmp(command, "unmount") == 0) {
187 android::init();
188 return android::unmount(argument);
189 } else if (strcmp(command, "ums") == 0) {
190 if (strcmp(argument, "enable") == 0) {
191 android::init();
192 return android::umsEnable(true);
193 } else if (strcmp(argument, "disable") == 0) {
194 android::init();
195 return android::umsEnable(false);
196 }
San Mehat36972292010-01-06 11:06:32 -0800197 } else if (!strcmp(command, "asec")) {
198 const char* id = (argc > 3 ? argv[3] : NULL);
199
200 if (!id)
201 goto usage;
202
203 android::init();
204 if (!strcmp(argument, "create")) {
205
206 if (argc != 8)
207 goto usage;
208 return android::asec_create(id, atoi(argv[4]), argv[5], argv[6],
209 atoi(argv[7]));
210 } else if (!strcmp(argument, "finalize")) {
211 return android::asec_finalize(id);
212 } else if (!strcmp(argument, "destroy")) {
213 return android::asec_destroy(id);
214 } else if (!strcmp(argument, "mount")) {
215 return android::asec_mount(id, argv[4], atoi(argv[5]));
San Mehat9dba7092010-01-18 06:47:41 -0800216 } else if (!strcmp(argument, "unmount")) {
217 android::asec_unmount(id);
218 return 0;
San Mehat36972292010-01-06 11:06:32 -0800219 } else if (!strcmp(argument, "path")) {
220 return android::asec_path(id);
221 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800222 }
223
San Mehat36972292010-01-06 11:06:32 -0800224usage:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800225 fprintf(stderr, "usage:\n"
226 " sdutil mount <mount path> - mounts the SD card at the given mount point\n"
227 " sdutil unmount <mount path> - unmounts the SD card at the given mount point\n"
228 " sdutil format <mount path> - formats the SD card at the given mount point\n"
229 " sdutil ums enable - enables USB mass storage\n"
San Mehat36972292010-01-06 11:06:32 -0800230 " sdutil ums disable - disables USB mass storage\n"
231 " sdutil asec create <id> <sizeMb> <fstype> <key> <ownerUid>\n"
232 " sdutil asec finalize <id>\n"
233 " sdutil asec destroy <id>\n"
234 " sdutil asec mount <id> <key> <ownerUid>\n"
San Mehat9dba7092010-01-18 06:47:41 -0800235 " sdutil asec unmount <id>\n"
San Mehat36972292010-01-06 11:06:32 -0800236 " sdutil asec path <id>\n"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800237 );
238 return -1;
239}