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