The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* |
| 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 Agopian | 0795272 | 2009-05-19 19:08:10 -0700 | [diff] [blame] | 18 | #include <binder/BpBinder.h> |
| 19 | #include <binder/IServiceManager.h> |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 20 | |
| 21 | #include <stdio.h> |
| 22 | #include <stdlib.h> |
| 23 | #include <stdio.h> |
| 24 | #include <string.h> |
| 25 | #include <time.h> |
| 26 | |
| 27 | namespace android { |
| 28 | |
| 29 | static sp<IMountService> gMountService; |
| 30 | |
| 31 | static 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 | |
| 41 | static 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 | |
| 79 | static 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 | |
| 87 | static int mount(const char* path) { |
| 88 | String16 string(path); |
San Mehat | cf7aa68 | 2010-01-15 10:05:37 -0800 | [diff] [blame] | 89 | gMountService->mountVolume(string); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 90 | |
San Mehat | 9b0a5e5 | 2009-07-16 10:29:23 -0700 | [diff] [blame] | 91 | for (int i = 0; i < 60; i++) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | 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 Mehat | 3697229 | 2010-01-06 11:06:32 -0800 | [diff] [blame] | 102 | static 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 Mehat | 0f5525a | 2010-01-11 10:15:16 -0800 | [diff] [blame] | 108 | String16 r = gMountService->createSecureContainer(sId, sizeMb, sFstype, |
| 109 | sKey, ownerUid); |
San Mehat | 3697229 | 2010-01-06 11:06:32 -0800 | [diff] [blame] | 110 | return 0; |
| 111 | } |
| 112 | |
| 113 | static int asec_finalize(const char *id) { |
| 114 | String16 sId(id); |
San Mehat | 0f5525a | 2010-01-11 10:15:16 -0800 | [diff] [blame] | 115 | gMountService->finalizeSecureContainer(sId); |
San Mehat | 3697229 | 2010-01-06 11:06:32 -0800 | [diff] [blame] | 116 | return 0; |
| 117 | } |
| 118 | |
| 119 | static int asec_destroy(const char *id) { |
| 120 | String16 sId(id); |
San Mehat | 0f5525a | 2010-01-11 10:15:16 -0800 | [diff] [blame] | 121 | gMountService->destroySecureContainer(sId); |
San Mehat | 3697229 | 2010-01-06 11:06:32 -0800 | [diff] [blame] | 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | static int asec_mount(const char *id, const char *key, int ownerUid) { |
| 126 | String16 sId(id); |
| 127 | String16 sKey(key); |
San Mehat | 0f5525a | 2010-01-11 10:15:16 -0800 | [diff] [blame] | 128 | gMountService->mountSecureContainer(sId, sKey, ownerUid); |
San Mehat | 3697229 | 2010-01-06 11:06:32 -0800 | [diff] [blame] | 129 | return 0; |
| 130 | } |
| 131 | |
San Mehat | 9dba709 | 2010-01-18 06:47:41 -0800 | [diff] [blame] | 132 | static void asec_unmount(const char *id) { |
| 133 | String16 sId(id); |
| 134 | gMountService->unmountSecureContainer(sId); |
| 135 | } |
| 136 | |
San Mehat | 3697229 | 2010-01-06 11:06:32 -0800 | [diff] [blame] | 137 | static int asec_path(const char *id) { |
| 138 | String16 sId(id); |
San Mehat | 0f5525a | 2010-01-11 10:15:16 -0800 | [diff] [blame] | 139 | gMountService->getSecureContainerPath(sId); |
San Mehat | 3697229 | 2010-01-06 11:06:32 -0800 | [diff] [blame] | 140 | return 0; |
| 141 | } |
| 142 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 143 | static int unmount(const char* path) { |
| 144 | String16 string(path); |
San Mehat | cf7aa68 | 2010-01-15 10:05:37 -0800 | [diff] [blame] | 145 | gMountService->unmountVolume(string); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 146 | |
San Mehat | 9b0a5e5 | 2009-07-16 10:29:23 -0700 | [diff] [blame] | 147 | for (int i = 0; i < 20; i++) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 148 | 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 | |
| 158 | static int format(const char* path) { |
| 159 | String16 string(path); |
| 160 | |
| 161 | if (isMounted(path)) |
| 162 | return -EBUSY; |
San Mehat | cf7aa68 | 2010-01-15 10:05:37 -0800 | [diff] [blame] | 163 | gMountService->formatVolume(string); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 164 | |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | static int umsEnable(bool enable) { |
| 169 | gMountService->setMassStorageEnabled(enable); |
| 170 | return 0; |
| 171 | } |
| 172 | |
| 173 | }; |
| 174 | |
| 175 | int 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 Mehat | 3697229 | 2010-01-06 11:06:32 -0800 | [diff] [blame] | 197 | } 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 Mehat | 9dba709 | 2010-01-18 06:47:41 -0800 | [diff] [blame] | 216 | } else if (!strcmp(argument, "unmount")) { |
| 217 | android::asec_unmount(id); |
| 218 | return 0; |
San Mehat | 3697229 | 2010-01-06 11:06:32 -0800 | [diff] [blame] | 219 | } else if (!strcmp(argument, "path")) { |
| 220 | return android::asec_path(id); |
| 221 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 222 | } |
| 223 | |
San Mehat | 3697229 | 2010-01-06 11:06:32 -0800 | [diff] [blame] | 224 | usage: |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 225 | 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 Mehat | 3697229 | 2010-01-06 11:06:32 -0800 | [diff] [blame] | 230 | " 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 Mehat | 9dba709 | 2010-01-18 06:47:41 -0800 | [diff] [blame] | 235 | " sdutil asec unmount <id>\n" |
San Mehat | 3697229 | 2010-01-06 11:06:32 -0800 | [diff] [blame] | 236 | " sdutil asec path <id>\n" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 237 | ); |
| 238 | return -1; |
| 239 | } |