Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 17 | #include "Disk.h" |
| 18 | #include "PublicVolume.h" |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 19 | #include "PrivateVolume.h" |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 20 | #include "Utils.h" |
| 21 | #include "VolumeBase.h" |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 22 | #include "VolumeManager.h" |
| 23 | #include "ResponseCode.h" |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 24 | |
Dan Albert | ae9e890 | 2015-03-16 10:35:17 -0700 | [diff] [blame] | 25 | #include <base/file.h> |
| 26 | #include <base/stringprintf.h> |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 27 | #include <base/logging.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 28 | #include <diskconfig/diskconfig.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 29 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 30 | #include <vector> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 31 | #include <fcntl.h> |
Jeff Sharkey | 38cfc02 | 2015-03-30 21:22:07 -0700 | [diff] [blame] | 32 | #include <inttypes.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 33 | #include <stdio.h> |
| 34 | #include <stdlib.h> |
| 35 | #include <sys/types.h> |
| 36 | #include <sys/stat.h> |
| 37 | #include <sys/mount.h> |
| 38 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame^] | 39 | #define ENTIRE_DEVICE_FALLBACK 0 |
| 40 | |
Dan Albert | ae9e890 | 2015-03-16 10:35:17 -0700 | [diff] [blame] | 41 | using android::base::ReadFileToString; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 42 | using android::base::WriteStringToFile; |
Dan Albert | ae9e890 | 2015-03-16 10:35:17 -0700 | [diff] [blame] | 43 | using android::base::StringPrintf; |
| 44 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 45 | namespace android { |
| 46 | namespace vold { |
| 47 | |
| 48 | static const char* kSgdiskPath = "/system/bin/sgdisk"; |
| 49 | static const char* kSgdiskToken = " \t\n"; |
| 50 | |
| 51 | static const char* kSysfsMmcMaxMinors = "/sys/module/mmcblk/parameters/perdev_minors"; |
| 52 | |
| 53 | static const unsigned int kMajorBlockScsi = 8; |
| 54 | static const unsigned int kMajorBlockMmc = 179; |
| 55 | |
| 56 | static const char* kGptBasicData = "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7"; |
| 57 | static const char* kGptAndroidMeta = "19A710A2-B3CA-11E4-B026-10604B889DCF"; |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 58 | static const char* kGptAndroidExpand = "193D1EA4-B3CA-11E4-B075-10604B889DCF"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 59 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 60 | static const char* kKeyPath = "/data/misc/vold"; |
| 61 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 62 | enum class Table { |
| 63 | kUnknown, |
| 64 | kMbr, |
| 65 | kGpt, |
| 66 | }; |
| 67 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 68 | Disk::Disk(const std::string& eventPath, dev_t device, |
| 69 | const std::string& nickname, int flags) : |
| 70 | mDevice(device), mSize(-1), mNickname(nickname), mFlags(flags), mCreated( |
| 71 | false), mJustPartitioned(false) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 72 | mId = StringPrintf("disk:%u,%u", major(device), minor(device)); |
| 73 | mEventPath = eventPath; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 74 | mSysPath = StringPrintf("/sys/%s", eventPath.c_str()); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 75 | mDevPath = StringPrintf("/dev/block/vold/%s", mId.c_str()); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 76 | CreateDeviceNode(mDevPath, mDevice); |
| 77 | } |
| 78 | |
| 79 | Disk::~Disk() { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 80 | CHECK(!mCreated); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 81 | DestroyDeviceNode(mDevPath); |
| 82 | } |
| 83 | |
| 84 | std::shared_ptr<VolumeBase> Disk::findVolume(const std::string& id) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 85 | for (auto vol : mVolumes) { |
| 86 | if (vol->getId() == id) { |
| 87 | return vol; |
| 88 | } |
| 89 | auto stackedVol = vol->findVolume(id); |
| 90 | if (stackedVol != nullptr) { |
| 91 | return stackedVol; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | return nullptr; |
| 95 | } |
| 96 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 97 | status_t Disk::create() { |
| 98 | CHECK(!mCreated); |
| 99 | mCreated = true; |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 100 | notifyEvent(ResponseCode::DiskCreated, StringPrintf("%d", mFlags)); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 101 | readMetadata(); |
| 102 | readPartitions(); |
| 103 | return OK; |
| 104 | } |
| 105 | |
| 106 | status_t Disk::destroy() { |
| 107 | CHECK(mCreated); |
| 108 | destroyAllVolumes(); |
| 109 | mCreated = false; |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 110 | notifyEvent(ResponseCode::DiskDestroyed); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 111 | return OK; |
| 112 | } |
| 113 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 114 | static std::string BuildKeyPath(const std::string& partGuid) { |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 115 | return StringPrintf("%s/expand_%s.key", kKeyPath, partGuid.c_str()); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 116 | } |
| 117 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 118 | void Disk::createPublicVolume(dev_t device) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 119 | auto vol = std::shared_ptr<VolumeBase>(new PublicVolume(device)); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 120 | if (mJustPartitioned) { |
| 121 | LOG(DEBUG) << "Device just partitioned; silently formatting"; |
| 122 | vol->setSilent(true); |
| 123 | vol->create(); |
| 124 | vol->format(); |
| 125 | vol->destroy(); |
| 126 | vol->setSilent(false); |
| 127 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 128 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 129 | mVolumes.push_back(vol); |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame^] | 130 | vol->setDiskId(getId()); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 131 | vol->create(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 134 | void Disk::createPrivateVolume(dev_t device, const std::string& partGuid) { |
| 135 | std::string tmp; |
| 136 | std::string normalizedGuid; |
| 137 | if (HexToStr(partGuid, tmp)) { |
| 138 | LOG(WARNING) << "Invalid GUID " << partGuid; |
| 139 | return; |
| 140 | } |
| 141 | StrToHex(tmp, normalizedGuid); |
| 142 | |
| 143 | std::string keyRaw; |
| 144 | if (!ReadFileToString(BuildKeyPath(normalizedGuid), &keyRaw)) { |
| 145 | PLOG(ERROR) << "Failed to load key for GUID " << normalizedGuid; |
| 146 | return; |
| 147 | } |
| 148 | |
| 149 | LOG(DEBUG) << "Found key for GUID " << normalizedGuid; |
| 150 | |
| 151 | auto vol = std::shared_ptr<VolumeBase>(new PrivateVolume(device, keyRaw)); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 152 | if (mJustPartitioned) { |
| 153 | LOG(DEBUG) << "Device just partitioned; silently formatting"; |
| 154 | vol->setSilent(true); |
| 155 | vol->create(); |
| 156 | vol->format(); |
| 157 | vol->destroy(); |
| 158 | vol->setSilent(false); |
| 159 | } |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 160 | |
| 161 | mVolumes.push_back(vol); |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame^] | 162 | vol->setDiskId(getId()); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 163 | vol->create(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | void Disk::destroyAllVolumes() { |
| 167 | for (auto vol : mVolumes) { |
| 168 | vol->destroy(); |
| 169 | } |
| 170 | mVolumes.clear(); |
| 171 | } |
| 172 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 173 | status_t Disk::readMetadata() { |
| 174 | mSize = -1; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 175 | mLabel.clear(); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 176 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 177 | int fd = open(mDevPath.c_str(), O_RDONLY | O_CLOEXEC); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 178 | if (fd != -1) { |
| 179 | if (ioctl(fd, BLKGETSIZE64, &mSize)) { |
| 180 | mSize = -1; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 181 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 182 | close(fd); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | switch (major(mDevice)) { |
| 186 | case kMajorBlockScsi: { |
| 187 | std::string path(mSysPath + "/device/vendor"); |
| 188 | std::string tmp; |
| 189 | if (!ReadFileToString(path, &tmp)) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 190 | PLOG(WARNING) << "Failed to read vendor from " << path; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 191 | return -errno; |
| 192 | } |
| 193 | mLabel = tmp; |
| 194 | break; |
| 195 | } |
| 196 | case kMajorBlockMmc: { |
| 197 | std::string path(mSysPath + "/device/manfid"); |
| 198 | std::string tmp; |
| 199 | if (!ReadFileToString(path, &tmp)) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 200 | PLOG(WARNING) << "Failed to read manufacturer from " << path; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 201 | return -errno; |
| 202 | } |
| 203 | uint64_t manfid = strtoll(tmp.c_str(), nullptr, 16); |
| 204 | // Our goal here is to give the user a meaningful label, ideally |
| 205 | // matching whatever is silk-screened on the card. To reduce |
| 206 | // user confusion, this list doesn't contain white-label manfid. |
| 207 | switch (manfid) { |
| 208 | case 0x000003: mLabel = "SanDisk"; break; |
| 209 | case 0x00001b: mLabel = "Samsung"; break; |
| 210 | case 0x000028: mLabel = "Lexar"; break; |
| 211 | case 0x000074: mLabel = "Transcend"; break; |
| 212 | } |
| 213 | break; |
| 214 | } |
| 215 | default: { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 216 | LOG(WARNING) << "Unsupported block major type" << major(mDevice); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 217 | return -ENOTSUP; |
| 218 | } |
| 219 | } |
| 220 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 221 | notifyEvent(ResponseCode::DiskSizeChanged, StringPrintf("%" PRId64, mSize)); |
| 222 | notifyEvent(ResponseCode::DiskLabelChanged, mLabel); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 223 | return OK; |
| 224 | } |
| 225 | |
| 226 | status_t Disk::readPartitions() { |
Jeff Sharkey | 7d9d011 | 2015-04-14 23:14:23 -0700 | [diff] [blame] | 227 | std::lock_guard<std::mutex> lock(mLock); |
| 228 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 229 | int8_t maxMinors = getMaxMinors(); |
| 230 | if (maxMinors < 0) { |
| 231 | return -ENOTSUP; |
| 232 | } |
| 233 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 234 | destroyAllVolumes(); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 235 | |
| 236 | // Parse partition table |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 237 | |
| 238 | std::vector<std::string> cmd; |
| 239 | cmd.push_back(kSgdiskPath); |
| 240 | cmd.push_back("--android-dump"); |
| 241 | cmd.push_back(mDevPath); |
| 242 | |
| 243 | std::vector<std::string> output; |
| 244 | status_t res = ForkExecvp(cmd, output); |
| 245 | if (res != OK) { |
| 246 | LOG(WARNING) << "sgdisk failed to scan " << mDevPath; |
| 247 | mJustPartitioned = false; |
| 248 | return res; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 249 | } |
| 250 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 251 | Table table = Table::kUnknown; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 252 | bool foundParts = false; |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 253 | for (auto line : output) { |
| 254 | char* cline = (char*) line.c_str(); |
| 255 | char* token = strtok(cline, kSgdiskToken); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 256 | if (token == nullptr) continue; |
| 257 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 258 | if (!strcmp(token, "DISK")) { |
| 259 | const char* type = strtok(nullptr, kSgdiskToken); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 260 | if (!strcmp(type, "mbr")) { |
| 261 | table = Table::kMbr; |
| 262 | } else if (!strcmp(type, "gpt")) { |
| 263 | table = Table::kGpt; |
| 264 | } |
| 265 | } else if (!strcmp(token, "PART")) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 266 | foundParts = true; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 267 | int i = strtol(strtok(nullptr, kSgdiskToken), nullptr, 10); |
| 268 | if (i <= 0 || i > maxMinors) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 269 | LOG(WARNING) << mId << " is ignoring partition " << i |
| 270 | << " beyond max supported devices"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 271 | continue; |
| 272 | } |
| 273 | dev_t partDevice = makedev(major(mDevice), minor(mDevice) + i); |
| 274 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 275 | if (table == Table::kMbr) { |
| 276 | const char* type = strtok(nullptr, kSgdiskToken); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 277 | |
| 278 | switch (strtol(type, nullptr, 16)) { |
| 279 | case 0x06: // FAT16 |
| 280 | case 0x0b: // W95 FAT32 (LBA) |
| 281 | case 0x0c: // W95 FAT32 (LBA) |
| 282 | case 0x0e: // W95 FAT16 (LBA) |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 283 | createPublicVolume(partDevice); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 284 | break; |
| 285 | } |
| 286 | } else if (table == Table::kGpt) { |
| 287 | const char* typeGuid = strtok(nullptr, kSgdiskToken); |
| 288 | const char* partGuid = strtok(nullptr, kSgdiskToken); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 289 | |
| 290 | if (!strcasecmp(typeGuid, kGptBasicData)) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 291 | createPublicVolume(partDevice); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 292 | } else if (!strcasecmp(typeGuid, kGptAndroidExpand)) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 293 | createPrivateVolume(partDevice, partGuid); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 294 | } |
| 295 | } |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame^] | 299 | #if ENTIRE_DEVICE_FALLBACK |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 300 | // Ugly last ditch effort, treat entire disk as partition |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 301 | if (table == Table::kUnknown || !foundParts) { |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame^] | 302 | // TODO: use blkid to confirm filesystem before doing this |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 303 | LOG(WARNING) << mId << " has unknown partition table; trying entire device"; |
| 304 | createPublicVolume(mDevice); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 305 | } |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame^] | 306 | #endif |
| 307 | |
| 308 | // Well this is embarrassing, we can't handle the disk |
| 309 | if (mVolumes.size() == 0) { |
| 310 | notifyEvent(ResponseCode::DiskUnsupported); |
| 311 | } |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 312 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 313 | mJustPartitioned = false; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 314 | return OK; |
| 315 | } |
| 316 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 317 | status_t Disk::unmountAll() { |
| 318 | for (auto vol : mVolumes) { |
| 319 | vol->unmount(); |
| 320 | } |
| 321 | return OK; |
| 322 | } |
| 323 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 324 | status_t Disk::partitionPublic() { |
Jeff Sharkey | 7d9d011 | 2015-04-14 23:14:23 -0700 | [diff] [blame] | 325 | std::lock_guard<std::mutex> lock(mLock); |
| 326 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 327 | // TODO: improve this code |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 328 | destroyAllVolumes(); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 329 | mJustPartitioned = true; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 330 | |
| 331 | struct disk_info dinfo; |
| 332 | memset(&dinfo, 0, sizeof(dinfo)); |
| 333 | |
| 334 | if (!(dinfo.part_lst = (struct part_info *) malloc( |
| 335 | MAX_NUM_PARTS * sizeof(struct part_info)))) { |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 336 | return -1; |
| 337 | } |
| 338 | |
| 339 | memset(dinfo.part_lst, 0, MAX_NUM_PARTS * sizeof(struct part_info)); |
| 340 | dinfo.device = strdup(mDevPath.c_str()); |
| 341 | dinfo.scheme = PART_SCHEME_MBR; |
| 342 | dinfo.sect_size = 512; |
| 343 | dinfo.skip_lba = 2048; |
| 344 | dinfo.num_lba = 0; |
| 345 | dinfo.num_parts = 1; |
| 346 | |
| 347 | struct part_info *pinfo = &dinfo.part_lst[0]; |
| 348 | |
| 349 | pinfo->name = strdup("android_sdcard"); |
| 350 | pinfo->flags |= PART_ACTIVE_FLAG; |
| 351 | pinfo->type = PC_PART_TYPE_FAT32; |
| 352 | pinfo->len_kb = -1; |
| 353 | |
| 354 | int rc = apply_disk_config(&dinfo, 0); |
| 355 | if (rc) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 356 | LOG(ERROR) << "Failed to apply disk configuration: " << rc; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 357 | goto out; |
| 358 | } |
| 359 | |
| 360 | out: |
| 361 | free(pinfo->name); |
| 362 | free(dinfo.device); |
| 363 | free(dinfo.part_lst); |
| 364 | |
| 365 | return rc; |
| 366 | } |
| 367 | |
| 368 | status_t Disk::partitionPrivate() { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 369 | return partitionMixed(0); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | status_t Disk::partitionMixed(int8_t ratio) { |
Jeff Sharkey | 7d9d011 | 2015-04-14 23:14:23 -0700 | [diff] [blame] | 373 | std::lock_guard<std::mutex> lock(mLock); |
| 374 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 375 | int res; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 376 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 377 | destroyAllVolumes(); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 378 | mJustPartitioned = true; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 379 | |
| 380 | // First nuke any existing partition table |
| 381 | std::vector<std::string> cmd; |
| 382 | cmd.push_back(kSgdiskPath); |
| 383 | cmd.push_back("--zap-all"); |
| 384 | cmd.push_back(mDevPath); |
| 385 | |
Jeff Sharkey | ffeb007 | 2015-04-14 22:22:34 -0700 | [diff] [blame] | 386 | // Zap sometimes returns an error when it actually succeeded, so |
| 387 | // just log as warning and keep rolling forward. |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 388 | if ((res = ForkExecvp(cmd)) != 0) { |
Jeff Sharkey | ffeb007 | 2015-04-14 22:22:34 -0700 | [diff] [blame] | 389 | LOG(WARNING) << "Failed to zap; status " << res; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | // We've had some success above, so generate both the private partition |
| 393 | // GUID and encryption key and persist them. |
| 394 | std::string partGuidRaw; |
| 395 | std::string keyRaw; |
| 396 | if (ReadRandomBytes(16, partGuidRaw) || ReadRandomBytes(16, keyRaw)) { |
| 397 | LOG(ERROR) << "Failed to generate GUID or key"; |
| 398 | return -EIO; |
| 399 | } |
| 400 | |
| 401 | std::string partGuid; |
| 402 | StrToHex(partGuidRaw, partGuid); |
| 403 | |
| 404 | if (!WriteStringToFile(keyRaw, BuildKeyPath(partGuid))) { |
| 405 | LOG(ERROR) << "Failed to persist key"; |
| 406 | return -EIO; |
| 407 | } else { |
| 408 | LOG(DEBUG) << "Persisted key for GUID " << partGuid; |
| 409 | } |
| 410 | |
| 411 | // Now let's build the new GPT table. We heavily rely on sgdisk to |
| 412 | // force optimal alignment on the created partitions. |
| 413 | cmd.clear(); |
| 414 | cmd.push_back(kSgdiskPath); |
| 415 | |
| 416 | // If requested, create a public partition first. Mixed-mode partitioning |
| 417 | // like this is an experimental feature. |
| 418 | if (ratio > 0) { |
| 419 | if (ratio < 10 || ratio > 90) { |
| 420 | LOG(ERROR) << "Mixed partition ratio must be between 10-90%"; |
| 421 | return -EINVAL; |
| 422 | } |
| 423 | |
| 424 | uint64_t splitMb = ((mSize / 100) * ratio) / 1024 / 1024; |
| 425 | cmd.push_back(StringPrintf("--new=0:0:+%" PRId64 "M", splitMb)); |
| 426 | cmd.push_back(StringPrintf("--typecode=0:%s", kGptBasicData)); |
| 427 | cmd.push_back("--change-name=0:shared"); |
| 428 | } |
| 429 | |
| 430 | // Define a metadata partition which is designed for future use; there |
| 431 | // should only be one of these per physical device, even if there are |
| 432 | // multiple private volumes. |
| 433 | cmd.push_back("--new=0:0:+16M"); |
| 434 | cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidMeta)); |
| 435 | cmd.push_back("--change-name=0:android_meta"); |
| 436 | |
| 437 | // Define a single private partition filling the rest of disk. |
| 438 | cmd.push_back("--new=0:0:-0"); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 439 | cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidExpand)); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 440 | cmd.push_back(StringPrintf("--partition-guid=0:%s", partGuid.c_str())); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 441 | cmd.push_back("--change-name=0:android_expand"); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 442 | |
| 443 | cmd.push_back(mDevPath); |
| 444 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 445 | if ((res = ForkExecvp(cmd)) != 0) { |
| 446 | LOG(ERROR) << "Failed to partition; status " << res; |
| 447 | return res; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | return OK; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 451 | } |
| 452 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 453 | void Disk::notifyEvent(int event) { |
| 454 | VolumeManager::Instance()->getBroadcaster()->sendBroadcast(event, |
| 455 | getId().c_str(), false); |
| 456 | } |
| 457 | |
| 458 | void Disk::notifyEvent(int event, const std::string& value) { |
| 459 | VolumeManager::Instance()->getBroadcaster()->sendBroadcast(event, |
| 460 | StringPrintf("%s %s", getId().c_str(), value.c_str()).c_str(), false); |
| 461 | } |
| 462 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 463 | int Disk::getMaxMinors() { |
| 464 | // Figure out maximum partition devices supported |
| 465 | switch (major(mDevice)) { |
| 466 | case kMajorBlockScsi: { |
| 467 | // Per Documentation/devices.txt this is static |
| 468 | return 15; |
| 469 | } |
| 470 | case kMajorBlockMmc: { |
| 471 | // Per Documentation/devices.txt this is dynamic |
| 472 | std::string tmp; |
| 473 | if (!ReadFileToString(kSysfsMmcMaxMinors, &tmp)) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 474 | LOG(ERROR) << "Failed to read max minors"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 475 | return -errno; |
| 476 | } |
| 477 | return atoi(tmp.c_str()); |
| 478 | } |
| 479 | } |
| 480 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 481 | LOG(ERROR) << "Unsupported block major type " << major(mDevice); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 482 | return -ENOTSUP; |
| 483 | } |
| 484 | |
| 485 | } // namespace vold |
| 486 | } // namespace android |