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