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