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" |
| 19 | #include "Utils.h" |
| 20 | #include "VolumeBase.h" |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 21 | #include "VolumeManager.h" |
| 22 | #include "ResponseCode.h" |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 23 | |
Dan Albert | ae9e890 | 2015-03-16 10:35:17 -0700 | [diff] [blame] | 24 | #include <base/file.h> |
| 25 | #include <base/stringprintf.h> |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 26 | #include <base/logging.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 27 | #include <diskconfig/diskconfig.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 28 | |
| 29 | #include <fcntl.h> |
Jeff Sharkey | 38cfc02 | 2015-03-30 21:22:07 -0700 | [diff] [blame^] | 30 | #include <inttypes.h> |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 31 | #include <stdio.h> |
| 32 | #include <stdlib.h> |
| 33 | #include <sys/types.h> |
| 34 | #include <sys/stat.h> |
| 35 | #include <sys/mount.h> |
| 36 | |
Dan Albert | ae9e890 | 2015-03-16 10:35:17 -0700 | [diff] [blame] | 37 | using android::base::ReadFileToString; |
| 38 | using android::base::StringPrintf; |
| 39 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 40 | namespace android { |
| 41 | namespace vold { |
| 42 | |
| 43 | static const char* kSgdiskPath = "/system/bin/sgdisk"; |
| 44 | static const char* kSgdiskToken = " \t\n"; |
| 45 | |
| 46 | static const char* kSysfsMmcMaxMinors = "/sys/module/mmcblk/parameters/perdev_minors"; |
| 47 | |
| 48 | static const unsigned int kMajorBlockScsi = 8; |
| 49 | static const unsigned int kMajorBlockMmc = 179; |
| 50 | |
| 51 | static const char* kGptBasicData = "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7"; |
| 52 | static const char* kGptAndroidMeta = "19A710A2-B3CA-11E4-B026-10604B889DCF"; |
| 53 | static const char* kGptAndroidExt = "193D1EA4-B3CA-11E4-B075-10604B889DCF"; |
| 54 | |
| 55 | enum class Table { |
| 56 | kUnknown, |
| 57 | kMbr, |
| 58 | kGpt, |
| 59 | }; |
| 60 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 61 | Disk::Disk(const std::string& eventPath, dev_t device, const std::string& nickname, int flags) : |
| 62 | mDevice(device), mSize(-1), mNickname(nickname), mFlags(flags), mCreated(false) { |
| 63 | mId = StringPrintf("disk:%u,%u", major(device), minor(device)); |
| 64 | mEventPath = eventPath; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 65 | mSysPath = StringPrintf("/sys/%s", eventPath.c_str()); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 66 | mDevPath = StringPrintf("/dev/block/vold/%s", mId.c_str()); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 67 | CreateDeviceNode(mDevPath, mDevice); |
| 68 | } |
| 69 | |
| 70 | Disk::~Disk() { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 71 | CHECK(!mCreated); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 72 | DestroyDeviceNode(mDevPath); |
| 73 | } |
| 74 | |
| 75 | std::shared_ptr<VolumeBase> Disk::findVolume(const std::string& id) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 76 | for (auto vol : mVolumes) { |
| 77 | if (vol->getId() == id) { |
| 78 | return vol; |
| 79 | } |
| 80 | auto stackedVol = vol->findVolume(id); |
| 81 | if (stackedVol != nullptr) { |
| 82 | return stackedVol; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | return nullptr; |
| 86 | } |
| 87 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 88 | status_t Disk::create() { |
| 89 | CHECK(!mCreated); |
| 90 | mCreated = true; |
| 91 | VolumeManager::Instance()->getBroadcaster()->sendBroadcast( |
| 92 | ResponseCode::DiskCreated, |
| 93 | StringPrintf("%s %d", getId().c_str(), mFlags).c_str(), false); |
| 94 | readMetadata(); |
| 95 | readPartitions(); |
| 96 | return OK; |
| 97 | } |
| 98 | |
| 99 | status_t Disk::destroy() { |
| 100 | CHECK(mCreated); |
| 101 | destroyAllVolumes(); |
| 102 | mCreated = false; |
| 103 | VolumeManager::Instance()->getBroadcaster()->sendBroadcast( |
| 104 | ResponseCode::DiskDestroyed, getId().c_str(), false); |
| 105 | return OK; |
| 106 | } |
| 107 | |
| 108 | void Disk::createPublicVolume(dev_t device) { |
| 109 | auto vol = new PublicVolume(device); |
| 110 | vol->create(); |
| 111 | |
| 112 | mVolumes.push_back(std::shared_ptr<VolumeBase>(vol)); |
| 113 | VolumeManager::Instance()->getBroadcaster()->sendBroadcast( |
| 114 | ResponseCode::DiskVolumeCreated, |
| 115 | StringPrintf("%s %s", getId().c_str(), vol->getId().c_str()).c_str(), false); |
| 116 | } |
| 117 | |
| 118 | void Disk::createPrivateVolume(dev_t device) { |
| 119 | // TODO: create and add |
| 120 | } |
| 121 | |
| 122 | void Disk::destroyAllVolumes() { |
| 123 | for (auto vol : mVolumes) { |
| 124 | vol->destroy(); |
| 125 | } |
| 126 | mVolumes.clear(); |
| 127 | } |
| 128 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 129 | status_t Disk::readMetadata() { |
| 130 | mSize = -1; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 131 | mLabel.clear(); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 132 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 133 | int fd = open(mDevPath.c_str(), O_RDONLY); |
| 134 | if (fd != -1) { |
| 135 | if (ioctl(fd, BLKGETSIZE64, &mSize)) { |
| 136 | mSize = -1; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 137 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 138 | close(fd); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | switch (major(mDevice)) { |
| 142 | case kMajorBlockScsi: { |
| 143 | std::string path(mSysPath + "/device/vendor"); |
| 144 | std::string tmp; |
| 145 | if (!ReadFileToString(path, &tmp)) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 146 | PLOG(WARNING) << "Failed to read vendor from " << path; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 147 | return -errno; |
| 148 | } |
| 149 | mLabel = tmp; |
| 150 | break; |
| 151 | } |
| 152 | case kMajorBlockMmc: { |
| 153 | std::string path(mSysPath + "/device/manfid"); |
| 154 | std::string tmp; |
| 155 | if (!ReadFileToString(path, &tmp)) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 156 | PLOG(WARNING) << "Failed to read manufacturer from " << path; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 157 | return -errno; |
| 158 | } |
| 159 | uint64_t manfid = strtoll(tmp.c_str(), nullptr, 16); |
| 160 | // Our goal here is to give the user a meaningful label, ideally |
| 161 | // matching whatever is silk-screened on the card. To reduce |
| 162 | // user confusion, this list doesn't contain white-label manfid. |
| 163 | switch (manfid) { |
| 164 | case 0x000003: mLabel = "SanDisk"; break; |
| 165 | case 0x00001b: mLabel = "Samsung"; break; |
| 166 | case 0x000028: mLabel = "Lexar"; break; |
| 167 | case 0x000074: mLabel = "Transcend"; break; |
| 168 | } |
| 169 | break; |
| 170 | } |
| 171 | default: { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 172 | LOG(WARNING) << "Unsupported block major type" << major(mDevice); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 173 | return -ENOTSUP; |
| 174 | } |
| 175 | } |
| 176 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 177 | VolumeManager::Instance()->getBroadcaster()->sendBroadcast( |
| 178 | ResponseCode::DiskSizeChanged, |
Jeff Sharkey | 38cfc02 | 2015-03-30 21:22:07 -0700 | [diff] [blame^] | 179 | StringPrintf("%s %" PRId64, getId().c_str(), mSize).c_str(), false); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 180 | VolumeManager::Instance()->getBroadcaster()->sendBroadcast( |
| 181 | ResponseCode::DiskLabelChanged, |
| 182 | StringPrintf("%s %s", getId().c_str(), mLabel.c_str()).c_str(), false); |
| 183 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 184 | return OK; |
| 185 | } |
| 186 | |
| 187 | status_t Disk::readPartitions() { |
| 188 | int8_t maxMinors = getMaxMinors(); |
| 189 | if (maxMinors < 0) { |
| 190 | return -ENOTSUP; |
| 191 | } |
| 192 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 193 | destroyAllVolumes(); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 194 | |
| 195 | // Parse partition table |
| 196 | std::string path(kSgdiskPath); |
| 197 | path += " --android-dump "; |
| 198 | path += mDevPath; |
| 199 | FILE* fp = popen(path.c_str(), "r"); |
| 200 | if (!fp) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 201 | PLOG(ERROR) << "Failed to run " << path; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 202 | return -errno; |
| 203 | } |
| 204 | |
| 205 | char line[1024]; |
| 206 | Table table = Table::kUnknown; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 207 | bool foundParts = false; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 208 | while (fgets(line, sizeof(line), fp) != nullptr) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 209 | LOG(DEBUG) << "sgdisk: " << line; |
| 210 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 211 | char* token = strtok(line, kSgdiskToken); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 212 | if (token == nullptr) continue; |
| 213 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 214 | if (!strcmp(token, "DISK")) { |
| 215 | const char* type = strtok(nullptr, kSgdiskToken); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 216 | if (!strcmp(type, "mbr")) { |
| 217 | table = Table::kMbr; |
| 218 | } else if (!strcmp(type, "gpt")) { |
| 219 | table = Table::kGpt; |
| 220 | } |
| 221 | } else if (!strcmp(token, "PART")) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 222 | foundParts = true; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 223 | int i = strtol(strtok(nullptr, kSgdiskToken), nullptr, 10); |
| 224 | if (i <= 0 || i > maxMinors) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 225 | LOG(WARNING) << mId << " is ignoring partition " << i |
| 226 | << " beyond max supported devices"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 227 | continue; |
| 228 | } |
| 229 | dev_t partDevice = makedev(major(mDevice), minor(mDevice) + i); |
| 230 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 231 | if (table == Table::kMbr) { |
| 232 | const char* type = strtok(nullptr, kSgdiskToken); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 233 | |
| 234 | switch (strtol(type, nullptr, 16)) { |
| 235 | case 0x06: // FAT16 |
| 236 | case 0x0b: // W95 FAT32 (LBA) |
| 237 | case 0x0c: // W95 FAT32 (LBA) |
| 238 | case 0x0e: // W95 FAT16 (LBA) |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 239 | createPublicVolume(partDevice); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 240 | break; |
| 241 | } |
| 242 | } else if (table == Table::kGpt) { |
| 243 | const char* typeGuid = strtok(nullptr, kSgdiskToken); |
| 244 | const char* partGuid = strtok(nullptr, kSgdiskToken); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 245 | |
| 246 | if (!strcasecmp(typeGuid, kGptBasicData)) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 247 | createPublicVolume(partDevice); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 248 | } else if (!strcasecmp(typeGuid, kGptAndroidExt)) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 249 | createPrivateVolume(partDevice); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 250 | } |
| 251 | } |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 252 | } |
| 253 | } |
| 254 | |
| 255 | // Ugly last ditch effort, treat entire disk as partition |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 256 | if (table == Table::kUnknown || !foundParts) { |
| 257 | LOG(WARNING) << mId << " has unknown partition table; trying entire device"; |
| 258 | createPublicVolume(mDevice); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | pclose(fp); |
| 262 | return OK; |
| 263 | } |
| 264 | |
| 265 | status_t Disk::partitionPublic() { |
| 266 | // TODO: improve this code |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 267 | destroyAllVolumes(); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 268 | |
| 269 | struct disk_info dinfo; |
| 270 | memset(&dinfo, 0, sizeof(dinfo)); |
| 271 | |
| 272 | if (!(dinfo.part_lst = (struct part_info *) malloc( |
| 273 | MAX_NUM_PARTS * sizeof(struct part_info)))) { |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 274 | return -1; |
| 275 | } |
| 276 | |
| 277 | memset(dinfo.part_lst, 0, MAX_NUM_PARTS * sizeof(struct part_info)); |
| 278 | dinfo.device = strdup(mDevPath.c_str()); |
| 279 | dinfo.scheme = PART_SCHEME_MBR; |
| 280 | dinfo.sect_size = 512; |
| 281 | dinfo.skip_lba = 2048; |
| 282 | dinfo.num_lba = 0; |
| 283 | dinfo.num_parts = 1; |
| 284 | |
| 285 | struct part_info *pinfo = &dinfo.part_lst[0]; |
| 286 | |
| 287 | pinfo->name = strdup("android_sdcard"); |
| 288 | pinfo->flags |= PART_ACTIVE_FLAG; |
| 289 | pinfo->type = PC_PART_TYPE_FAT32; |
| 290 | pinfo->len_kb = -1; |
| 291 | |
| 292 | int rc = apply_disk_config(&dinfo, 0); |
| 293 | if (rc) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 294 | LOG(ERROR) << "Failed to apply disk configuration: " << rc; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 295 | goto out; |
| 296 | } |
| 297 | |
| 298 | out: |
| 299 | free(pinfo->name); |
| 300 | free(dinfo.device); |
| 301 | free(dinfo.part_lst); |
| 302 | |
| 303 | return rc; |
| 304 | } |
| 305 | |
| 306 | status_t Disk::partitionPrivate() { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 307 | destroyAllVolumes(); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 308 | return -ENOTSUP; |
| 309 | } |
| 310 | |
| 311 | status_t Disk::partitionMixed(int8_t ratio) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 312 | destroyAllVolumes(); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 313 | return -ENOTSUP; |
| 314 | } |
| 315 | |
| 316 | int Disk::getMaxMinors() { |
| 317 | // Figure out maximum partition devices supported |
| 318 | switch (major(mDevice)) { |
| 319 | case kMajorBlockScsi: { |
| 320 | // Per Documentation/devices.txt this is static |
| 321 | return 15; |
| 322 | } |
| 323 | case kMajorBlockMmc: { |
| 324 | // Per Documentation/devices.txt this is dynamic |
| 325 | std::string tmp; |
| 326 | if (!ReadFileToString(kSysfsMmcMaxMinors, &tmp)) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 327 | LOG(ERROR) << "Failed to read max minors"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 328 | return -errno; |
| 329 | } |
| 330 | return atoi(tmp.c_str()); |
| 331 | } |
| 332 | } |
| 333 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 334 | LOG(ERROR) << "Unsupported block major type " << major(mDevice); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 335 | return -ENOTSUP; |
| 336 | } |
| 337 | |
| 338 | } // namespace vold |
| 339 | } // namespace android |