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 | |
Elliott Hughes | 7e128fb | 2015-12-04 15:50:53 -0800 | [diff] [blame] | 25 | #include <android-base/file.h> |
| 26 | #include <android-base/stringprintf.h> |
| 27 | #include <android-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 | |
Dan Albert | ae9e890 | 2015-03-16 10:35:17 -0700 | [diff] [blame] | 39 | using android::base::ReadFileToString; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 40 | using android::base::WriteStringToFile; |
Dan Albert | ae9e890 | 2015-03-16 10:35:17 -0700 | [diff] [blame] | 41 | using android::base::StringPrintf; |
| 42 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 43 | namespace android { |
| 44 | namespace vold { |
| 45 | |
| 46 | static const char* kSgdiskPath = "/system/bin/sgdisk"; |
| 47 | static const char* kSgdiskToken = " \t\n"; |
| 48 | |
| 49 | static const char* kSysfsMmcMaxMinors = "/sys/module/mmcblk/parameters/perdev_minors"; |
| 50 | |
Jeff Sharkey | f3ee200 | 2015-04-19 15:55:42 -0700 | [diff] [blame] | 51 | static const unsigned int kMajorBlockScsiA = 8; |
| 52 | static const unsigned int kMajorBlockScsiB = 65; |
| 53 | static const unsigned int kMajorBlockScsiC = 66; |
| 54 | static const unsigned int kMajorBlockScsiD = 67; |
| 55 | static const unsigned int kMajorBlockScsiE = 68; |
| 56 | static const unsigned int kMajorBlockScsiF = 69; |
| 57 | static const unsigned int kMajorBlockScsiG = 70; |
| 58 | static const unsigned int kMajorBlockScsiH = 71; |
| 59 | static const unsigned int kMajorBlockScsiI = 128; |
| 60 | static const unsigned int kMajorBlockScsiJ = 129; |
| 61 | static const unsigned int kMajorBlockScsiK = 130; |
| 62 | static const unsigned int kMajorBlockScsiL = 131; |
| 63 | static const unsigned int kMajorBlockScsiM = 132; |
| 64 | static const unsigned int kMajorBlockScsiN = 133; |
| 65 | static const unsigned int kMajorBlockScsiO = 134; |
| 66 | static const unsigned int kMajorBlockScsiP = 135; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 67 | static const unsigned int kMajorBlockMmc = 179; |
Yu Ning | 942d4e8 | 2016-01-08 17:36:47 +0800 | [diff] [blame] | 68 | static const unsigned int kMajorBlockExperimentalMin = 240; |
| 69 | static const unsigned int kMajorBlockExperimentalMax = 254; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 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 | |
| 75 | enum class Table { |
| 76 | kUnknown, |
| 77 | kMbr, |
| 78 | kGpt, |
| 79 | }; |
| 80 | |
Yu Ning | 942d4e8 | 2016-01-08 17:36:47 +0800 | [diff] [blame] | 81 | static bool isVirtioBlkDevice(unsigned int major) { |
| 82 | /* |
| 83 | * The new emulator's "ranchu" virtual board no longer includes a goldfish |
| 84 | * MMC-based SD card device; instead, it emulates SD cards with virtio-blk, |
| 85 | * which has been supported by upstream kernel and QEMU for quite a while. |
| 86 | * Unfortunately, the virtio-blk block device driver does not use a fixed |
| 87 | * major number, but relies on the kernel to assign one from a specific |
| 88 | * range of block majors, which are allocated for "LOCAL/EXPERIMENAL USE" |
| 89 | * per Documentation/devices.txt. This is true even for the latest Linux |
| 90 | * kernel (4.4; see init() in drivers/block/virtio_blk.c). |
| 91 | * |
| 92 | * This makes it difficult for vold to detect a virtio-blk based SD card. |
| 93 | * The current solution checks two conditions (both must be met): |
| 94 | * |
| 95 | * a) If the running environment is the emulator; |
| 96 | * b) If the major number is an experimental block device major number (for |
| 97 | * x86/x86_64 3.10 ranchu kernels, virtio-blk always gets major number |
| 98 | * 253, but it is safer to match the range than just one value). |
| 99 | * |
| 100 | * Other conditions could be used, too, e.g. the hardware name should be |
| 101 | * "ranchu", the device's sysfs path should end with "/block/vd[d-z]", etc. |
| 102 | * But just having a) and b) is enough for now. |
| 103 | */ |
| 104 | return IsRunningInEmulator() && major >= kMajorBlockExperimentalMin |
| 105 | && major <= kMajorBlockExperimentalMax; |
| 106 | } |
| 107 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 108 | Disk::Disk(const std::string& eventPath, dev_t device, |
| 109 | const std::string& nickname, int flags) : |
| 110 | mDevice(device), mSize(-1), mNickname(nickname), mFlags(flags), mCreated( |
| 111 | false), mJustPartitioned(false) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 112 | mId = StringPrintf("disk:%u,%u", major(device), minor(device)); |
| 113 | mEventPath = eventPath; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 114 | mSysPath = StringPrintf("/sys/%s", eventPath.c_str()); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 115 | mDevPath = StringPrintf("/dev/block/vold/%s", mId.c_str()); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 116 | CreateDeviceNode(mDevPath, mDevice); |
| 117 | } |
| 118 | |
| 119 | Disk::~Disk() { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 120 | CHECK(!mCreated); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 121 | DestroyDeviceNode(mDevPath); |
| 122 | } |
| 123 | |
| 124 | std::shared_ptr<VolumeBase> Disk::findVolume(const std::string& id) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 125 | for (auto vol : mVolumes) { |
| 126 | if (vol->getId() == id) { |
| 127 | return vol; |
| 128 | } |
| 129 | auto stackedVol = vol->findVolume(id); |
| 130 | if (stackedVol != nullptr) { |
| 131 | return stackedVol; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 132 | } |
| 133 | } |
| 134 | return nullptr; |
| 135 | } |
| 136 | |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 137 | void Disk::listVolumes(VolumeBase::Type type, std::list<std::string>& list) { |
| 138 | for (auto vol : mVolumes) { |
| 139 | if (vol->getType() == type) { |
| 140 | list.push_back(vol->getId()); |
| 141 | } |
| 142 | // TODO: consider looking at stacked volumes |
| 143 | } |
| 144 | } |
| 145 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 146 | status_t Disk::create() { |
| 147 | CHECK(!mCreated); |
| 148 | mCreated = true; |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 149 | notifyEvent(ResponseCode::DiskCreated, StringPrintf("%d", mFlags)); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 150 | readMetadata(); |
| 151 | readPartitions(); |
| 152 | return OK; |
| 153 | } |
| 154 | |
| 155 | status_t Disk::destroy() { |
| 156 | CHECK(mCreated); |
| 157 | destroyAllVolumes(); |
| 158 | mCreated = false; |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 159 | notifyEvent(ResponseCode::DiskDestroyed); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 160 | return OK; |
| 161 | } |
| 162 | |
| 163 | void Disk::createPublicVolume(dev_t device) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 164 | auto vol = std::shared_ptr<VolumeBase>(new PublicVolume(device)); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 165 | if (mJustPartitioned) { |
| 166 | LOG(DEBUG) << "Device just partitioned; silently formatting"; |
| 167 | vol->setSilent(true); |
| 168 | vol->create(); |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 169 | vol->format("auto"); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 170 | vol->destroy(); |
| 171 | vol->setSilent(false); |
| 172 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 173 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 174 | mVolumes.push_back(vol); |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 175 | vol->setDiskId(getId()); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 176 | vol->create(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 179 | void Disk::createPrivateVolume(dev_t device, const std::string& partGuid) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 180 | std::string normalizedGuid; |
Jeff Sharkey | bc40cc8 | 2015-06-18 14:25:08 -0700 | [diff] [blame] | 181 | if (NormalizeHex(partGuid, normalizedGuid)) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 182 | LOG(WARNING) << "Invalid GUID " << partGuid; |
| 183 | return; |
| 184 | } |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 185 | |
| 186 | std::string keyRaw; |
| 187 | if (!ReadFileToString(BuildKeyPath(normalizedGuid), &keyRaw)) { |
| 188 | PLOG(ERROR) << "Failed to load key for GUID " << normalizedGuid; |
| 189 | return; |
| 190 | } |
| 191 | |
| 192 | LOG(DEBUG) << "Found key for GUID " << normalizedGuid; |
| 193 | |
| 194 | auto vol = std::shared_ptr<VolumeBase>(new PrivateVolume(device, keyRaw)); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 195 | if (mJustPartitioned) { |
| 196 | LOG(DEBUG) << "Device just partitioned; silently formatting"; |
| 197 | vol->setSilent(true); |
| 198 | vol->create(); |
Jeff Sharkey | d0640f6 | 2015-05-21 22:35:42 -0700 | [diff] [blame] | 199 | vol->format("auto"); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 200 | vol->destroy(); |
| 201 | vol->setSilent(false); |
| 202 | } |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 203 | |
| 204 | mVolumes.push_back(vol); |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 205 | vol->setDiskId(getId()); |
Jeff Sharkey | bc40cc8 | 2015-06-18 14:25:08 -0700 | [diff] [blame] | 206 | vol->setPartGuid(partGuid); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 207 | vol->create(); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | void Disk::destroyAllVolumes() { |
| 211 | for (auto vol : mVolumes) { |
| 212 | vol->destroy(); |
| 213 | } |
| 214 | mVolumes.clear(); |
| 215 | } |
| 216 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 217 | status_t Disk::readMetadata() { |
| 218 | mSize = -1; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 219 | mLabel.clear(); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 220 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 221 | int fd = open(mDevPath.c_str(), O_RDONLY | O_CLOEXEC); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 222 | if (fd != -1) { |
| 223 | if (ioctl(fd, BLKGETSIZE64, &mSize)) { |
| 224 | mSize = -1; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 225 | } |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 226 | close(fd); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 227 | } |
| 228 | |
Yu Ning | 942d4e8 | 2016-01-08 17:36:47 +0800 | [diff] [blame] | 229 | unsigned int majorId = major(mDevice); |
| 230 | switch (majorId) { |
Jeff Sharkey | f3ee200 | 2015-04-19 15:55:42 -0700 | [diff] [blame] | 231 | case kMajorBlockScsiA: case kMajorBlockScsiB: case kMajorBlockScsiC: case kMajorBlockScsiD: |
| 232 | case kMajorBlockScsiE: case kMajorBlockScsiF: case kMajorBlockScsiG: case kMajorBlockScsiH: |
| 233 | case kMajorBlockScsiI: case kMajorBlockScsiJ: case kMajorBlockScsiK: case kMajorBlockScsiL: |
| 234 | case kMajorBlockScsiM: case kMajorBlockScsiN: case kMajorBlockScsiO: case kMajorBlockScsiP: { |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 235 | std::string path(mSysPath + "/device/vendor"); |
| 236 | std::string tmp; |
| 237 | if (!ReadFileToString(path, &tmp)) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 238 | PLOG(WARNING) << "Failed to read vendor from " << path; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 239 | return -errno; |
| 240 | } |
| 241 | mLabel = tmp; |
| 242 | break; |
| 243 | } |
| 244 | case kMajorBlockMmc: { |
| 245 | std::string path(mSysPath + "/device/manfid"); |
| 246 | std::string tmp; |
| 247 | if (!ReadFileToString(path, &tmp)) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 248 | PLOG(WARNING) << "Failed to read manufacturer from " << path; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 249 | return -errno; |
| 250 | } |
| 251 | uint64_t manfid = strtoll(tmp.c_str(), nullptr, 16); |
| 252 | // Our goal here is to give the user a meaningful label, ideally |
| 253 | // matching whatever is silk-screened on the card. To reduce |
| 254 | // user confusion, this list doesn't contain white-label manfid. |
| 255 | switch (manfid) { |
| 256 | case 0x000003: mLabel = "SanDisk"; break; |
| 257 | case 0x00001b: mLabel = "Samsung"; break; |
| 258 | case 0x000028: mLabel = "Lexar"; break; |
| 259 | case 0x000074: mLabel = "Transcend"; break; |
| 260 | } |
| 261 | break; |
| 262 | } |
| 263 | default: { |
Yu Ning | 942d4e8 | 2016-01-08 17:36:47 +0800 | [diff] [blame] | 264 | if (isVirtioBlkDevice(majorId)) { |
| 265 | LOG(DEBUG) << "Recognized experimental block major ID " << majorId |
| 266 | << " as virtio-blk (emulator's virtual SD card device)"; |
| 267 | mLabel = "Virtual"; |
| 268 | break; |
| 269 | } |
| 270 | LOG(WARNING) << "Unsupported block major type " << majorId; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 271 | return -ENOTSUP; |
| 272 | } |
| 273 | } |
| 274 | |
Jeff Sharkey | d087bbc | 2016-03-10 12:11:09 -0700 | [diff] [blame] | 275 | notifyEvent(ResponseCode::DiskSizeChanged, StringPrintf("%" PRIu64, mSize)); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 276 | notifyEvent(ResponseCode::DiskLabelChanged, mLabel); |
Jeff Sharkey | c86ab6f | 2015-06-26 14:02:09 -0700 | [diff] [blame] | 277 | notifyEvent(ResponseCode::DiskSysPathChanged, mSysPath); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 278 | return OK; |
| 279 | } |
| 280 | |
| 281 | status_t Disk::readPartitions() { |
| 282 | int8_t maxMinors = getMaxMinors(); |
| 283 | if (maxMinors < 0) { |
| 284 | return -ENOTSUP; |
| 285 | } |
| 286 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 287 | destroyAllVolumes(); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 288 | |
| 289 | // Parse partition table |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 290 | |
| 291 | std::vector<std::string> cmd; |
| 292 | cmd.push_back(kSgdiskPath); |
| 293 | cmd.push_back("--android-dump"); |
| 294 | cmd.push_back(mDevPath); |
| 295 | |
| 296 | std::vector<std::string> output; |
| 297 | status_t res = ForkExecvp(cmd, output); |
| 298 | if (res != OK) { |
| 299 | LOG(WARNING) << "sgdisk failed to scan " << mDevPath; |
Jeff Sharkey | ba6747f | 2015-04-28 21:17:43 -0700 | [diff] [blame] | 300 | notifyEvent(ResponseCode::DiskScanned); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 301 | mJustPartitioned = false; |
| 302 | return res; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 303 | } |
| 304 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 305 | Table table = Table::kUnknown; |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 306 | bool foundParts = false; |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 307 | for (auto line : output) { |
| 308 | char* cline = (char*) line.c_str(); |
| 309 | char* token = strtok(cline, kSgdiskToken); |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 310 | if (token == nullptr) continue; |
| 311 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 312 | if (!strcmp(token, "DISK")) { |
| 313 | const char* type = strtok(nullptr, kSgdiskToken); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 314 | if (!strcmp(type, "mbr")) { |
| 315 | table = Table::kMbr; |
| 316 | } else if (!strcmp(type, "gpt")) { |
| 317 | table = Table::kGpt; |
| 318 | } |
| 319 | } else if (!strcmp(token, "PART")) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 320 | foundParts = true; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 321 | int i = strtol(strtok(nullptr, kSgdiskToken), nullptr, 10); |
| 322 | if (i <= 0 || i > maxMinors) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 323 | LOG(WARNING) << mId << " is ignoring partition " << i |
| 324 | << " beyond max supported devices"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 325 | continue; |
| 326 | } |
| 327 | dev_t partDevice = makedev(major(mDevice), minor(mDevice) + i); |
| 328 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 329 | if (table == Table::kMbr) { |
| 330 | const char* type = strtok(nullptr, kSgdiskToken); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 331 | |
| 332 | switch (strtol(type, nullptr, 16)) { |
| 333 | case 0x06: // FAT16 |
| 334 | case 0x0b: // W95 FAT32 (LBA) |
| 335 | case 0x0c: // W95 FAT32 (LBA) |
| 336 | case 0x0e: // W95 FAT16 (LBA) |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 337 | createPublicVolume(partDevice); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 338 | break; |
| 339 | } |
| 340 | } else if (table == Table::kGpt) { |
| 341 | const char* typeGuid = strtok(nullptr, kSgdiskToken); |
| 342 | const char* partGuid = strtok(nullptr, kSgdiskToken); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 343 | |
| 344 | if (!strcasecmp(typeGuid, kGptBasicData)) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 345 | createPublicVolume(partDevice); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 346 | } else if (!strcasecmp(typeGuid, kGptAndroidExpand)) { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 347 | createPrivateVolume(partDevice, partGuid); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 348 | } |
| 349 | } |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 350 | } |
| 351 | } |
| 352 | |
| 353 | // Ugly last ditch effort, treat entire disk as partition |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 354 | if (table == Table::kUnknown || !foundParts) { |
| 355 | LOG(WARNING) << mId << " has unknown partition table; trying entire device"; |
Jeff Sharkey | 63123c0 | 2015-06-26 11:16:14 -0700 | [diff] [blame] | 356 | |
| 357 | std::string fsType; |
| 358 | std::string unused; |
| 359 | if (ReadMetadataUntrusted(mDevPath, fsType, unused, unused) == OK) { |
| 360 | createPublicVolume(mDevice); |
| 361 | } else { |
| 362 | LOG(WARNING) << mId << " failed to identify, giving up"; |
| 363 | } |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 364 | } |
Jeff Sharkey | f1b996d | 2015-04-17 17:35:20 -0700 | [diff] [blame] | 365 | |
Jeff Sharkey | 613b26f | 2015-04-19 14:57:55 -0700 | [diff] [blame] | 366 | notifyEvent(ResponseCode::DiskScanned); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 367 | mJustPartitioned = false; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 368 | return OK; |
| 369 | } |
| 370 | |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 371 | status_t Disk::unmountAll() { |
| 372 | for (auto vol : mVolumes) { |
| 373 | vol->unmount(); |
| 374 | } |
| 375 | return OK; |
| 376 | } |
| 377 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 378 | status_t Disk::partitionPublic() { |
Jeff Sharkey | dadccee | 2015-09-23 14:13:45 -0700 | [diff] [blame] | 379 | int res; |
| 380 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 381 | // TODO: improve this code |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 382 | destroyAllVolumes(); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 383 | mJustPartitioned = true; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 384 | |
Jeff Sharkey | dadccee | 2015-09-23 14:13:45 -0700 | [diff] [blame] | 385 | // First nuke any existing partition table |
| 386 | std::vector<std::string> cmd; |
| 387 | cmd.push_back(kSgdiskPath); |
| 388 | cmd.push_back("--zap-all"); |
| 389 | cmd.push_back(mDevPath); |
| 390 | |
| 391 | // Zap sometimes returns an error when it actually succeeded, so |
| 392 | // just log as warning and keep rolling forward. |
| 393 | if ((res = ForkExecvp(cmd)) != 0) { |
| 394 | LOG(WARNING) << "Failed to zap; status " << res; |
| 395 | } |
| 396 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 397 | struct disk_info dinfo; |
| 398 | memset(&dinfo, 0, sizeof(dinfo)); |
| 399 | |
| 400 | if (!(dinfo.part_lst = (struct part_info *) malloc( |
| 401 | MAX_NUM_PARTS * sizeof(struct part_info)))) { |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 402 | return -1; |
| 403 | } |
| 404 | |
| 405 | memset(dinfo.part_lst, 0, MAX_NUM_PARTS * sizeof(struct part_info)); |
| 406 | dinfo.device = strdup(mDevPath.c_str()); |
| 407 | dinfo.scheme = PART_SCHEME_MBR; |
| 408 | dinfo.sect_size = 512; |
| 409 | dinfo.skip_lba = 2048; |
| 410 | dinfo.num_lba = 0; |
| 411 | dinfo.num_parts = 1; |
| 412 | |
| 413 | struct part_info *pinfo = &dinfo.part_lst[0]; |
| 414 | |
| 415 | pinfo->name = strdup("android_sdcard"); |
| 416 | pinfo->flags |= PART_ACTIVE_FLAG; |
| 417 | pinfo->type = PC_PART_TYPE_FAT32; |
| 418 | pinfo->len_kb = -1; |
| 419 | |
| 420 | int rc = apply_disk_config(&dinfo, 0); |
| 421 | if (rc) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 422 | LOG(ERROR) << "Failed to apply disk configuration: " << rc; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 423 | goto out; |
| 424 | } |
| 425 | |
| 426 | out: |
| 427 | free(pinfo->name); |
| 428 | free(dinfo.device); |
| 429 | free(dinfo.part_lst); |
| 430 | |
| 431 | return rc; |
| 432 | } |
| 433 | |
| 434 | status_t Disk::partitionPrivate() { |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 435 | return partitionMixed(0); |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 436 | } |
| 437 | |
| 438 | status_t Disk::partitionMixed(int8_t ratio) { |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 439 | int res; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 440 | |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 441 | destroyAllVolumes(); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 442 | mJustPartitioned = true; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 443 | |
| 444 | // First nuke any existing partition table |
| 445 | std::vector<std::string> cmd; |
| 446 | cmd.push_back(kSgdiskPath); |
| 447 | cmd.push_back("--zap-all"); |
| 448 | cmd.push_back(mDevPath); |
| 449 | |
Jeff Sharkey | ffeb007 | 2015-04-14 22:22:34 -0700 | [diff] [blame] | 450 | // Zap sometimes returns an error when it actually succeeded, so |
| 451 | // just log as warning and keep rolling forward. |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 452 | if ((res = ForkExecvp(cmd)) != 0) { |
Jeff Sharkey | ffeb007 | 2015-04-14 22:22:34 -0700 | [diff] [blame] | 453 | LOG(WARNING) << "Failed to zap; status " << res; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | // We've had some success above, so generate both the private partition |
| 457 | // GUID and encryption key and persist them. |
| 458 | std::string partGuidRaw; |
| 459 | std::string keyRaw; |
| 460 | if (ReadRandomBytes(16, partGuidRaw) || ReadRandomBytes(16, keyRaw)) { |
| 461 | LOG(ERROR) << "Failed to generate GUID or key"; |
| 462 | return -EIO; |
| 463 | } |
| 464 | |
| 465 | std::string partGuid; |
| 466 | StrToHex(partGuidRaw, partGuid); |
| 467 | |
| 468 | if (!WriteStringToFile(keyRaw, BuildKeyPath(partGuid))) { |
| 469 | LOG(ERROR) << "Failed to persist key"; |
| 470 | return -EIO; |
| 471 | } else { |
| 472 | LOG(DEBUG) << "Persisted key for GUID " << partGuid; |
| 473 | } |
| 474 | |
| 475 | // Now let's build the new GPT table. We heavily rely on sgdisk to |
| 476 | // force optimal alignment on the created partitions. |
| 477 | cmd.clear(); |
| 478 | cmd.push_back(kSgdiskPath); |
| 479 | |
| 480 | // If requested, create a public partition first. Mixed-mode partitioning |
| 481 | // like this is an experimental feature. |
| 482 | if (ratio > 0) { |
| 483 | if (ratio < 10 || ratio > 90) { |
| 484 | LOG(ERROR) << "Mixed partition ratio must be between 10-90%"; |
| 485 | return -EINVAL; |
| 486 | } |
| 487 | |
| 488 | uint64_t splitMb = ((mSize / 100) * ratio) / 1024 / 1024; |
| 489 | cmd.push_back(StringPrintf("--new=0:0:+%" PRId64 "M", splitMb)); |
| 490 | cmd.push_back(StringPrintf("--typecode=0:%s", kGptBasicData)); |
| 491 | cmd.push_back("--change-name=0:shared"); |
| 492 | } |
| 493 | |
| 494 | // Define a metadata partition which is designed for future use; there |
| 495 | // should only be one of these per physical device, even if there are |
| 496 | // multiple private volumes. |
| 497 | cmd.push_back("--new=0:0:+16M"); |
| 498 | cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidMeta)); |
| 499 | cmd.push_back("--change-name=0:android_meta"); |
| 500 | |
| 501 | // Define a single private partition filling the rest of disk. |
| 502 | cmd.push_back("--new=0:0:-0"); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 503 | cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidExpand)); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 504 | cmd.push_back(StringPrintf("--partition-guid=0:%s", partGuid.c_str())); |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 505 | cmd.push_back("--change-name=0:android_expand"); |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 506 | |
| 507 | cmd.push_back(mDevPath); |
| 508 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 509 | if ((res = ForkExecvp(cmd)) != 0) { |
| 510 | LOG(ERROR) << "Failed to partition; status " << res; |
| 511 | return res; |
Jeff Sharkey | 9c48498 | 2015-03-31 10:35:33 -0700 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | return OK; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 515 | } |
| 516 | |
Jeff Sharkey | ce6a913 | 2015-04-08 21:07:21 -0700 | [diff] [blame] | 517 | void Disk::notifyEvent(int event) { |
| 518 | VolumeManager::Instance()->getBroadcaster()->sendBroadcast(event, |
| 519 | getId().c_str(), false); |
| 520 | } |
| 521 | |
| 522 | void Disk::notifyEvent(int event, const std::string& value) { |
| 523 | VolumeManager::Instance()->getBroadcaster()->sendBroadcast(event, |
| 524 | StringPrintf("%s %s", getId().c_str(), value.c_str()).c_str(), false); |
| 525 | } |
| 526 | |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 527 | int Disk::getMaxMinors() { |
| 528 | // Figure out maximum partition devices supported |
Yu Ning | 942d4e8 | 2016-01-08 17:36:47 +0800 | [diff] [blame] | 529 | unsigned int majorId = major(mDevice); |
| 530 | switch (majorId) { |
Jeff Sharkey | f3ee200 | 2015-04-19 15:55:42 -0700 | [diff] [blame] | 531 | case kMajorBlockScsiA: case kMajorBlockScsiB: case kMajorBlockScsiC: case kMajorBlockScsiD: |
| 532 | case kMajorBlockScsiE: case kMajorBlockScsiF: case kMajorBlockScsiG: case kMajorBlockScsiH: |
| 533 | case kMajorBlockScsiI: case kMajorBlockScsiJ: case kMajorBlockScsiK: case kMajorBlockScsiL: |
| 534 | case kMajorBlockScsiM: case kMajorBlockScsiN: case kMajorBlockScsiO: case kMajorBlockScsiP: { |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 535 | // Per Documentation/devices.txt this is static |
| 536 | return 15; |
| 537 | } |
| 538 | case kMajorBlockMmc: { |
| 539 | // Per Documentation/devices.txt this is dynamic |
| 540 | std::string tmp; |
| 541 | if (!ReadFileToString(kSysfsMmcMaxMinors, &tmp)) { |
Jeff Sharkey | 36801cc | 2015-03-13 16:09:20 -0700 | [diff] [blame] | 542 | LOG(ERROR) << "Failed to read max minors"; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 543 | return -errno; |
| 544 | } |
| 545 | return atoi(tmp.c_str()); |
| 546 | } |
Yu Ning | 942d4e8 | 2016-01-08 17:36:47 +0800 | [diff] [blame] | 547 | default: { |
| 548 | if (isVirtioBlkDevice(majorId)) { |
| 549 | // drivers/block/virtio_blk.c has "#define PART_BITS 4", so max is |
| 550 | // 2^4 - 1 = 15 |
| 551 | return 15; |
| 552 | } |
| 553 | } |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 554 | } |
| 555 | |
Yu Ning | 942d4e8 | 2016-01-08 17:36:47 +0800 | [diff] [blame] | 556 | LOG(ERROR) << "Unsupported block major type " << majorId; |
Jeff Sharkey | deb2405 | 2015-03-02 21:01:40 -0800 | [diff] [blame] | 557 | return -ENOTSUP; |
| 558 | } |
| 559 | |
| 560 | } // namespace vold |
| 561 | } // namespace android |