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