San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2008 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 | |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 17 | #include <stdlib.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 18 | #include <string.h> |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 19 | #include <dirent.h> |
| 20 | #include <errno.h> |
| 21 | #include <fcntl.h> |
| 22 | |
| 23 | #include <sys/types.h> |
| 24 | #include <sys/stat.h> |
| 25 | #include <sys/types.h> |
| 26 | #include <sys/mman.h> |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 27 | #include <sys/mount.h> |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 28 | #include <sys/param.h> |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 29 | |
| 30 | #include <linux/kdev_t.h> |
| 31 | |
| 32 | #include <cutils/properties.h> |
| 33 | |
San Mehat | 2a5b8ce | 2010-03-10 12:48:57 -0800 | [diff] [blame] | 34 | #include <diskconfig/diskconfig.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 35 | |
Mike Lockwood | 9092b1d | 2011-03-23 14:55:49 -0400 | [diff] [blame] | 36 | #include <private/android_filesystem_config.h> |
| 37 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 38 | #define LOG_TAG "Vold" |
| 39 | |
| 40 | #include <cutils/log.h> |
| 41 | |
| 42 | #include "Volume.h" |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 43 | #include "VolumeManager.h" |
| 44 | #include "ResponseCode.h" |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 45 | #include "Fat.h" |
San Mehat | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 46 | #include "Process.h" |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 47 | #include "cryptfs.h" |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 48 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 49 | extern "C" void dos_partition_dec(void const *pp, struct dos_partition *d); |
| 50 | extern "C" void dos_partition_enc(void *pp, struct dos_partition *d); |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 51 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 52 | |
| 53 | /* |
| 54 | * Secure directory - stuff that only root can see |
| 55 | */ |
| 56 | const char *Volume::SECDIR = "/mnt/secure"; |
| 57 | |
| 58 | /* |
| 59 | * Secure staging directory - where media is mounted for preparation |
| 60 | */ |
| 61 | const char *Volume::SEC_STGDIR = "/mnt/secure/staging"; |
| 62 | |
| 63 | /* |
| 64 | * Path to the directory on the media which contains publicly accessable |
| 65 | * asec imagefiles. This path will be obscured before the mount is |
| 66 | * exposed to non priviledged users. |
| 67 | */ |
San Mehat | 52c2ccb | 2010-02-23 18:26:13 -0800 | [diff] [blame] | 68 | const char *Volume::SEC_STG_SECIMGDIR = "/mnt/secure/staging/.android_secure"; |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 69 | |
| 70 | /* |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 71 | * Path to external storage where *only* root can access ASEC image files |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 72 | */ |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 73 | const char *Volume::SEC_ASECDIR_EXT = "/mnt/secure/asec"; |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 74 | |
| 75 | /* |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 76 | * Path to internal storage where *only* root can access ASEC image files |
| 77 | */ |
| 78 | const char *Volume::SEC_ASECDIR_INT = "/data/app-asec"; |
| 79 | /* |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 80 | * Path to where secure containers are mounted |
| 81 | */ |
| 82 | const char *Volume::ASECDIR = "/mnt/asec"; |
| 83 | |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 84 | /* |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 85 | * Path to where OBBs are mounted |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 86 | */ |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 87 | const char *Volume::LOOPDIR = "/mnt/obb"; |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 88 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 89 | static const char *stateToStr(int state) { |
| 90 | if (state == Volume::State_Init) |
| 91 | return "Initializing"; |
| 92 | else if (state == Volume::State_NoMedia) |
| 93 | return "No-Media"; |
| 94 | else if (state == Volume::State_Idle) |
| 95 | return "Idle-Unmounted"; |
| 96 | else if (state == Volume::State_Pending) |
| 97 | return "Pending"; |
| 98 | else if (state == Volume::State_Mounted) |
| 99 | return "Mounted"; |
| 100 | else if (state == Volume::State_Unmounting) |
| 101 | return "Unmounting"; |
| 102 | else if (state == Volume::State_Checking) |
| 103 | return "Checking"; |
| 104 | else if (state == Volume::State_Formatting) |
| 105 | return "Formatting"; |
| 106 | else if (state == Volume::State_Shared) |
| 107 | return "Shared-Unmounted"; |
| 108 | else if (state == Volume::State_SharedMnt) |
| 109 | return "Shared-Mounted"; |
| 110 | else |
| 111 | return "Unknown-Error"; |
| 112 | } |
| 113 | |
| 114 | Volume::Volume(VolumeManager *vm, const char *label, const char *mount_point) { |
| 115 | mVm = vm; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 116 | mDebug = false; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 117 | mLabel = strdup(label); |
| 118 | mMountpoint = strdup(mount_point); |
| 119 | mState = Volume::State_Init; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 120 | mCurrentlyMountedKdev = -1; |
Mike Lockwood | a4886f1 | 2010-09-21 13:56:35 -0400 | [diff] [blame] | 121 | mPartIdx = -1; |
Joseph Lehrer | 507d31b | 2011-04-11 15:02:50 -0700 | [diff] [blame] | 122 | mRetryMount = false; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | Volume::~Volume() { |
| 126 | free(mLabel); |
| 127 | free(mMountpoint); |
| 128 | } |
| 129 | |
San Mehat | cb4dac8 | 2010-03-14 13:41:54 -0700 | [diff] [blame] | 130 | void Volume::protectFromAutorunStupidity() { |
| 131 | char filename[255]; |
| 132 | |
| 133 | snprintf(filename, sizeof(filename), "%s/autorun.inf", SEC_STGDIR); |
| 134 | if (!access(filename, F_OK)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 135 | SLOGW("Volume contains an autorun.inf! - removing"); |
San Mehat | cb4dac8 | 2010-03-14 13:41:54 -0700 | [diff] [blame] | 136 | /* |
| 137 | * Ensure the filename is all lower-case so |
| 138 | * the process killer can find the inode. |
| 139 | * Probably being paranoid here but meh. |
| 140 | */ |
| 141 | rename(filename, filename); |
| 142 | Process::killProcessesWithOpenFiles(filename, 2); |
| 143 | if (unlink(filename)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 144 | SLOGE("Failed to remove %s (%s)", filename, strerror(errno)); |
San Mehat | cb4dac8 | 2010-03-14 13:41:54 -0700 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | } |
| 148 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 149 | void Volume::setDebug(bool enable) { |
| 150 | mDebug = enable; |
| 151 | } |
| 152 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 153 | dev_t Volume::getDiskDevice() { |
| 154 | return MKDEV(0, 0); |
| 155 | }; |
| 156 | |
Mike Lockwood | 2dfe297 | 2010-09-17 18:50:51 -0400 | [diff] [blame] | 157 | dev_t Volume::getShareDevice() { |
| 158 | return getDiskDevice(); |
| 159 | } |
| 160 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 161 | void Volume::handleVolumeShared() { |
| 162 | } |
| 163 | |
| 164 | void Volume::handleVolumeUnshared() { |
| 165 | } |
| 166 | |
San Mehat | fd7f587 | 2009-10-12 11:32:47 -0700 | [diff] [blame] | 167 | int Volume::handleBlockEvent(NetlinkEvent *evt) { |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 168 | errno = ENOSYS; |
| 169 | return -1; |
| 170 | } |
| 171 | |
| 172 | void Volume::setState(int state) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 173 | char msg[255]; |
| 174 | int oldState = mState; |
| 175 | |
| 176 | if (oldState == state) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 177 | SLOGW("Duplicate state (%d)\n", state); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 178 | return; |
| 179 | } |
| 180 | |
Joseph Lehrer | 507d31b | 2011-04-11 15:02:50 -0700 | [diff] [blame] | 181 | if ((oldState == Volume::State_Pending) && (state != Volume::State_Idle)) { |
| 182 | mRetryMount = false; |
| 183 | } |
| 184 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 185 | mState = state; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 186 | |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 187 | SLOGD("Volume %s state changing %d (%s) -> %d (%s)", mLabel, |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 188 | oldState, stateToStr(oldState), mState, stateToStr(mState)); |
| 189 | snprintf(msg, sizeof(msg), |
| 190 | "Volume %s %s state changed from %d (%s) to %d (%s)", getLabel(), |
| 191 | getMountpoint(), oldState, stateToStr(oldState), mState, |
| 192 | stateToStr(mState)); |
| 193 | |
| 194 | mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeStateChange, |
| 195 | msg, false); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 196 | } |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 197 | |
San Mehat | dd9b8e9 | 2009-10-21 11:06:52 -0700 | [diff] [blame] | 198 | int Volume::createDeviceNode(const char *path, int major, int minor) { |
| 199 | mode_t mode = 0660 | S_IFBLK; |
| 200 | dev_t dev = (major << 8) | minor; |
| 201 | if (mknod(path, mode, dev) < 0) { |
| 202 | if (errno != EEXIST) { |
| 203 | return -1; |
| 204 | } |
| 205 | } |
| 206 | return 0; |
| 207 | } |
| 208 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 209 | int Volume::formatVol() { |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 210 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 211 | if (getState() == Volume::State_NoMedia) { |
| 212 | errno = ENODEV; |
| 213 | return -1; |
| 214 | } else if (getState() != Volume::State_Idle) { |
| 215 | errno = EBUSY; |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 216 | return -1; |
| 217 | } |
| 218 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 219 | if (isMountpointMounted(getMountpoint())) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 220 | SLOGW("Volume is idle but appears to be mounted - fixing"); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 221 | setState(Volume::State_Mounted); |
| 222 | // mCurrentlyMountedKdev = XXX |
| 223 | errno = EBUSY; |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 224 | return -1; |
| 225 | } |
| 226 | |
Mike Lockwood | a4886f1 | 2010-09-21 13:56:35 -0400 | [diff] [blame] | 227 | bool formatEntireDevice = (mPartIdx == -1); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 228 | char devicePath[255]; |
| 229 | dev_t diskNode = getDiskDevice(); |
Mike Lockwood | a4886f1 | 2010-09-21 13:56:35 -0400 | [diff] [blame] | 230 | dev_t partNode = MKDEV(MAJOR(diskNode), (formatEntireDevice ? 1 : mPartIdx)); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 231 | |
San Mehat | 2a5b8ce | 2010-03-10 12:48:57 -0800 | [diff] [blame] | 232 | setState(Volume::State_Formatting); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 233 | |
Chih-Wei Huang | 64382de | 2010-11-16 13:18:19 +0800 | [diff] [blame] | 234 | int ret = -1; |
Mike Lockwood | a4886f1 | 2010-09-21 13:56:35 -0400 | [diff] [blame] | 235 | // Only initialize the MBR if we are formatting the entire device |
| 236 | if (formatEntireDevice) { |
| 237 | sprintf(devicePath, "/dev/block/vold/%d:%d", |
| 238 | MAJOR(diskNode), MINOR(diskNode)); |
| 239 | |
| 240 | if (initializeMbr(devicePath)) { |
| 241 | SLOGE("Failed to initialize MBR (%s)", strerror(errno)); |
| 242 | goto err; |
| 243 | } |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 244 | } |
| 245 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 246 | sprintf(devicePath, "/dev/block/vold/%d:%d", |
| 247 | MAJOR(partNode), MINOR(partNode)); |
San Mehat | dd9b8e9 | 2009-10-21 11:06:52 -0700 | [diff] [blame] | 248 | |
Mike Lockwood | a4886f1 | 2010-09-21 13:56:35 -0400 | [diff] [blame] | 249 | if (mDebug) { |
| 250 | SLOGI("Formatting volume %s (%s)", getLabel(), devicePath); |
| 251 | } |
| 252 | |
San Mehat | fcf24fe | 2010-03-03 12:37:32 -0800 | [diff] [blame] | 253 | if (Fat::format(devicePath, 0)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 254 | SLOGE("Failed to format (%s)", strerror(errno)); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 255 | goto err; |
| 256 | } |
| 257 | |
Chih-Wei Huang | 64382de | 2010-11-16 13:18:19 +0800 | [diff] [blame] | 258 | ret = 0; |
| 259 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 260 | err: |
Chih-Wei Huang | 64382de | 2010-11-16 13:18:19 +0800 | [diff] [blame] | 261 | setState(Volume::State_Idle); |
| 262 | return ret; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 263 | } |
| 264 | |
| 265 | bool Volume::isMountpointMounted(const char *path) { |
| 266 | char device[256]; |
| 267 | char mount_path[256]; |
| 268 | char rest[256]; |
| 269 | FILE *fp; |
| 270 | char line[1024]; |
| 271 | |
| 272 | if (!(fp = fopen("/proc/mounts", "r"))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 273 | SLOGE("Error opening /proc/mounts (%s)", strerror(errno)); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 274 | return false; |
| 275 | } |
| 276 | |
| 277 | while(fgets(line, sizeof(line), fp)) { |
| 278 | line[strlen(line)-1] = '\0'; |
| 279 | sscanf(line, "%255s %255s %255s\n", device, mount_path, rest); |
| 280 | if (!strcmp(mount_path, path)) { |
| 281 | fclose(fp); |
| 282 | return true; |
| 283 | } |
| 284 | |
| 285 | } |
| 286 | |
| 287 | fclose(fp); |
| 288 | return false; |
| 289 | } |
| 290 | |
| 291 | int Volume::mountVol() { |
| 292 | dev_t deviceNodes[4]; |
| 293 | int n, i, rc = 0; |
| 294 | char errmsg[255]; |
Mike Lockwood | 8509494 | 2011-03-25 12:37:43 -0700 | [diff] [blame] | 295 | const char* externalStorage = getenv("EXTERNAL_STORAGE"); |
| 296 | bool primaryStorage = externalStorage && !strcmp(getMountpoint(), externalStorage); |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 297 | char decrypt_state[PROPERTY_VALUE_MAX]; |
| 298 | char crypto_state[PROPERTY_VALUE_MAX]; |
| 299 | char encrypt_progress[PROPERTY_VALUE_MAX]; |
| 300 | int flags; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 301 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 302 | property_get("vold.decrypt", decrypt_state, ""); |
| 303 | property_get("vold.encrypt_progress", encrypt_progress, ""); |
| 304 | |
| 305 | /* Don't try to mount the volumes if we have not yet entered the disk password |
| 306 | * or are in the process of encrypting. |
| 307 | */ |
| 308 | if ((getState() == Volume::State_NoMedia) || |
| 309 | ((!strcmp(decrypt_state, "1") || encrypt_progress[0]) && primaryStorage)) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 310 | snprintf(errmsg, sizeof(errmsg), |
| 311 | "Volume %s %s mount failed - no media", |
| 312 | getLabel(), getMountpoint()); |
| 313 | mVm->getBroadcaster()->sendBroadcast( |
| 314 | ResponseCode::VolumeMountFailedNoMedia, |
| 315 | errmsg, false); |
| 316 | errno = ENODEV; |
| 317 | return -1; |
| 318 | } else if (getState() != Volume::State_Idle) { |
| 319 | errno = EBUSY; |
Joseph Lehrer | 507d31b | 2011-04-11 15:02:50 -0700 | [diff] [blame] | 320 | if (getState() == Volume::State_Pending) { |
| 321 | mRetryMount = true; |
| 322 | } |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 323 | return -1; |
| 324 | } |
| 325 | |
| 326 | if (isMountpointMounted(getMountpoint())) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 327 | SLOGW("Volume is idle but appears to be mounted - fixing"); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 328 | setState(Volume::State_Mounted); |
| 329 | // mCurrentlyMountedKdev = XXX |
| 330 | return 0; |
| 331 | } |
| 332 | |
| 333 | n = getDeviceNodes((dev_t *) &deviceNodes, 4); |
| 334 | if (!n) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 335 | SLOGE("Failed to get device nodes (%s)\n", strerror(errno)); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 336 | return -1; |
| 337 | } |
| 338 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 339 | /* If we're running encrypted, and the volume is marked as encryptable and nonremovable, |
| 340 | * and vold is asking to mount the primaryStorage device, then we need to decrypt |
| 341 | * that partition, and update the volume object to point to it's new decrypted |
| 342 | * block device |
| 343 | */ |
| 344 | property_get("ro.crypto.state", crypto_state, ""); |
| 345 | flags = getFlags(); |
| 346 | if (primaryStorage && |
| 347 | ((flags & (VOL_NONREMOVABLE | VOL_ENCRYPTABLE))==(VOL_NONREMOVABLE | VOL_ENCRYPTABLE)) && |
| 348 | !strcmp(crypto_state, "encrypted") && !isDecrypted()) { |
| 349 | char new_sys_path[MAXPATHLEN]; |
| 350 | char nodepath[256]; |
| 351 | int new_major, new_minor; |
| 352 | |
| 353 | if (n != 1) { |
| 354 | /* We only expect one device node returned when mounting encryptable volumes */ |
| 355 | SLOGE("Too many device nodes returned when mounting %d\n", getMountpoint()); |
| 356 | return -1; |
| 357 | } |
| 358 | |
| 359 | if (cryptfs_setup_volume(getLabel(), MAJOR(deviceNodes[0]), MINOR(deviceNodes[0]), |
| 360 | new_sys_path, sizeof(new_sys_path), |
| 361 | &new_major, &new_minor)) { |
| 362 | SLOGE("Cannot setup encryption mapping for %d\n", getMountpoint()); |
| 363 | return -1; |
| 364 | } |
| 365 | /* We now have the new sysfs path for the decrypted block device, and the |
| 366 | * majore and minor numbers for it. So, create the device, update the |
| 367 | * path to the new sysfs path, and continue. |
| 368 | */ |
| 369 | snprintf(nodepath, |
| 370 | sizeof(nodepath), "/dev/block/vold/%d:%d", |
| 371 | new_major, new_minor); |
| 372 | if (createDeviceNode(nodepath, new_major, new_minor)) { |
| 373 | SLOGE("Error making device node '%s' (%s)", nodepath, |
| 374 | strerror(errno)); |
| 375 | } |
| 376 | |
| 377 | // Todo: Either create sys filename from nodepath, or pass in bogus path so |
| 378 | // vold ignores state changes on this internal device. |
| 379 | updateDeviceInfo(nodepath, new_major, new_minor); |
| 380 | |
| 381 | /* Get the device nodes again, because they just changed */ |
| 382 | n = getDeviceNodes((dev_t *) &deviceNodes, 4); |
| 383 | if (!n) { |
| 384 | SLOGE("Failed to get device nodes (%s)\n", strerror(errno)); |
| 385 | return -1; |
| 386 | } |
| 387 | } |
| 388 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 389 | for (i = 0; i < n; i++) { |
| 390 | char devicePath[255]; |
| 391 | |
| 392 | sprintf(devicePath, "/dev/block/vold/%d:%d", MAJOR(deviceNodes[i]), |
| 393 | MINOR(deviceNodes[i])); |
| 394 | |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 395 | SLOGI("%s being considered for volume %s\n", devicePath, getLabel()); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 396 | |
| 397 | errno = 0; |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 398 | setState(Volume::State_Checking); |
| 399 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 400 | if (Fat::check(devicePath)) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 401 | if (errno == ENODATA) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 402 | SLOGW("%s does not contain a FAT filesystem\n", devicePath); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 403 | continue; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 404 | } |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 405 | errno = EIO; |
| 406 | /* Badness - abort the mount */ |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 407 | SLOGE("%s failed FS checks (%s)", devicePath, strerror(errno)); |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 408 | setState(Volume::State_Idle); |
| 409 | return -1; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 410 | } |
| 411 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 412 | /* |
| 413 | * Mount the device on our internal staging mountpoint so we can |
| 414 | * muck with it before exposing it to non priviledged users. |
| 415 | */ |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 416 | errno = 0; |
Mike Lockwood | 9092b1d | 2011-03-23 14:55:49 -0400 | [diff] [blame] | 417 | int gid; |
| 418 | |
Mike Lockwood | 8509494 | 2011-03-25 12:37:43 -0700 | [diff] [blame] | 419 | if (primaryStorage) { |
Mike Lockwood | 9092b1d | 2011-03-23 14:55:49 -0400 | [diff] [blame] | 420 | // Special case the primary SD card. |
| 421 | // For this we grant write access to the SDCARD_RW group. |
| 422 | gid = AID_SDCARD_RW; |
| 423 | } else { |
| 424 | // For secondary external storage we keep things locked up. |
| 425 | gid = AID_MEDIA_RW; |
| 426 | } |
Kenny Root | a3e0608 | 2010-08-27 08:31:35 -0700 | [diff] [blame] | 427 | if (Fat::doMount(devicePath, "/mnt/secure/staging", false, false, false, |
Mike Lockwood | 9092b1d | 2011-03-23 14:55:49 -0400 | [diff] [blame] | 428 | AID_SYSTEM, gid, 0702, true)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 429 | SLOGE("%s failed to mount via VFAT (%s)\n", devicePath, strerror(errno)); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 430 | continue; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 431 | } |
| 432 | |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 433 | SLOGI("Device %s, target %s mounted @ /mnt/secure/staging", devicePath, getMountpoint()); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 434 | |
San Mehat | cb4dac8 | 2010-03-14 13:41:54 -0700 | [diff] [blame] | 435 | protectFromAutorunStupidity(); |
| 436 | |
Mike Lockwood | 8509494 | 2011-03-25 12:37:43 -0700 | [diff] [blame] | 437 | // only create android_secure on primary storage |
| 438 | if (primaryStorage && createBindMounts()) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 439 | SLOGE("Failed to create bindmounts (%s)", strerror(errno)); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 440 | umount("/mnt/secure/staging"); |
| 441 | setState(Volume::State_Idle); |
| 442 | return -1; |
| 443 | } |
| 444 | |
| 445 | /* |
| 446 | * Now that the bindmount trickery is done, atomically move the |
| 447 | * whole subtree to expose it to non priviledged users. |
| 448 | */ |
| 449 | if (doMoveMount("/mnt/secure/staging", getMountpoint(), false)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 450 | SLOGE("Failed to move mount (%s)", strerror(errno)); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 451 | umount("/mnt/secure/staging"); |
| 452 | setState(Volume::State_Idle); |
| 453 | return -1; |
| 454 | } |
| 455 | setState(Volume::State_Mounted); |
| 456 | mCurrentlyMountedKdev = deviceNodes[i]; |
| 457 | return 0; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 458 | } |
| 459 | |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 460 | SLOGE("Volume %s found no suitable devices for mounting :(\n", getLabel()); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 461 | setState(Volume::State_Idle); |
| 462 | |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 463 | return -1; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 464 | } |
| 465 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 466 | int Volume::createBindMounts() { |
| 467 | unsigned long flags; |
| 468 | |
| 469 | /* |
San Mehat | 52c2ccb | 2010-02-23 18:26:13 -0800 | [diff] [blame] | 470 | * Rename old /android_secure -> /.android_secure |
| 471 | */ |
| 472 | if (!access("/mnt/secure/staging/android_secure", R_OK | X_OK) && |
| 473 | access(SEC_STG_SECIMGDIR, R_OK | X_OK)) { |
| 474 | if (rename("/mnt/secure/staging/android_secure", SEC_STG_SECIMGDIR)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 475 | SLOGE("Failed to rename legacy asec dir (%s)", strerror(errno)); |
San Mehat | 52c2ccb | 2010-02-23 18:26:13 -0800 | [diff] [blame] | 476 | } |
| 477 | } |
| 478 | |
| 479 | /* |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 480 | * Ensure that /android_secure exists and is a directory |
| 481 | */ |
| 482 | if (access(SEC_STG_SECIMGDIR, R_OK | X_OK)) { |
| 483 | if (errno == ENOENT) { |
| 484 | if (mkdir(SEC_STG_SECIMGDIR, 0777)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 485 | SLOGE("Failed to create %s (%s)", SEC_STG_SECIMGDIR, strerror(errno)); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 486 | return -1; |
| 487 | } |
| 488 | } else { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 489 | SLOGE("Failed to access %s (%s)", SEC_STG_SECIMGDIR, strerror(errno)); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 490 | return -1; |
| 491 | } |
| 492 | } else { |
| 493 | struct stat sbuf; |
| 494 | |
| 495 | if (stat(SEC_STG_SECIMGDIR, &sbuf)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 496 | SLOGE("Failed to stat %s (%s)", SEC_STG_SECIMGDIR, strerror(errno)); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 497 | return -1; |
| 498 | } |
| 499 | if (!S_ISDIR(sbuf.st_mode)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 500 | SLOGE("%s is not a directory", SEC_STG_SECIMGDIR); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 501 | errno = ENOTDIR; |
| 502 | return -1; |
| 503 | } |
| 504 | } |
| 505 | |
| 506 | /* |
| 507 | * Bind mount /mnt/secure/staging/android_secure -> /mnt/secure/asec so we'll |
| 508 | * have a root only accessable mountpoint for it. |
| 509 | */ |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 510 | if (mount(SEC_STG_SECIMGDIR, SEC_ASECDIR_EXT, "", MS_BIND, NULL)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 511 | SLOGE("Failed to bind mount points %s -> %s (%s)", |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 512 | SEC_STG_SECIMGDIR, SEC_ASECDIR_EXT, strerror(errno)); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 513 | return -1; |
| 514 | } |
| 515 | |
| 516 | /* |
| 517 | * Mount a read-only, zero-sized tmpfs on <mountpoint>/android_secure to |
| 518 | * obscure the underlying directory from everybody - sneaky eh? ;) |
| 519 | */ |
| 520 | if (mount("tmpfs", SEC_STG_SECIMGDIR, "tmpfs", MS_RDONLY, "size=0,mode=000,uid=0,gid=0")) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 521 | SLOGE("Failed to obscure %s (%s)", SEC_STG_SECIMGDIR, strerror(errno)); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 522 | umount("/mnt/asec_secure"); |
| 523 | return -1; |
| 524 | } |
| 525 | |
| 526 | return 0; |
| 527 | } |
| 528 | |
| 529 | int Volume::doMoveMount(const char *src, const char *dst, bool force) { |
| 530 | unsigned int flags = MS_MOVE; |
| 531 | int retries = 5; |
| 532 | |
| 533 | while(retries--) { |
| 534 | if (!mount(src, dst, "", flags, NULL)) { |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 535 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 536 | SLOGD("Moved mount %s -> %s sucessfully", src, dst); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 537 | } |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 538 | return 0; |
| 539 | } else if (errno != EBUSY) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 540 | SLOGE("Failed to move mount %s -> %s (%s)", src, dst, strerror(errno)); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 541 | return -1; |
| 542 | } |
| 543 | int action = 0; |
| 544 | |
| 545 | if (force) { |
| 546 | if (retries == 1) { |
| 547 | action = 2; // SIGKILL |
| 548 | } else if (retries == 2) { |
| 549 | action = 1; // SIGHUP |
| 550 | } |
| 551 | } |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 552 | SLOGW("Failed to move %s -> %s (%s, retries %d, action %d)", |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 553 | src, dst, strerror(errno), retries, action); |
| 554 | Process::killProcessesWithOpenFiles(src, action); |
| 555 | usleep(1000*250); |
| 556 | } |
| 557 | |
| 558 | errno = EBUSY; |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 559 | SLOGE("Giving up on move %s -> %s (%s)", src, dst, strerror(errno)); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 560 | return -1; |
| 561 | } |
| 562 | |
| 563 | int Volume::doUnmount(const char *path, bool force) { |
| 564 | int retries = 10; |
| 565 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 566 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 567 | SLOGD("Unmounting {%s}, force = %d", path, force); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 568 | } |
| 569 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 570 | while (retries--) { |
| 571 | if (!umount(path) || errno == EINVAL || errno == ENOENT) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 572 | SLOGI("%s sucessfully unmounted", path); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 573 | return 0; |
| 574 | } |
| 575 | |
| 576 | int action = 0; |
| 577 | |
| 578 | if (force) { |
| 579 | if (retries == 1) { |
| 580 | action = 2; // SIGKILL |
| 581 | } else if (retries == 2) { |
| 582 | action = 1; // SIGHUP |
| 583 | } |
| 584 | } |
| 585 | |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 586 | SLOGW("Failed to unmount %s (%s, retries %d, action %d)", |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 587 | path, strerror(errno), retries, action); |
| 588 | |
| 589 | Process::killProcessesWithOpenFiles(path, action); |
| 590 | usleep(1000*1000); |
| 591 | } |
| 592 | errno = EBUSY; |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 593 | SLOGE("Giving up on unmount %s (%s)", path, strerror(errno)); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 594 | return -1; |
| 595 | } |
| 596 | |
Ken Sumrall | 0b8b597 | 2011-08-31 16:14:23 -0700 | [diff] [blame] | 597 | int Volume::unmountVol(bool force, bool revert) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 598 | int i, rc; |
| 599 | |
| 600 | if (getState() != Volume::State_Mounted) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 601 | SLOGE("Volume %s unmount request when not mounted", getLabel()); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 602 | errno = EINVAL; |
Ken Sumrall | 319b104 | 2011-06-14 14:01:55 -0700 | [diff] [blame] | 603 | return UNMOUNT_NOT_MOUNTED_ERR; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | setState(Volume::State_Unmounting); |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 607 | usleep(1000 * 1000); // Give the framework some time to react |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 608 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 609 | /* |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 610 | * Remove the bindmount we were using to keep a reference to |
| 611 | * the previously obscured directory. |
| 612 | */ |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 613 | if (doUnmount(Volume::SEC_ASECDIR_EXT, force)) { |
| 614 | SLOGE("Failed to remove bindmount on %s (%s)", SEC_ASECDIR_EXT, strerror(errno)); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 615 | goto fail_remount_tmpfs; |
| 616 | } |
| 617 | |
| 618 | /* |
Jeff Sharkey | 7a3c3d4 | 2012-10-04 16:49:22 -0700 | [diff] [blame] | 619 | * Unmount the tmpfs which was obscuring the asec image directory |
| 620 | * from non root users |
| 621 | */ |
| 622 | char secure_dir[PATH_MAX]; |
| 623 | snprintf(secure_dir, PATH_MAX, "%s/.android_secure", getMountpoint()); |
| 624 | if (doUnmount(secure_dir, force)) { |
| 625 | SLOGE("Failed to unmount tmpfs on %s (%s)", secure_dir, strerror(errno)); |
| 626 | goto fail_republish; |
| 627 | } |
| 628 | |
| 629 | /* |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 630 | * Finally, unmount the actual block device from the staging dir |
| 631 | */ |
Jeff Sharkey | 7a3c3d4 | 2012-10-04 16:49:22 -0700 | [diff] [blame] | 632 | if (doUnmount(getMountpoint(), force)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 633 | SLOGE("Failed to unmount %s (%s)", SEC_STGDIR, strerror(errno)); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 634 | goto fail_recreate_bindmount; |
| 635 | } |
| 636 | |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 637 | SLOGI("%s unmounted sucessfully", getMountpoint()); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 638 | |
Ken Sumrall | 0b8b597 | 2011-08-31 16:14:23 -0700 | [diff] [blame] | 639 | /* If this is an encrypted volume, and we've been asked to undo |
| 640 | * the crypto mapping, then revert the dm-crypt mapping, and revert |
| 641 | * the device info to the original values. |
| 642 | */ |
| 643 | if (revert && isDecrypted()) { |
| 644 | cryptfs_revert_volume(getLabel()); |
| 645 | revertDeviceInfo(); |
| 646 | SLOGI("Encrypted volume %s reverted successfully", getMountpoint()); |
| 647 | } |
| 648 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 649 | setState(Volume::State_Idle); |
| 650 | mCurrentlyMountedKdev = -1; |
| 651 | return 0; |
| 652 | |
| 653 | /* |
| 654 | * Failure handling - try to restore everything back the way it was |
| 655 | */ |
| 656 | fail_recreate_bindmount: |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 657 | if (mount(SEC_STG_SECIMGDIR, SEC_ASECDIR_EXT, "", MS_BIND, NULL)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 658 | SLOGE("Failed to restore bindmount after failure! - Storage will appear offline!"); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 659 | goto out_nomedia; |
| 660 | } |
| 661 | fail_remount_tmpfs: |
| 662 | if (mount("tmpfs", SEC_STG_SECIMGDIR, "tmpfs", MS_RDONLY, "size=0,mode=0,uid=0,gid=0")) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 663 | SLOGE("Failed to restore tmpfs after failure! - Storage will appear offline!"); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 664 | goto out_nomedia; |
| 665 | } |
| 666 | fail_republish: |
| 667 | if (doMoveMount(SEC_STGDIR, getMountpoint(), force)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 668 | SLOGE("Failed to republish mount after failure! - Storage will appear offline!"); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 669 | goto out_nomedia; |
| 670 | } |
| 671 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 672 | setState(Volume::State_Mounted); |
| 673 | return -1; |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 674 | |
| 675 | out_nomedia: |
| 676 | setState(Volume::State_NoMedia); |
| 677 | return -1; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 678 | } |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 679 | int Volume::initializeMbr(const char *deviceNode) { |
San Mehat | 2a5b8ce | 2010-03-10 12:48:57 -0800 | [diff] [blame] | 680 | struct disk_info dinfo; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 681 | |
San Mehat | 2a5b8ce | 2010-03-10 12:48:57 -0800 | [diff] [blame] | 682 | memset(&dinfo, 0, sizeof(dinfo)); |
| 683 | |
| 684 | if (!(dinfo.part_lst = (struct part_info *) malloc(MAX_NUM_PARTS * sizeof(struct part_info)))) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 685 | SLOGE("Failed to malloc prt_lst"); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 686 | return -1; |
| 687 | } |
| 688 | |
San Mehat | 2a5b8ce | 2010-03-10 12:48:57 -0800 | [diff] [blame] | 689 | memset(dinfo.part_lst, 0, MAX_NUM_PARTS * sizeof(struct part_info)); |
| 690 | dinfo.device = strdup(deviceNode); |
| 691 | dinfo.scheme = PART_SCHEME_MBR; |
| 692 | dinfo.sect_size = 512; |
| 693 | dinfo.skip_lba = 2048; |
| 694 | dinfo.num_lba = 0; |
| 695 | dinfo.num_parts = 1; |
| 696 | |
| 697 | struct part_info *pinfo = &dinfo.part_lst[0]; |
| 698 | |
| 699 | pinfo->name = strdup("android_sdcard"); |
| 700 | pinfo->flags |= PART_ACTIVE_FLAG; |
| 701 | pinfo->type = PC_PART_TYPE_FAT32; |
| 702 | pinfo->len_kb = -1; |
| 703 | |
| 704 | int rc = apply_disk_config(&dinfo, 0); |
| 705 | |
| 706 | if (rc) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 707 | SLOGE("Failed to apply disk configuration (%d)", rc); |
San Mehat | 2a5b8ce | 2010-03-10 12:48:57 -0800 | [diff] [blame] | 708 | goto out; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 709 | } |
| 710 | |
San Mehat | 2a5b8ce | 2010-03-10 12:48:57 -0800 | [diff] [blame] | 711 | out: |
| 712 | free(pinfo->name); |
| 713 | free(dinfo.device); |
| 714 | free(dinfo.part_lst); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 715 | |
San Mehat | 2a5b8ce | 2010-03-10 12:48:57 -0800 | [diff] [blame] | 716 | return rc; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 717 | } |