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