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