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