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