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