blob: c77424fbc674fdae5f680a5af78ce0f9b380a8d2 [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);
89 gMountService->mountMedia(string);
90
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
132static int asec_path(const char *id) {
133 String16 sId(id);
San Mehat0f5525a2010-01-11 10:15:16 -0800134 gMountService->getSecureContainerPath(sId);
San Mehat36972292010-01-06 11:06:32 -0800135 return 0;
136}
137
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800138static int unmount(const char* path) {
139 String16 string(path);
140 gMountService->unmountMedia(string);
141
San Mehat9b0a5e52009-07-16 10:29:23 -0700142 for (int i = 0; i < 20; i++) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800143 if (!isMounted(path)) {
144 return 0;
145 }
146 millisecondSleep(500);
147 }
148
149 fprintf(stderr, "failed to unmount %s\n", path);
150 return -1;
151}
152
153static int format(const char* path) {
154 String16 string(path);
155
156 if (isMounted(path))
157 return -EBUSY;
158 gMountService->formatMedia(string);
159
160 return 0;
161}
162
163static int umsEnable(bool enable) {
164 gMountService->setMassStorageEnabled(enable);
165 return 0;
166}
167
168};
169
170int main(int argc, char **argv)
171{
172 const char* command = (argc > 1 ? argv[1] : "");
173 const char* argument = (argc > 2 ? argv[2] : "");
174
175 if (strcmp(command, "mount") == 0) {
176 android::init();
177 return android::mount(argument);
178 } else if (strcmp(command, "format") == 0) {
179 android::init();
180 return android::format(argument);
181 } else if (strcmp(command, "unmount") == 0) {
182 android::init();
183 return android::unmount(argument);
184 } else if (strcmp(command, "ums") == 0) {
185 if (strcmp(argument, "enable") == 0) {
186 android::init();
187 return android::umsEnable(true);
188 } else if (strcmp(argument, "disable") == 0) {
189 android::init();
190 return android::umsEnable(false);
191 }
San Mehat36972292010-01-06 11:06:32 -0800192 } else if (!strcmp(command, "asec")) {
193 const char* id = (argc > 3 ? argv[3] : NULL);
194
195 if (!id)
196 goto usage;
197
198 android::init();
199 if (!strcmp(argument, "create")) {
200
201 if (argc != 8)
202 goto usage;
203 return android::asec_create(id, atoi(argv[4]), argv[5], argv[6],
204 atoi(argv[7]));
205 } else if (!strcmp(argument, "finalize")) {
206 return android::asec_finalize(id);
207 } else if (!strcmp(argument, "destroy")) {
208 return android::asec_destroy(id);
209 } else if (!strcmp(argument, "mount")) {
210 return android::asec_mount(id, argv[4], atoi(argv[5]));
211 } else if (!strcmp(argument, "path")) {
212 return android::asec_path(id);
213 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800214 }
215
San Mehat36972292010-01-06 11:06:32 -0800216usage:
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800217 fprintf(stderr, "usage:\n"
218 " sdutil mount <mount path> - mounts the SD card at the given mount point\n"
219 " sdutil unmount <mount path> - unmounts the SD card at the given mount point\n"
220 " sdutil format <mount path> - formats the SD card at the given mount point\n"
221 " sdutil ums enable - enables USB mass storage\n"
San Mehat36972292010-01-06 11:06:32 -0800222 " sdutil ums disable - disables USB mass storage\n"
223 " sdutil asec create <id> <sizeMb> <fstype> <key> <ownerUid>\n"
224 " sdutil asec finalize <id>\n"
225 " sdutil asec destroy <id>\n"
226 " sdutil asec mount <id> <key> <ownerUid>\n"
227 " sdutil asec path <id>\n"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800228 );
229 return -1;
230}