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