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