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> |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 22 | #include <mntent.h> |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 23 | |
| 24 | #include <sys/types.h> |
| 25 | #include <sys/stat.h> |
| 26 | #include <sys/types.h> |
| 27 | #include <sys/mman.h> |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 28 | #include <sys/mount.h> |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 29 | #include <sys/param.h> |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 30 | |
| 31 | #include <linux/kdev_t.h> |
| 32 | |
| 33 | #include <cutils/properties.h> |
| 34 | |
San Mehat | 2a5b8ce | 2010-03-10 12:48:57 -0800 | [diff] [blame] | 35 | #include <diskconfig/diskconfig.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 36 | |
Mike Lockwood | 9092b1d | 2011-03-23 14:55:49 -0400 | [diff] [blame] | 37 | #include <private/android_filesystem_config.h> |
| 38 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 39 | #define LOG_TAG "Vold" |
| 40 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 41 | #include <cutils/fs.h> |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 42 | #include <cutils/log.h> |
| 43 | |
Jeff Sharkey | 0de365f | 2013-10-16 16:24:19 -0700 | [diff] [blame] | 44 | #include <string> |
| 45 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 46 | #include "Volume.h" |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 47 | #include "VolumeManager.h" |
| 48 | #include "ResponseCode.h" |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 49 | #include "Fat.h" |
San Mehat | 586536c | 2010-02-16 17:12:00 -0800 | [diff] [blame] | 50 | #include "Process.h" |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 51 | #include "cryptfs.h" |
Stephen Smalley | 684e662 | 2014-09-30 10:29:24 -0400 | [diff] [blame] | 52 | #include "sehandle.h" |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 53 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 54 | extern "C" void dos_partition_dec(void const *pp, struct dos_partition *d); |
| 55 | extern "C" void dos_partition_enc(void *pp, struct dos_partition *d); |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 56 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 57 | |
| 58 | /* |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 59 | * Media directory - stuff that only media_rw user can see |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 60 | */ |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 61 | const char *Volume::MEDIA_DIR = "/mnt/media_rw"; |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 62 | |
| 63 | /* |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 64 | * Fuse directory - location where fuse wrapped filesystems go |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 65 | */ |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 66 | const char *Volume::FUSE_DIR = "/storage"; |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 67 | |
| 68 | /* |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 69 | * Path to external storage where *only* root can access ASEC image files |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 70 | */ |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 71 | const char *Volume::SEC_ASECDIR_EXT = "/mnt/secure/asec"; |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 72 | |
| 73 | /* |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 74 | * Path to internal storage where *only* root can access ASEC image files |
| 75 | */ |
| 76 | const char *Volume::SEC_ASECDIR_INT = "/data/app-asec"; |
Jeff Sharkey | 8c2c15b | 2013-10-17 15:17:19 -0700 | [diff] [blame] | 77 | |
Kenny Root | 344ca10 | 2012-04-03 17:23:01 -0700 | [diff] [blame] | 78 | /* |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 79 | * Path to where secure containers are mounted |
| 80 | */ |
| 81 | const char *Volume::ASECDIR = "/mnt/asec"; |
| 82 | |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 83 | /* |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 84 | * Path to where OBBs are mounted |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 85 | */ |
Kenny Root | 508c0e1 | 2010-07-12 09:59:49 -0700 | [diff] [blame] | 86 | const char *Volume::LOOPDIR = "/mnt/obb"; |
Kenny Root | fb7c4d5 | 2010-06-30 18:48:41 -0700 | [diff] [blame] | 87 | |
Jeff Sharkey | 0de365f | 2013-10-16 16:24:19 -0700 | [diff] [blame] | 88 | const char *Volume::BLKID_PATH = "/system/bin/blkid"; |
| 89 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 90 | static const char *stateToStr(int state) { |
| 91 | if (state == Volume::State_Init) |
| 92 | return "Initializing"; |
| 93 | else if (state == Volume::State_NoMedia) |
| 94 | return "No-Media"; |
| 95 | else if (state == Volume::State_Idle) |
| 96 | return "Idle-Unmounted"; |
| 97 | else if (state == Volume::State_Pending) |
| 98 | return "Pending"; |
| 99 | else if (state == Volume::State_Mounted) |
| 100 | return "Mounted"; |
| 101 | else if (state == Volume::State_Unmounting) |
| 102 | return "Unmounting"; |
| 103 | else if (state == Volume::State_Checking) |
| 104 | return "Checking"; |
| 105 | else if (state == Volume::State_Formatting) |
| 106 | return "Formatting"; |
| 107 | else if (state == Volume::State_Shared) |
| 108 | return "Shared-Unmounted"; |
| 109 | else if (state == Volume::State_SharedMnt) |
| 110 | return "Shared-Mounted"; |
| 111 | else |
| 112 | return "Unknown-Error"; |
| 113 | } |
| 114 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 115 | Volume::Volume(VolumeManager *vm, const fstab_rec* rec, int flags) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 116 | mVm = vm; |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 117 | mDebug = false; |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 118 | mLabel = strdup(rec->label); |
Jeff Sharkey | 0de365f | 2013-10-16 16:24:19 -0700 | [diff] [blame] | 119 | mUuid = NULL; |
| 120 | mUserLabel = NULL; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 121 | mState = Volume::State_Init; |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 122 | mFlags = flags; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 123 | mCurrentlyMountedKdev = -1; |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 124 | mPartIdx = rec->partnum; |
Joseph Lehrer | 507d31b | 2011-04-11 15:02:50 -0700 | [diff] [blame] | 125 | mRetryMount = false; |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 126 | } |
| 127 | |
| 128 | Volume::~Volume() { |
| 129 | free(mLabel); |
Jeff Sharkey | 0de365f | 2013-10-16 16:24:19 -0700 | [diff] [blame] | 130 | free(mUuid); |
| 131 | free(mUserLabel); |
San Mehat | cb4dac8 | 2010-03-14 13:41:54 -0700 | [diff] [blame] | 132 | } |
| 133 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 134 | void Volume::setDebug(bool enable) { |
| 135 | mDebug = enable; |
| 136 | } |
| 137 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 138 | dev_t Volume::getDiskDevice() { |
| 139 | return MKDEV(0, 0); |
| 140 | }; |
| 141 | |
Mike Lockwood | 2dfe297 | 2010-09-17 18:50:51 -0400 | [diff] [blame] | 142 | dev_t Volume::getShareDevice() { |
| 143 | return getDiskDevice(); |
| 144 | } |
| 145 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 146 | void Volume::handleVolumeShared() { |
| 147 | } |
| 148 | |
| 149 | void Volume::handleVolumeUnshared() { |
| 150 | } |
| 151 | |
Mark Salyzyn | 5eecc44 | 2014-02-12 14:16:14 -0800 | [diff] [blame] | 152 | int Volume::handleBlockEvent(NetlinkEvent * /*evt*/) { |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 153 | errno = ENOSYS; |
| 154 | return -1; |
| 155 | } |
| 156 | |
Jeff Sharkey | 0de365f | 2013-10-16 16:24:19 -0700 | [diff] [blame] | 157 | void Volume::setUuid(const char* uuid) { |
| 158 | char msg[256]; |
| 159 | |
| 160 | if (mUuid) { |
| 161 | free(mUuid); |
| 162 | } |
| 163 | |
| 164 | if (uuid) { |
| 165 | mUuid = strdup(uuid); |
| 166 | snprintf(msg, sizeof(msg), "%s %s \"%s\"", getLabel(), |
| 167 | getFuseMountpoint(), mUuid); |
| 168 | } else { |
| 169 | mUuid = NULL; |
| 170 | snprintf(msg, sizeof(msg), "%s %s", getLabel(), getFuseMountpoint()); |
| 171 | } |
| 172 | |
| 173 | mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeUuidChange, msg, |
| 174 | false); |
| 175 | } |
| 176 | |
| 177 | void Volume::setUserLabel(const char* userLabel) { |
| 178 | char msg[256]; |
| 179 | |
| 180 | if (mUserLabel) { |
| 181 | free(mUserLabel); |
| 182 | } |
| 183 | |
| 184 | if (userLabel) { |
| 185 | mUserLabel = strdup(userLabel); |
| 186 | snprintf(msg, sizeof(msg), "%s %s \"%s\"", getLabel(), |
| 187 | getFuseMountpoint(), mUserLabel); |
| 188 | } else { |
| 189 | mUserLabel = NULL; |
| 190 | snprintf(msg, sizeof(msg), "%s %s", getLabel(), getFuseMountpoint()); |
| 191 | } |
| 192 | |
| 193 | mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeUserLabelChange, |
| 194 | msg, false); |
| 195 | } |
| 196 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 197 | void Volume::setState(int state) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 198 | char msg[255]; |
| 199 | int oldState = mState; |
| 200 | |
| 201 | if (oldState == state) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 202 | SLOGW("Duplicate state (%d)\n", state); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 203 | return; |
| 204 | } |
| 205 | |
Joseph Lehrer | 507d31b | 2011-04-11 15:02:50 -0700 | [diff] [blame] | 206 | if ((oldState == Volume::State_Pending) && (state != Volume::State_Idle)) { |
| 207 | mRetryMount = false; |
| 208 | } |
| 209 | |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 210 | mState = state; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 211 | |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 212 | SLOGD("Volume %s state changing %d (%s) -> %d (%s)", mLabel, |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 213 | oldState, stateToStr(oldState), mState, stateToStr(mState)); |
| 214 | snprintf(msg, sizeof(msg), |
| 215 | "Volume %s %s state changed from %d (%s) to %d (%s)", getLabel(), |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 216 | getFuseMountpoint(), oldState, stateToStr(oldState), mState, |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 217 | stateToStr(mState)); |
| 218 | |
| 219 | mVm->getBroadcaster()->sendBroadcast(ResponseCode::VolumeStateChange, |
| 220 | msg, false); |
San Mehat | f1b736b | 2009-10-10 17:22:08 -0700 | [diff] [blame] | 221 | } |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 222 | |
San Mehat | dd9b8e9 | 2009-10-21 11:06:52 -0700 | [diff] [blame] | 223 | int Volume::createDeviceNode(const char *path, int major, int minor) { |
Stephen Smalley | 684e662 | 2014-09-30 10:29:24 -0400 | [diff] [blame] | 224 | char *secontext = NULL; |
San Mehat | dd9b8e9 | 2009-10-21 11:06:52 -0700 | [diff] [blame] | 225 | mode_t mode = 0660 | S_IFBLK; |
| 226 | dev_t dev = (major << 8) | minor; |
Stephen Smalley | 684e662 | 2014-09-30 10:29:24 -0400 | [diff] [blame] | 227 | int rc; |
| 228 | if (sehandle) { |
| 229 | rc = selabel_lookup(sehandle, &secontext, path, S_IFBLK); |
| 230 | if (rc == 0) |
| 231 | setfscreatecon(secontext); |
| 232 | } |
San Mehat | dd9b8e9 | 2009-10-21 11:06:52 -0700 | [diff] [blame] | 233 | if (mknod(path, mode, dev) < 0) { |
| 234 | if (errno != EEXIST) { |
Stephen Smalley | 684e662 | 2014-09-30 10:29:24 -0400 | [diff] [blame] | 235 | int sverrno = errno; |
| 236 | if (secontext) { |
| 237 | freecon(secontext); |
| 238 | setfscreatecon(NULL); |
| 239 | } |
| 240 | errno = sverrno; |
San Mehat | dd9b8e9 | 2009-10-21 11:06:52 -0700 | [diff] [blame] | 241 | return -1; |
| 242 | } |
| 243 | } |
Stephen Smalley | 684e662 | 2014-09-30 10:29:24 -0400 | [diff] [blame] | 244 | if (secontext) { |
| 245 | setfscreatecon(NULL); |
| 246 | freecon(secontext); |
| 247 | } |
San Mehat | dd9b8e9 | 2009-10-21 11:06:52 -0700 | [diff] [blame] | 248 | return 0; |
| 249 | } |
| 250 | |
Ken Sumrall | 9caab76 | 2013-06-11 19:10:20 -0700 | [diff] [blame] | 251 | int Volume::formatVol(bool wipe) { |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 252 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 253 | if (getState() == Volume::State_NoMedia) { |
| 254 | errno = ENODEV; |
| 255 | return -1; |
| 256 | } else if (getState() != Volume::State_Idle) { |
| 257 | errno = EBUSY; |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 258 | return -1; |
| 259 | } |
| 260 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 261 | if (isMountpointMounted(getMountpoint())) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 262 | SLOGW("Volume is idle but appears to be mounted - fixing"); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 263 | setState(Volume::State_Mounted); |
| 264 | // mCurrentlyMountedKdev = XXX |
| 265 | errno = EBUSY; |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 266 | return -1; |
| 267 | } |
| 268 | |
Mike Lockwood | a4886f1 | 2010-09-21 13:56:35 -0400 | [diff] [blame] | 269 | bool formatEntireDevice = (mPartIdx == -1); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 270 | char devicePath[255]; |
| 271 | dev_t diskNode = getDiskDevice(); |
Blair Prescott | cc21d6e | 2013-10-21 15:25:37 -0700 | [diff] [blame] | 272 | dev_t partNode = |
| 273 | MKDEV(MAJOR(diskNode), |
Bogdan George Stefan | 1a89e8b | 2014-06-30 15:40:03 +0300 | [diff] [blame] | 274 | MINOR(diskNode) + (formatEntireDevice ? 0 : mPartIdx)); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 275 | |
San Mehat | 2a5b8ce | 2010-03-10 12:48:57 -0800 | [diff] [blame] | 276 | setState(Volume::State_Formatting); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 277 | |
Chih-Wei Huang | 64382de | 2010-11-16 13:18:19 +0800 | [diff] [blame] | 278 | int ret = -1; |
Mike Lockwood | a4886f1 | 2010-09-21 13:56:35 -0400 | [diff] [blame] | 279 | // Only initialize the MBR if we are formatting the entire device |
| 280 | if (formatEntireDevice) { |
| 281 | sprintf(devicePath, "/dev/block/vold/%d:%d", |
Colin Cross | 346c5b2 | 2014-01-22 23:59:41 -0800 | [diff] [blame] | 282 | major(diskNode), minor(diskNode)); |
Mike Lockwood | a4886f1 | 2010-09-21 13:56:35 -0400 | [diff] [blame] | 283 | |
| 284 | if (initializeMbr(devicePath)) { |
| 285 | SLOGE("Failed to initialize MBR (%s)", strerror(errno)); |
| 286 | goto err; |
| 287 | } |
San Mehat | 49e2bce | 2009-10-12 16:29:01 -0700 | [diff] [blame] | 288 | } |
| 289 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 290 | sprintf(devicePath, "/dev/block/vold/%d:%d", |
Colin Cross | 346c5b2 | 2014-01-22 23:59:41 -0800 | [diff] [blame] | 291 | major(partNode), minor(partNode)); |
San Mehat | dd9b8e9 | 2009-10-21 11:06:52 -0700 | [diff] [blame] | 292 | |
Mike Lockwood | a4886f1 | 2010-09-21 13:56:35 -0400 | [diff] [blame] | 293 | if (mDebug) { |
| 294 | SLOGI("Formatting volume %s (%s)", getLabel(), devicePath); |
| 295 | } |
| 296 | |
Ken Sumrall | 9caab76 | 2013-06-11 19:10:20 -0700 | [diff] [blame] | 297 | if (Fat::format(devicePath, 0, wipe)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 298 | SLOGE("Failed to format (%s)", strerror(errno)); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 299 | goto err; |
| 300 | } |
| 301 | |
Chih-Wei Huang | 64382de | 2010-11-16 13:18:19 +0800 | [diff] [blame] | 302 | ret = 0; |
| 303 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 304 | err: |
Chih-Wei Huang | 64382de | 2010-11-16 13:18:19 +0800 | [diff] [blame] | 305 | setState(Volume::State_Idle); |
| 306 | return ret; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | bool Volume::isMountpointMounted(const char *path) { |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 310 | FILE *fp = setmntent("/proc/mounts", "r"); |
| 311 | if (fp == NULL) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 312 | SLOGE("Error opening /proc/mounts (%s)", strerror(errno)); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 313 | return false; |
| 314 | } |
| 315 | |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 316 | bool found_path = false; |
| 317 | mntent* mentry; |
| 318 | while ((mentry = getmntent(fp)) != NULL) { |
| 319 | if (strcmp(mentry->mnt_dir, path) == 0) { |
| 320 | found_path = true; |
| 321 | break; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 322 | } |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 323 | } |
Yabin Cui | d1104f7 | 2015-01-02 13:28:28 -0800 | [diff] [blame] | 324 | endmntent(fp); |
| 325 | return found_path; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 326 | } |
| 327 | |
| 328 | int Volume::mountVol() { |
| 329 | dev_t deviceNodes[4]; |
Tim Murray | 8439dc9 | 2014-12-15 11:56:11 -0800 | [diff] [blame] | 330 | int n, i; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 331 | char errmsg[255]; |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 332 | |
| 333 | int flags = getFlags(); |
| 334 | bool providesAsec = (flags & VOL_PROVIDES_ASEC) != 0; |
| 335 | |
| 336 | // TODO: handle "bind" style mounts, for emulated storage |
| 337 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 338 | char decrypt_state[PROPERTY_VALUE_MAX]; |
| 339 | char crypto_state[PROPERTY_VALUE_MAX]; |
| 340 | char encrypt_progress[PROPERTY_VALUE_MAX]; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 341 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 342 | property_get("vold.decrypt", decrypt_state, ""); |
| 343 | property_get("vold.encrypt_progress", encrypt_progress, ""); |
| 344 | |
| 345 | /* Don't try to mount the volumes if we have not yet entered the disk password |
| 346 | * or are in the process of encrypting. |
| 347 | */ |
| 348 | if ((getState() == Volume::State_NoMedia) || |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 349 | ((!strcmp(decrypt_state, "1") || encrypt_progress[0]) && providesAsec)) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 350 | snprintf(errmsg, sizeof(errmsg), |
| 351 | "Volume %s %s mount failed - no media", |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 352 | getLabel(), getFuseMountpoint()); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 353 | mVm->getBroadcaster()->sendBroadcast( |
| 354 | ResponseCode::VolumeMountFailedNoMedia, |
| 355 | errmsg, false); |
| 356 | errno = ENODEV; |
| 357 | return -1; |
| 358 | } else if (getState() != Volume::State_Idle) { |
| 359 | errno = EBUSY; |
Joseph Lehrer | 507d31b | 2011-04-11 15:02:50 -0700 | [diff] [blame] | 360 | if (getState() == Volume::State_Pending) { |
| 361 | mRetryMount = true; |
| 362 | } |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 363 | return -1; |
| 364 | } |
| 365 | |
| 366 | if (isMountpointMounted(getMountpoint())) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 367 | SLOGW("Volume is idle but appears to be mounted - fixing"); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 368 | setState(Volume::State_Mounted); |
| 369 | // mCurrentlyMountedKdev = XXX |
| 370 | return 0; |
| 371 | } |
| 372 | |
| 373 | n = getDeviceNodes((dev_t *) &deviceNodes, 4); |
| 374 | if (!n) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 375 | SLOGE("Failed to get device nodes (%s)\n", strerror(errno)); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 376 | return -1; |
| 377 | } |
| 378 | |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 379 | /* If we're running encrypted, and the volume is marked as encryptable and nonremovable, |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 380 | * and also marked as providing Asec storage, then we need to decrypt |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 381 | * that partition, and update the volume object to point to it's new decrypted |
| 382 | * block device |
| 383 | */ |
| 384 | property_get("ro.crypto.state", crypto_state, ""); |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 385 | if (providesAsec && |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 386 | ((flags & (VOL_NONREMOVABLE | VOL_ENCRYPTABLE))==(VOL_NONREMOVABLE | VOL_ENCRYPTABLE)) && |
| 387 | !strcmp(crypto_state, "encrypted") && !isDecrypted()) { |
| 388 | char new_sys_path[MAXPATHLEN]; |
| 389 | char nodepath[256]; |
| 390 | int new_major, new_minor; |
| 391 | |
| 392 | if (n != 1) { |
| 393 | /* We only expect one device node returned when mounting encryptable volumes */ |
Colin Cross | 59846b6 | 2014-02-06 20:34:29 -0800 | [diff] [blame] | 394 | SLOGE("Too many device nodes returned when mounting %s\n", getMountpoint()); |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 395 | return -1; |
| 396 | } |
| 397 | |
| 398 | if (cryptfs_setup_volume(getLabel(), MAJOR(deviceNodes[0]), MINOR(deviceNodes[0]), |
| 399 | new_sys_path, sizeof(new_sys_path), |
| 400 | &new_major, &new_minor)) { |
Colin Cross | 59846b6 | 2014-02-06 20:34:29 -0800 | [diff] [blame] | 401 | SLOGE("Cannot setup encryption mapping for %s\n", getMountpoint()); |
Ken Sumrall | 29d8da8 | 2011-05-18 17:20:07 -0700 | [diff] [blame] | 402 | return -1; |
| 403 | } |
| 404 | /* We now have the new sysfs path for the decrypted block device, and the |
| 405 | * majore and minor numbers for it. So, create the device, update the |
| 406 | * path to the new sysfs path, and continue. |
| 407 | */ |
| 408 | snprintf(nodepath, |
| 409 | sizeof(nodepath), "/dev/block/vold/%d:%d", |
| 410 | new_major, new_minor); |
| 411 | if (createDeviceNode(nodepath, new_major, new_minor)) { |
| 412 | SLOGE("Error making device node '%s' (%s)", nodepath, |
| 413 | strerror(errno)); |
| 414 | } |
| 415 | |
| 416 | // Todo: Either create sys filename from nodepath, or pass in bogus path so |
| 417 | // vold ignores state changes on this internal device. |
| 418 | updateDeviceInfo(nodepath, new_major, new_minor); |
| 419 | |
| 420 | /* Get the device nodes again, because they just changed */ |
| 421 | n = getDeviceNodes((dev_t *) &deviceNodes, 4); |
| 422 | if (!n) { |
| 423 | SLOGE("Failed to get device nodes (%s)\n", strerror(errno)); |
| 424 | return -1; |
| 425 | } |
| 426 | } |
| 427 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 428 | for (i = 0; i < n; i++) { |
| 429 | char devicePath[255]; |
| 430 | |
Colin Cross | 346c5b2 | 2014-01-22 23:59:41 -0800 | [diff] [blame] | 431 | sprintf(devicePath, "/dev/block/vold/%d:%d", major(deviceNodes[i]), |
| 432 | minor(deviceNodes[i])); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 433 | |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 434 | SLOGI("%s being considered for volume %s\n", devicePath, getLabel()); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 435 | |
| 436 | errno = 0; |
San Mehat | bf04185 | 2010-01-04 10:09:16 -0800 | [diff] [blame] | 437 | setState(Volume::State_Checking); |
| 438 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 439 | if (Fat::check(devicePath)) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 440 | if (errno == ENODATA) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 441 | SLOGW("%s does not contain a FAT filesystem\n", devicePath); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 442 | continue; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 443 | } |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 444 | errno = EIO; |
| 445 | /* Badness - abort the mount */ |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 446 | SLOGE("%s failed FS checks (%s)", devicePath, strerror(errno)); |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 447 | setState(Volume::State_Idle); |
| 448 | return -1; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 449 | } |
| 450 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 451 | errno = 0; |
Mike Lockwood | 9092b1d | 2011-03-23 14:55:49 -0400 | [diff] [blame] | 452 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 453 | if (Fat::doMount(devicePath, getMountpoint(), false, false, false, |
| 454 | AID_MEDIA_RW, AID_MEDIA_RW, 0007, true)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 455 | SLOGE("%s failed to mount via VFAT (%s)\n", devicePath, strerror(errno)); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 456 | continue; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 457 | } |
| 458 | |
Jeff Sharkey | 0de365f | 2013-10-16 16:24:19 -0700 | [diff] [blame] | 459 | extractMetadata(devicePath); |
San Mehat | cb4dac8 | 2010-03-14 13:41:54 -0700 | [diff] [blame] | 460 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 461 | if (providesAsec && mountAsecExternal() != 0) { |
| 462 | SLOGE("Failed to mount secure area (%s)", strerror(errno)); |
| 463 | umount(getMountpoint()); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 464 | setState(Volume::State_Idle); |
| 465 | return -1; |
| 466 | } |
| 467 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 468 | char service[64]; |
| 469 | snprintf(service, 64, "fuse_%s", getLabel()); |
| 470 | property_set("ctl.start", service); |
| 471 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 472 | setState(Volume::State_Mounted); |
| 473 | mCurrentlyMountedKdev = deviceNodes[i]; |
| 474 | return 0; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 475 | } |
| 476 | |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 477 | SLOGE("Volume %s found no suitable devices for mounting :(\n", getLabel()); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 478 | setState(Volume::State_Idle); |
| 479 | |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 480 | return -1; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 481 | } |
| 482 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 483 | int Volume::mountAsecExternal() { |
| 484 | char legacy_path[PATH_MAX]; |
| 485 | char secure_path[PATH_MAX]; |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 486 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 487 | snprintf(legacy_path, PATH_MAX, "%s/android_secure", getMountpoint()); |
| 488 | snprintf(secure_path, PATH_MAX, "%s/.android_secure", getMountpoint()); |
| 489 | |
| 490 | // Recover legacy secure path |
| 491 | if (!access(legacy_path, R_OK | X_OK) && access(secure_path, R_OK | X_OK)) { |
| 492 | if (rename(legacy_path, secure_path)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 493 | SLOGE("Failed to rename legacy asec dir (%s)", strerror(errno)); |
San Mehat | 52c2ccb | 2010-02-23 18:26:13 -0800 | [diff] [blame] | 494 | } |
| 495 | } |
| 496 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 497 | if (fs_prepare_dir(secure_path, 0770, AID_MEDIA_RW, AID_MEDIA_RW) != 0) { |
Jeff Sharkey | 8c2c15b | 2013-10-17 15:17:19 -0700 | [diff] [blame] | 498 | SLOGW("fs_prepare_dir failed: %s", strerror(errno)); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 499 | return -1; |
| 500 | } |
| 501 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 502 | if (mount(secure_path, SEC_ASECDIR_EXT, "", MS_BIND, NULL)) { |
| 503 | SLOGE("Failed to bind mount points %s -> %s (%s)", secure_path, |
| 504 | SEC_ASECDIR_EXT, strerror(errno)); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 505 | return -1; |
| 506 | } |
| 507 | |
| 508 | return 0; |
| 509 | } |
| 510 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 511 | int Volume::doUnmount(const char *path, bool force) { |
| 512 | int retries = 10; |
| 513 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 514 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 515 | SLOGD("Unmounting {%s}, force = %d", path, force); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 516 | } |
| 517 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 518 | while (retries--) { |
| 519 | if (!umount(path) || errno == EINVAL || errno == ENOENT) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 520 | SLOGI("%s sucessfully unmounted", path); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 521 | return 0; |
| 522 | } |
| 523 | |
| 524 | int action = 0; |
| 525 | |
| 526 | if (force) { |
| 527 | if (retries == 1) { |
| 528 | action = 2; // SIGKILL |
| 529 | } else if (retries == 2) { |
| 530 | action = 1; // SIGHUP |
| 531 | } |
| 532 | } |
| 533 | |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 534 | SLOGW("Failed to unmount %s (%s, retries %d, action %d)", |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 535 | path, strerror(errno), retries, action); |
| 536 | |
| 537 | Process::killProcessesWithOpenFiles(path, action); |
| 538 | usleep(1000*1000); |
| 539 | } |
| 540 | errno = EBUSY; |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 541 | SLOGE("Giving up on unmount %s (%s)", path, strerror(errno)); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 542 | return -1; |
| 543 | } |
| 544 | |
Ken Sumrall | 0b8b597 | 2011-08-31 16:14:23 -0700 | [diff] [blame] | 545 | int Volume::unmountVol(bool force, bool revert) { |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 546 | int flags = getFlags(); |
| 547 | bool providesAsec = (flags & VOL_PROVIDES_ASEC) != 0; |
| 548 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 549 | if (getState() != Volume::State_Mounted) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 550 | SLOGE("Volume %s unmount request when not mounted", getLabel()); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 551 | errno = EINVAL; |
Ken Sumrall | 319b104 | 2011-06-14 14:01:55 -0700 | [diff] [blame] | 552 | return UNMOUNT_NOT_MOUNTED_ERR; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 553 | } |
| 554 | |
| 555 | setState(Volume::State_Unmounting); |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 556 | usleep(1000 * 1000); // Give the framework some time to react |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 557 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 558 | char service[64]; |
| 559 | snprintf(service, 64, "fuse_%s", getLabel()); |
| 560 | property_set("ctl.stop", service); |
| 561 | /* Give it a chance to stop. I wish we had a synchronous way to determine this... */ |
| 562 | sleep(1); |
| 563 | |
| 564 | // TODO: determine failure mode if FUSE times out |
| 565 | |
| 566 | if (providesAsec && doUnmount(Volume::SEC_ASECDIR_EXT, force) != 0) { |
| 567 | SLOGE("Failed to unmount secure area on %s (%s)", getMountpoint(), strerror(errno)); |
| 568 | goto out_mounted; |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 569 | } |
| 570 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 571 | /* Now that the fuse daemon is dead, unmount it */ |
| 572 | if (doUnmount(getFuseMountpoint(), force) != 0) { |
| 573 | SLOGE("Failed to unmount %s (%s)", getFuseMountpoint(), strerror(errno)); |
| 574 | goto fail_remount_secure; |
Jeff Sharkey | 7a3c3d4 | 2012-10-04 16:49:22 -0700 | [diff] [blame] | 575 | } |
| 576 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 577 | /* Unmount the real sd card */ |
| 578 | if (doUnmount(getMountpoint(), force) != 0) { |
| 579 | SLOGE("Failed to unmount %s (%s)", getMountpoint(), strerror(errno)); |
| 580 | goto fail_remount_secure; |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 581 | } |
| 582 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 583 | SLOGI("%s unmounted successfully", getMountpoint()); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 584 | |
Ken Sumrall | 0b8b597 | 2011-08-31 16:14:23 -0700 | [diff] [blame] | 585 | /* If this is an encrypted volume, and we've been asked to undo |
| 586 | * the crypto mapping, then revert the dm-crypt mapping, and revert |
| 587 | * the device info to the original values. |
| 588 | */ |
| 589 | if (revert && isDecrypted()) { |
| 590 | cryptfs_revert_volume(getLabel()); |
| 591 | revertDeviceInfo(); |
| 592 | SLOGI("Encrypted volume %s reverted successfully", getMountpoint()); |
| 593 | } |
| 594 | |
Jeff Sharkey | 0de365f | 2013-10-16 16:24:19 -0700 | [diff] [blame] | 595 | setUuid(NULL); |
| 596 | setUserLabel(NULL); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 597 | setState(Volume::State_Idle); |
| 598 | mCurrentlyMountedKdev = -1; |
| 599 | return 0; |
| 600 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 601 | fail_remount_secure: |
| 602 | if (providesAsec && mountAsecExternal() != 0) { |
| 603 | SLOGE("Failed to remount secure area (%s)", strerror(errno)); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 604 | goto out_nomedia; |
| 605 | } |
| 606 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 607 | out_mounted: |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 608 | setState(Volume::State_Mounted); |
| 609 | return -1; |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 610 | |
| 611 | out_nomedia: |
| 612 | setState(Volume::State_NoMedia); |
| 613 | return -1; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 614 | } |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 615 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 616 | int Volume::initializeMbr(const char *deviceNode) { |
San Mehat | 2a5b8ce | 2010-03-10 12:48:57 -0800 | [diff] [blame] | 617 | struct disk_info dinfo; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 618 | |
San Mehat | 2a5b8ce | 2010-03-10 12:48:57 -0800 | [diff] [blame] | 619 | memset(&dinfo, 0, sizeof(dinfo)); |
| 620 | |
| 621 | 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] | 622 | SLOGE("Failed to malloc prt_lst"); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 623 | return -1; |
| 624 | } |
| 625 | |
San Mehat | 2a5b8ce | 2010-03-10 12:48:57 -0800 | [diff] [blame] | 626 | memset(dinfo.part_lst, 0, MAX_NUM_PARTS * sizeof(struct part_info)); |
| 627 | dinfo.device = strdup(deviceNode); |
| 628 | dinfo.scheme = PART_SCHEME_MBR; |
| 629 | dinfo.sect_size = 512; |
| 630 | dinfo.skip_lba = 2048; |
| 631 | dinfo.num_lba = 0; |
| 632 | dinfo.num_parts = 1; |
| 633 | |
| 634 | struct part_info *pinfo = &dinfo.part_lst[0]; |
| 635 | |
| 636 | pinfo->name = strdup("android_sdcard"); |
| 637 | pinfo->flags |= PART_ACTIVE_FLAG; |
| 638 | pinfo->type = PC_PART_TYPE_FAT32; |
| 639 | pinfo->len_kb = -1; |
| 640 | |
| 641 | int rc = apply_disk_config(&dinfo, 0); |
| 642 | |
| 643 | if (rc) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 644 | SLOGE("Failed to apply disk configuration (%d)", rc); |
San Mehat | 2a5b8ce | 2010-03-10 12:48:57 -0800 | [diff] [blame] | 645 | goto out; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 646 | } |
| 647 | |
San Mehat | 2a5b8ce | 2010-03-10 12:48:57 -0800 | [diff] [blame] | 648 | out: |
| 649 | free(pinfo->name); |
| 650 | free(dinfo.device); |
| 651 | free(dinfo.part_lst); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 652 | |
San Mehat | 2a5b8ce | 2010-03-10 12:48:57 -0800 | [diff] [blame] | 653 | return rc; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 654 | } |
Jeff Sharkey | 0de365f | 2013-10-16 16:24:19 -0700 | [diff] [blame] | 655 | |
| 656 | /* |
| 657 | * Use blkid to extract UUID and label from device, since it handles many |
| 658 | * obscure edge cases around partition types and formats. Always broadcasts |
| 659 | * updated metadata values. |
| 660 | */ |
| 661 | int Volume::extractMetadata(const char* devicePath) { |
| 662 | int res = 0; |
| 663 | |
| 664 | std::string cmd; |
| 665 | cmd = BLKID_PATH; |
| 666 | cmd += " -c /dev/null "; |
| 667 | cmd += devicePath; |
| 668 | |
| 669 | FILE* fp = popen(cmd.c_str(), "r"); |
| 670 | if (!fp) { |
| 671 | ALOGE("Failed to run %s: %s", cmd.c_str(), strerror(errno)); |
| 672 | res = -1; |
| 673 | goto done; |
| 674 | } |
| 675 | |
| 676 | char line[1024]; |
| 677 | char value[128]; |
| 678 | if (fgets(line, sizeof(line), fp) != NULL) { |
Jeff Sharkey | 8c2c15b | 2013-10-17 15:17:19 -0700 | [diff] [blame] | 679 | ALOGD("blkid identified as %s", line); |
Jeff Sharkey | 0de365f | 2013-10-16 16:24:19 -0700 | [diff] [blame] | 680 | |
Jeff Sharkey | 6bcd362 | 2013-11-11 14:46:39 -0800 | [diff] [blame] | 681 | char* start = strstr(line, "UUID="); |
| 682 | if (start != NULL && sscanf(start + 5, "\"%127[^\"]\"", value) == 1) { |
Jeff Sharkey | 0de365f | 2013-10-16 16:24:19 -0700 | [diff] [blame] | 683 | setUuid(value); |
| 684 | } else { |
| 685 | setUuid(NULL); |
| 686 | } |
| 687 | |
Jeff Sharkey | 6bcd362 | 2013-11-11 14:46:39 -0800 | [diff] [blame] | 688 | start = strstr(line, "LABEL="); |
| 689 | if (start != NULL && sscanf(start + 6, "\"%127[^\"]\"", value) == 1) { |
Jeff Sharkey | 0de365f | 2013-10-16 16:24:19 -0700 | [diff] [blame] | 690 | setUserLabel(value); |
| 691 | } else { |
| 692 | setUserLabel(NULL); |
| 693 | } |
| 694 | } else { |
Jeff Sharkey | 8c2c15b | 2013-10-17 15:17:19 -0700 | [diff] [blame] | 695 | ALOGW("blkid failed to identify %s", devicePath); |
Jeff Sharkey | 0de365f | 2013-10-16 16:24:19 -0700 | [diff] [blame] | 696 | res = -1; |
| 697 | } |
| 698 | |
| 699 | pclose(fp); |
| 700 | |
| 701 | done: |
| 702 | if (res == -1) { |
| 703 | setUuid(NULL); |
| 704 | setUserLabel(NULL); |
| 705 | } |
| 706 | return res; |
| 707 | } |