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