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