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]; |
| 335 | int n, i, rc = 0; |
| 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 | int gid; |
| 458 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 459 | if (Fat::doMount(devicePath, getMountpoint(), false, false, false, |
| 460 | AID_MEDIA_RW, AID_MEDIA_RW, 0007, true)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 461 | SLOGE("%s failed to mount via VFAT (%s)\n", devicePath, strerror(errno)); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 462 | continue; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 463 | } |
| 464 | |
Jeff Sharkey | 0de365f | 2013-10-16 16:24:19 -0700 | [diff] [blame] | 465 | extractMetadata(devicePath); |
San Mehat | cb4dac8 | 2010-03-14 13:41:54 -0700 | [diff] [blame] | 466 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 467 | if (providesAsec && mountAsecExternal() != 0) { |
| 468 | SLOGE("Failed to mount secure area (%s)", strerror(errno)); |
| 469 | umount(getMountpoint()); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 470 | setState(Volume::State_Idle); |
| 471 | return -1; |
| 472 | } |
| 473 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 474 | char service[64]; |
| 475 | snprintf(service, 64, "fuse_%s", getLabel()); |
| 476 | property_set("ctl.start", service); |
| 477 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 478 | setState(Volume::State_Mounted); |
| 479 | mCurrentlyMountedKdev = deviceNodes[i]; |
| 480 | return 0; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 481 | } |
| 482 | |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 483 | SLOGE("Volume %s found no suitable devices for mounting :(\n", getLabel()); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 484 | setState(Volume::State_Idle); |
| 485 | |
San Mehat | eba65e9 | 2010-01-29 05:15:16 -0800 | [diff] [blame] | 486 | return -1; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 487 | } |
| 488 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 489 | int Volume::mountAsecExternal() { |
| 490 | char legacy_path[PATH_MAX]; |
| 491 | char secure_path[PATH_MAX]; |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 492 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 493 | snprintf(legacy_path, PATH_MAX, "%s/android_secure", getMountpoint()); |
| 494 | snprintf(secure_path, PATH_MAX, "%s/.android_secure", getMountpoint()); |
| 495 | |
| 496 | // Recover legacy secure path |
| 497 | if (!access(legacy_path, R_OK | X_OK) && access(secure_path, R_OK | X_OK)) { |
| 498 | if (rename(legacy_path, secure_path)) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 499 | SLOGE("Failed to rename legacy asec dir (%s)", strerror(errno)); |
San Mehat | 52c2ccb | 2010-02-23 18:26:13 -0800 | [diff] [blame] | 500 | } |
| 501 | } |
| 502 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 503 | 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] | 504 | SLOGW("fs_prepare_dir failed: %s", strerror(errno)); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 505 | return -1; |
| 506 | } |
| 507 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 508 | if (mount(secure_path, SEC_ASECDIR_EXT, "", MS_BIND, NULL)) { |
| 509 | SLOGE("Failed to bind mount points %s -> %s (%s)", secure_path, |
| 510 | SEC_ASECDIR_EXT, strerror(errno)); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 511 | return -1; |
| 512 | } |
| 513 | |
| 514 | return 0; |
| 515 | } |
| 516 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 517 | int Volume::doUnmount(const char *path, bool force) { |
| 518 | int retries = 10; |
| 519 | |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 520 | if (mDebug) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 521 | SLOGD("Unmounting {%s}, force = %d", path, force); |
San Mehat | d9a4e35 | 2010-03-12 13:32:47 -0800 | [diff] [blame] | 522 | } |
| 523 | |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 524 | while (retries--) { |
| 525 | if (!umount(path) || errno == EINVAL || errno == ENOENT) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 526 | SLOGI("%s sucessfully unmounted", path); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 527 | return 0; |
| 528 | } |
| 529 | |
| 530 | int action = 0; |
| 531 | |
| 532 | if (force) { |
| 533 | if (retries == 1) { |
| 534 | action = 2; // SIGKILL |
| 535 | } else if (retries == 2) { |
| 536 | action = 1; // SIGHUP |
| 537 | } |
| 538 | } |
| 539 | |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 540 | SLOGW("Failed to unmount %s (%s, retries %d, action %d)", |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 541 | path, strerror(errno), retries, action); |
| 542 | |
| 543 | Process::killProcessesWithOpenFiles(path, action); |
| 544 | usleep(1000*1000); |
| 545 | } |
| 546 | errno = EBUSY; |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 547 | SLOGE("Giving up on unmount %s (%s)", path, strerror(errno)); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 548 | return -1; |
| 549 | } |
| 550 | |
Ken Sumrall | 0b8b597 | 2011-08-31 16:14:23 -0700 | [diff] [blame] | 551 | int Volume::unmountVol(bool force, bool revert) { |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 552 | int i, rc; |
| 553 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 554 | int flags = getFlags(); |
| 555 | bool providesAsec = (flags & VOL_PROVIDES_ASEC) != 0; |
| 556 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 557 | if (getState() != Volume::State_Mounted) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 558 | SLOGE("Volume %s unmount request when not mounted", getLabel()); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 559 | errno = EINVAL; |
Ken Sumrall | 319b104 | 2011-06-14 14:01:55 -0700 | [diff] [blame] | 560 | return UNMOUNT_NOT_MOUNTED_ERR; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | setState(Volume::State_Unmounting); |
San Mehat | 4ba8948 | 2010-02-18 09:00:18 -0800 | [diff] [blame] | 564 | usleep(1000 * 1000); // Give the framework some time to react |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 565 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 566 | char service[64]; |
| 567 | snprintf(service, 64, "fuse_%s", getLabel()); |
| 568 | property_set("ctl.stop", service); |
| 569 | /* Give it a chance to stop. I wish we had a synchronous way to determine this... */ |
| 570 | sleep(1); |
| 571 | |
| 572 | // TODO: determine failure mode if FUSE times out |
| 573 | |
| 574 | if (providesAsec && doUnmount(Volume::SEC_ASECDIR_EXT, force) != 0) { |
| 575 | SLOGE("Failed to unmount secure area on %s (%s)", getMountpoint(), strerror(errno)); |
| 576 | goto out_mounted; |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 577 | } |
| 578 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 579 | /* Now that the fuse daemon is dead, unmount it */ |
| 580 | if (doUnmount(getFuseMountpoint(), force) != 0) { |
| 581 | SLOGE("Failed to unmount %s (%s)", getFuseMountpoint(), strerror(errno)); |
| 582 | goto fail_remount_secure; |
Jeff Sharkey | 7a3c3d4 | 2012-10-04 16:49:22 -0700 | [diff] [blame] | 583 | } |
| 584 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 585 | /* Unmount the real sd card */ |
| 586 | if (doUnmount(getMountpoint(), force) != 0) { |
| 587 | SLOGE("Failed to unmount %s (%s)", getMountpoint(), strerror(errno)); |
| 588 | goto fail_remount_secure; |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 589 | } |
| 590 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 591 | SLOGI("%s unmounted successfully", getMountpoint()); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 592 | |
Ken Sumrall | 0b8b597 | 2011-08-31 16:14:23 -0700 | [diff] [blame] | 593 | /* If this is an encrypted volume, and we've been asked to undo |
| 594 | * the crypto mapping, then revert the dm-crypt mapping, and revert |
| 595 | * the device info to the original values. |
| 596 | */ |
| 597 | if (revert && isDecrypted()) { |
| 598 | cryptfs_revert_volume(getLabel()); |
| 599 | revertDeviceInfo(); |
| 600 | SLOGI("Encrypted volume %s reverted successfully", getMountpoint()); |
| 601 | } |
| 602 | |
Jeff Sharkey | 0de365f | 2013-10-16 16:24:19 -0700 | [diff] [blame] | 603 | setUuid(NULL); |
| 604 | setUserLabel(NULL); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 605 | setState(Volume::State_Idle); |
| 606 | mCurrentlyMountedKdev = -1; |
| 607 | return 0; |
| 608 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 609 | fail_remount_secure: |
| 610 | if (providesAsec && mountAsecExternal() != 0) { |
| 611 | SLOGE("Failed to remount secure area (%s)", strerror(errno)); |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 612 | goto out_nomedia; |
| 613 | } |
| 614 | |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 615 | out_mounted: |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 616 | setState(Volume::State_Mounted); |
| 617 | return -1; |
San Mehat | 3bb6020 | 2010-02-19 18:14:36 -0800 | [diff] [blame] | 618 | |
| 619 | out_nomedia: |
| 620 | setState(Volume::State_NoMedia); |
| 621 | return -1; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 622 | } |
Jeff Sharkey | ba6ae8d | 2013-07-15 18:14:25 -0700 | [diff] [blame] | 623 | |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 624 | int Volume::initializeMbr(const char *deviceNode) { |
San Mehat | 2a5b8ce | 2010-03-10 12:48:57 -0800 | [diff] [blame] | 625 | struct disk_info dinfo; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 626 | |
San Mehat | 2a5b8ce | 2010-03-10 12:48:57 -0800 | [diff] [blame] | 627 | memset(&dinfo, 0, sizeof(dinfo)); |
| 628 | |
| 629 | 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] | 630 | SLOGE("Failed to malloc prt_lst"); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 631 | return -1; |
| 632 | } |
| 633 | |
San Mehat | 2a5b8ce | 2010-03-10 12:48:57 -0800 | [diff] [blame] | 634 | memset(dinfo.part_lst, 0, MAX_NUM_PARTS * sizeof(struct part_info)); |
| 635 | dinfo.device = strdup(deviceNode); |
| 636 | dinfo.scheme = PART_SCHEME_MBR; |
| 637 | dinfo.sect_size = 512; |
| 638 | dinfo.skip_lba = 2048; |
| 639 | dinfo.num_lba = 0; |
| 640 | dinfo.num_parts = 1; |
| 641 | |
| 642 | struct part_info *pinfo = &dinfo.part_lst[0]; |
| 643 | |
| 644 | pinfo->name = strdup("android_sdcard"); |
| 645 | pinfo->flags |= PART_ACTIVE_FLAG; |
| 646 | pinfo->type = PC_PART_TYPE_FAT32; |
| 647 | pinfo->len_kb = -1; |
| 648 | |
| 649 | int rc = apply_disk_config(&dinfo, 0); |
| 650 | |
| 651 | if (rc) { |
San Mehat | 97ac40e | 2010-03-24 10:24:19 -0700 | [diff] [blame] | 652 | SLOGE("Failed to apply disk configuration (%d)", rc); |
San Mehat | 2a5b8ce | 2010-03-10 12:48:57 -0800 | [diff] [blame] | 653 | goto out; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 654 | } |
| 655 | |
San Mehat | 2a5b8ce | 2010-03-10 12:48:57 -0800 | [diff] [blame] | 656 | out: |
| 657 | free(pinfo->name); |
| 658 | free(dinfo.device); |
| 659 | free(dinfo.part_lst); |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 660 | |
San Mehat | 2a5b8ce | 2010-03-10 12:48:57 -0800 | [diff] [blame] | 661 | return rc; |
San Mehat | a2677e4 | 2009-12-13 10:40:18 -0800 | [diff] [blame] | 662 | } |
Jeff Sharkey | 0de365f | 2013-10-16 16:24:19 -0700 | [diff] [blame] | 663 | |
| 664 | /* |
| 665 | * Use blkid to extract UUID and label from device, since it handles many |
| 666 | * obscure edge cases around partition types and formats. Always broadcasts |
| 667 | * updated metadata values. |
| 668 | */ |
| 669 | int Volume::extractMetadata(const char* devicePath) { |
| 670 | int res = 0; |
| 671 | |
| 672 | std::string cmd; |
| 673 | cmd = BLKID_PATH; |
| 674 | cmd += " -c /dev/null "; |
| 675 | cmd += devicePath; |
| 676 | |
| 677 | FILE* fp = popen(cmd.c_str(), "r"); |
| 678 | if (!fp) { |
| 679 | ALOGE("Failed to run %s: %s", cmd.c_str(), strerror(errno)); |
| 680 | res = -1; |
| 681 | goto done; |
| 682 | } |
| 683 | |
| 684 | char line[1024]; |
| 685 | char value[128]; |
| 686 | if (fgets(line, sizeof(line), fp) != NULL) { |
Jeff Sharkey | 8c2c15b | 2013-10-17 15:17:19 -0700 | [diff] [blame] | 687 | ALOGD("blkid identified as %s", line); |
Jeff Sharkey | 0de365f | 2013-10-16 16:24:19 -0700 | [diff] [blame] | 688 | |
Jeff Sharkey | 6bcd362 | 2013-11-11 14:46:39 -0800 | [diff] [blame] | 689 | char* start = strstr(line, "UUID="); |
| 690 | if (start != NULL && sscanf(start + 5, "\"%127[^\"]\"", value) == 1) { |
Jeff Sharkey | 0de365f | 2013-10-16 16:24:19 -0700 | [diff] [blame] | 691 | setUuid(value); |
| 692 | } else { |
| 693 | setUuid(NULL); |
| 694 | } |
| 695 | |
Jeff Sharkey | 6bcd362 | 2013-11-11 14:46:39 -0800 | [diff] [blame] | 696 | start = strstr(line, "LABEL="); |
| 697 | if (start != NULL && sscanf(start + 6, "\"%127[^\"]\"", value) == 1) { |
Jeff Sharkey | 0de365f | 2013-10-16 16:24:19 -0700 | [diff] [blame] | 698 | setUserLabel(value); |
| 699 | } else { |
| 700 | setUserLabel(NULL); |
| 701 | } |
| 702 | } else { |
Jeff Sharkey | 8c2c15b | 2013-10-17 15:17:19 -0700 | [diff] [blame] | 703 | ALOGW("blkid failed to identify %s", devicePath); |
Jeff Sharkey | 0de365f | 2013-10-16 16:24:19 -0700 | [diff] [blame] | 704 | res = -1; |
| 705 | } |
| 706 | |
| 707 | pclose(fp); |
| 708 | |
| 709 | done: |
| 710 | if (res == -1) { |
| 711 | setUuid(NULL); |
| 712 | setUserLabel(NULL); |
| 713 | } |
| 714 | return res; |
| 715 | } |