blob: b2ecc50631810a3378361b251aa9a665765d7281 [file] [log] [blame]
Jeff Sharkeydeb24052015-03-02 21:01:40 -08001/*
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 Sharkeydeb24052015-03-02 21:01:40 -080017#include "Disk.h"
Eric Biggersa701c452018-10-23 13:06:55 -070018#include "FsCrypt.h"
Paul Crowleyb3d018a2020-02-12 11:04:05 -080019#include "KeyUtil.h"
Jeff Sharkey9c484982015-03-31 10:35:33 -070020#include "PrivateVolume.h"
Paul Crowley14c8c072018-09-18 13:30:21 -070021#include "PublicVolume.h"
Jeff Sharkeydeb24052015-03-02 21:01:40 -080022#include "Utils.h"
23#include "VolumeBase.h"
Jeff Sharkey36801cc2015-03-13 16:09:20 -070024#include "VolumeManager.h"
Jeff Sharkeydeb24052015-03-02 21:01:40 -080025
Elliott Hughes7e128fb2015-12-04 15:50:53 -080026#include <android-base/file.h>
Paul Crowley3b71fc52017-10-09 10:55:21 -070027#include <android-base/logging.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070028#include <android-base/parseint.h>
Jeff Sharkey46bb69f2017-06-21 13:52:23 -060029#include <android-base/properties.h>
Elliott Hughes7e128fb2015-12-04 15:50:53 -080030#include <android-base/stringprintf.h>
Jeff Sharkey3472e522017-10-06 18:02:53 -060031#include <android-base/strings.h>
Eric Biggersa701c452018-10-23 13:06:55 -070032#include <fscrypt/fscrypt.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080033
Greg Kaiser57f9af62018-02-16 13:13:58 -080034#include "cryptfs.h"
35
Jeff Sharkeydeb24052015-03-02 21:01:40 -080036#include <fcntl.h>
Jeff Sharkey38cfc022015-03-30 21:22:07 -070037#include <inttypes.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080038#include <stdio.h>
39#include <stdlib.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070040#include <sys/mount.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080041#include <sys/stat.h>
Elliott Hughes0e08e842017-05-18 09:08:24 -070042#include <sys/sysmacros.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070043#include <sys/types.h>
44#include <vector>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080045
Dan Albertae9e8902015-03-16 10:35:17 -070046using android::base::ReadFileToString;
47using android::base::StringPrintf;
Paul Crowley14c8c072018-09-18 13:30:21 -070048using android::base::WriteStringToFile;
Dan Albertae9e8902015-03-16 10:35:17 -070049
Jeff Sharkeydeb24052015-03-02 21:01:40 -080050namespace android {
51namespace vold {
52
53static const char* kSgdiskPath = "/system/bin/sgdisk";
54static const char* kSgdiskToken = " \t\n";
55
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060056static const char* kSysfsLoopMaxMinors = "/sys/module/loop/parameters/max_part";
Pierre-Hugues Hussonf347cd02017-11-28 15:42:56 +010057static const char* kSysfsMmcMaxMinorsDeprecated = "/sys/module/mmcblk/parameters/perdev_minors";
58static const char* kSysfsMmcMaxMinors = "/sys/module/mmc_block/parameters/perdev_minors";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080059
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060060static const unsigned int kMajorBlockLoop = 7;
Jeff Sharkeyf3ee2002015-04-19 15:55:42 -070061static const unsigned int kMajorBlockScsiA = 8;
62static const unsigned int kMajorBlockScsiB = 65;
63static const unsigned int kMajorBlockScsiC = 66;
64static const unsigned int kMajorBlockScsiD = 67;
65static const unsigned int kMajorBlockScsiE = 68;
66static const unsigned int kMajorBlockScsiF = 69;
67static const unsigned int kMajorBlockScsiG = 70;
68static const unsigned int kMajorBlockScsiH = 71;
69static const unsigned int kMajorBlockScsiI = 128;
70static const unsigned int kMajorBlockScsiJ = 129;
71static const unsigned int kMajorBlockScsiK = 130;
72static const unsigned int kMajorBlockScsiL = 131;
73static const unsigned int kMajorBlockScsiM = 132;
74static const unsigned int kMajorBlockScsiN = 133;
75static const unsigned int kMajorBlockScsiO = 134;
76static const unsigned int kMajorBlockScsiP = 135;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080077static const unsigned int kMajorBlockMmc = 179;
Yu Ning942d4e82016-01-08 17:36:47 +080078static const unsigned int kMajorBlockExperimentalMin = 240;
79static const unsigned int kMajorBlockExperimentalMax = 254;
Dmitry Shmidt06dc6e52018-05-11 17:22:42 -070080static const unsigned int kMajorBlockDynamicMin = 234;
81static const unsigned int kMajorBlockDynamicMax = 512;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080082
83static const char* kGptBasicData = "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7";
84static const char* kGptAndroidMeta = "19A710A2-B3CA-11E4-B026-10604B889DCF";
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070085static const char* kGptAndroidExpand = "193D1EA4-B3CA-11E4-B075-10604B889DCF";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080086
87enum class Table {
88 kUnknown,
89 kMbr,
90 kGpt,
91};
92
Yu Ning942d4e82016-01-08 17:36:47 +080093static bool isVirtioBlkDevice(unsigned int major) {
94 /*
95 * The new emulator's "ranchu" virtual board no longer includes a goldfish
96 * MMC-based SD card device; instead, it emulates SD cards with virtio-blk,
97 * which has been supported by upstream kernel and QEMU for quite a while.
98 * Unfortunately, the virtio-blk block device driver does not use a fixed
99 * major number, but relies on the kernel to assign one from a specific
100 * range of block majors, which are allocated for "LOCAL/EXPERIMENAL USE"
101 * per Documentation/devices.txt. This is true even for the latest Linux
102 * kernel (4.4; see init() in drivers/block/virtio_blk.c).
103 *
104 * This makes it difficult for vold to detect a virtio-blk based SD card.
105 * The current solution checks two conditions (both must be met):
106 *
107 * a) If the running environment is the emulator;
108 * b) If the major number is an experimental block device major number (for
109 * x86/x86_64 3.10 ranchu kernels, virtio-blk always gets major number
110 * 253, but it is safer to match the range than just one value).
111 *
112 * Other conditions could be used, too, e.g. the hardware name should be
113 * "ranchu", the device's sysfs path should end with "/block/vd[d-z]", etc.
114 * But just having a) and b) is enough for now.
115 */
Paul Crowley14c8c072018-09-18 13:30:21 -0700116 return IsRunningInEmulator() && major >= kMajorBlockExperimentalMin &&
117 major <= kMajorBlockExperimentalMax;
Yu Ning942d4e82016-01-08 17:36:47 +0800118}
119
Dmitry Shmidt06dc6e52018-05-11 17:22:42 -0700120static bool isNvmeBlkDevice(unsigned int major, const std::string& sysPath) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700121 return sysPath.find("nvme") != std::string::npos && major >= kMajorBlockDynamicMin &&
122 major <= kMajorBlockDynamicMax;
Dmitry Shmidt06dc6e52018-05-11 17:22:42 -0700123}
124
Paul Crowley14c8c072018-09-18 13:30:21 -0700125Disk::Disk(const std::string& eventPath, dev_t device, const std::string& nickname, int flags)
126 : mDevice(device),
127 mSize(-1),
128 mNickname(nickname),
129 mFlags(flags),
130 mCreated(false),
131 mJustPartitioned(false) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700132 mId = StringPrintf("disk:%u,%u", major(device), minor(device));
133 mEventPath = eventPath;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800134 mSysPath = StringPrintf("/sys/%s", eventPath.c_str());
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700135 mDevPath = StringPrintf("/dev/block/vold/%s", mId.c_str());
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800136 CreateDeviceNode(mDevPath, mDevice);
137}
138
139Disk::~Disk() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700140 CHECK(!mCreated);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800141 DestroyDeviceNode(mDevPath);
142}
143
144std::shared_ptr<VolumeBase> Disk::findVolume(const std::string& id) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700145 for (auto vol : mVolumes) {
146 if (vol->getId() == id) {
147 return vol;
148 }
149 auto stackedVol = vol->findVolume(id);
150 if (stackedVol != nullptr) {
151 return stackedVol;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800152 }
153 }
154 return nullptr;
155}
156
Greg Kaiser2bc201e2018-12-18 08:42:08 -0800157void Disk::listVolumes(VolumeBase::Type type, std::list<std::string>& list) const {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700158 for (const auto& vol : mVolumes) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700159 if (vol->getType() == type) {
160 list.push_back(vol->getId());
161 }
162 // TODO: consider looking at stacked volumes
163 }
164}
165
Martijn Coenen0a7e9922020-01-24 16:17:32 +0100166std::vector<std::shared_ptr<VolumeBase>> Disk::getVolumes() const {
167 std::vector<std::shared_ptr<VolumeBase>> vols;
168 for (const auto& vol : mVolumes) {
169 vols.push_back(vol);
170 auto stackedVolumes = vol->getVolumes();
171 vols.insert(vols.end(), stackedVolumes.begin(), stackedVolumes.end());
172 }
173
174 return vols;
175}
176
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700177status_t Disk::create() {
178 CHECK(!mCreated);
179 mCreated = true;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600180
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600181 auto listener = VolumeManager::Instance()->getListener();
182 if (listener) listener->onDiskCreated(getId(), mFlags);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600183
Risan82e90de2020-02-04 16:07:21 +0900184 if (isStub()) {
185 createStubVolume();
186 return OK;
187 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700188 readMetadata();
189 readPartitions();
190 return OK;
191}
192
193status_t Disk::destroy() {
194 CHECK(mCreated);
195 destroyAllVolumes();
196 mCreated = false;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600197
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600198 auto listener = VolumeManager::Instance()->getListener();
199 if (listener) listener->onDiskDestroyed(getId());
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600200
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700201 return OK;
202}
203
204void Disk::createPublicVolume(dev_t device) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700205 auto vol = std::shared_ptr<VolumeBase>(new PublicVolume(device));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700206 if (mJustPartitioned) {
207 LOG(DEBUG) << "Device just partitioned; silently formatting";
208 vol->setSilent(true);
209 vol->create();
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700210 vol->format("auto");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700211 vol->destroy();
212 vol->setSilent(false);
213 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700214
Jeff Sharkey9c484982015-03-31 10:35:33 -0700215 mVolumes.push_back(vol);
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700216 vol->setDiskId(getId());
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700217 vol->create();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700218}
219
Jeff Sharkey9c484982015-03-31 10:35:33 -0700220void Disk::createPrivateVolume(dev_t device, const std::string& partGuid) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700221 std::string normalizedGuid;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700222 if (NormalizeHex(partGuid, normalizedGuid)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700223 LOG(WARNING) << "Invalid GUID " << partGuid;
224 return;
225 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700226
227 std::string keyRaw;
228 if (!ReadFileToString(BuildKeyPath(normalizedGuid), &keyRaw)) {
229 PLOG(ERROR) << "Failed to load key for GUID " << normalizedGuid;
230 return;
231 }
232
233 LOG(DEBUG) << "Found key for GUID " << normalizedGuid;
234
Paul Crowley3d98f5d2020-02-07 11:49:09 -0800235 auto keyBuffer = KeyBuffer(keyRaw.begin(), keyRaw.end());
236 auto vol = std::shared_ptr<VolumeBase>(new PrivateVolume(device, keyBuffer));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700237 if (mJustPartitioned) {
238 LOG(DEBUG) << "Device just partitioned; silently formatting";
239 vol->setSilent(true);
240 vol->create();
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700241 vol->format("auto");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700242 vol->destroy();
243 vol->setSilent(false);
244 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700245
246 mVolumes.push_back(vol);
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700247 vol->setDiskId(getId());
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700248 vol->setPartGuid(partGuid);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700249 vol->create();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700250}
251
Risan82e90de2020-02-04 16:07:21 +0900252void Disk::createStubVolume() {
253 CHECK(mVolumes.size() == 1);
254 auto listener = VolumeManager::Instance()->getListener();
255 if (listener) listener->onDiskMetadataChanged(getId(), mSize, mLabel, mSysPath);
256 if (listener) listener->onDiskScanned(getId());
257 mVolumes[0]->setDiskId(getId());
258 mVolumes[0]->create();
259}
260
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700261void Disk::destroyAllVolumes() {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700262 for (const auto& vol : mVolumes) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700263 vol->destroy();
264 }
265 mVolumes.clear();
266}
267
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800268status_t Disk::readMetadata() {
269 mSize = -1;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700270 mLabel.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800271
Oleksiy Avramchenko625dc782018-05-23 10:50:46 +0200272 if (GetBlockDevSize(mDevPath, &mSize) != OK) {
273 mSize = -1;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800274 }
275
Yu Ning942d4e82016-01-08 17:36:47 +0800276 unsigned int majorId = major(mDevice);
277 switch (majorId) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700278 case kMajorBlockLoop: {
Yu Ning942d4e82016-01-08 17:36:47 +0800279 mLabel = "Virtual";
280 break;
281 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700282 // clang-format off
283 case kMajorBlockScsiA: case kMajorBlockScsiB: case kMajorBlockScsiC:
284 case kMajorBlockScsiD: case kMajorBlockScsiE: case kMajorBlockScsiF:
285 case kMajorBlockScsiG: case kMajorBlockScsiH: case kMajorBlockScsiI:
286 case kMajorBlockScsiJ: case kMajorBlockScsiK: case kMajorBlockScsiL:
287 case kMajorBlockScsiM: case kMajorBlockScsiN: case kMajorBlockScsiO:
288 case kMajorBlockScsiP: {
289 // clang-format on
290 std::string path(mSysPath + "/device/vendor");
Dmitry Shmidt06dc6e52018-05-11 17:22:42 -0700291 std::string tmp;
292 if (!ReadFileToString(path, &tmp)) {
293 PLOG(WARNING) << "Failed to read vendor from " << path;
294 return -errno;
295 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700296 tmp = android::base::Trim(tmp);
Dmitry Shmidt06dc6e52018-05-11 17:22:42 -0700297 mLabel = tmp;
298 break;
299 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700300 case kMajorBlockMmc: {
301 std::string path(mSysPath + "/device/manfid");
302 std::string tmp;
303 if (!ReadFileToString(path, &tmp)) {
304 PLOG(WARNING) << "Failed to read manufacturer from " << path;
305 return -errno;
306 }
307 tmp = android::base::Trim(tmp);
308 int64_t manfid;
309 if (!android::base::ParseInt(tmp, &manfid)) {
310 PLOG(WARNING) << "Failed to parse manufacturer " << tmp;
311 return -EINVAL;
312 }
313 // Our goal here is to give the user a meaningful label, ideally
314 // matching whatever is silk-screened on the card. To reduce
315 // user confusion, this list doesn't contain white-label manfid.
316 switch (manfid) {
317 // clang-format off
318 case 0x000003: mLabel = "SanDisk"; break;
319 case 0x00001b: mLabel = "Samsung"; break;
320 case 0x000028: mLabel = "Lexar"; break;
321 case 0x000074: mLabel = "Transcend"; break;
322 // clang-format on
323 }
324 break;
325 }
326 default: {
327 if (isVirtioBlkDevice(majorId)) {
328 LOG(DEBUG) << "Recognized experimental block major ID " << majorId
329 << " as virtio-blk (emulator's virtual SD card device)";
330 mLabel = "Virtual";
331 break;
332 }
333 if (isNvmeBlkDevice(majorId, mSysPath)) {
334 std::string path(mSysPath + "/device/model");
335 std::string tmp;
336 if (!ReadFileToString(path, &tmp)) {
337 PLOG(WARNING) << "Failed to read vendor from " << path;
338 return -errno;
339 }
340 mLabel = tmp;
341 break;
342 }
343 LOG(WARNING) << "Unsupported block major type " << majorId;
344 return -ENOTSUP;
345 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800346 }
347
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600348 auto listener = VolumeManager::Instance()->getListener();
Paul Crowley14c8c072018-09-18 13:30:21 -0700349 if (listener) listener->onDiskMetadataChanged(getId(), mSize, mLabel, mSysPath);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600350
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800351 return OK;
352}
353
354status_t Disk::readPartitions() {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600355 int maxMinors = getMaxMinors();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800356 if (maxMinors < 0) {
357 return -ENOTSUP;
358 }
359
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700360 destroyAllVolumes();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800361
362 // Parse partition table
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700363
364 std::vector<std::string> cmd;
365 cmd.push_back(kSgdiskPath);
366 cmd.push_back("--android-dump");
367 cmd.push_back(mDevPath);
368
369 std::vector<std::string> output;
Paul Crowleyde2d6202018-11-30 11:43:47 -0800370 status_t res = ForkExecvp(cmd, &output);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700371 if (res != OK) {
372 LOG(WARNING) << "sgdisk failed to scan " << mDevPath;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600373
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600374 auto listener = VolumeManager::Instance()->getListener();
375 if (listener) listener->onDiskScanned(getId());
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600376
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700377 mJustPartitioned = false;
378 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800379 }
380
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800381 Table table = Table::kUnknown;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700382 bool foundParts = false;
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700383 for (const auto& line : output) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600384 auto split = android::base::Split(line, kSgdiskToken);
385 auto it = split.begin();
386 if (it == split.end()) continue;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700387
Jeff Sharkey3472e522017-10-06 18:02:53 -0600388 if (*it == "DISK") {
389 if (++it == split.end()) continue;
390 if (*it == "mbr") {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800391 table = Table::kMbr;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600392 } else if (*it == "gpt") {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800393 table = Table::kGpt;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600394 } else {
395 LOG(WARNING) << "Invalid partition table " << *it;
396 continue;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800397 }
Jeff Sharkey3472e522017-10-06 18:02:53 -0600398 } else if (*it == "PART") {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700399 foundParts = true;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600400
401 if (++it == split.end()) continue;
402 int i = 0;
403 if (!android::base::ParseInt(*it, &i, 1, maxMinors)) {
404 LOG(WARNING) << "Invalid partition number " << *it;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800405 continue;
406 }
407 dev_t partDevice = makedev(major(mDevice), minor(mDevice) + i);
408
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800409 if (table == Table::kMbr) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600410 if (++it == split.end()) continue;
411 int type = 0;
412 if (!android::base::ParseInt("0x" + *it, &type)) {
413 LOG(WARNING) << "Invalid partition type " << *it;
414 continue;
415 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800416
Jeff Sharkey3472e522017-10-06 18:02:53 -0600417 switch (type) {
Jeff Sharkey37ba1252018-01-19 10:55:18 +0900418 case 0x06: // FAT16
419 case 0x07: // HPFS/NTFS/exFAT
420 case 0x0b: // W95 FAT32 (LBA)
421 case 0x0c: // W95 FAT32 (LBA)
422 case 0x0e: // W95 FAT16 (LBA)
423 createPublicVolume(partDevice);
424 break;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800425 }
426 } else if (table == Table::kGpt) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600427 if (++it == split.end()) continue;
428 auto typeGuid = *it;
429 if (++it == split.end()) continue;
430 auto partGuid = *it;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800431
Jeff Sharkey3472e522017-10-06 18:02:53 -0600432 if (android::base::EqualsIgnoreCase(typeGuid, kGptBasicData)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700433 createPublicVolume(partDevice);
Jeff Sharkey3472e522017-10-06 18:02:53 -0600434 } else if (android::base::EqualsIgnoreCase(typeGuid, kGptAndroidExpand)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700435 createPrivateVolume(partDevice, partGuid);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800436 }
437 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800438 }
439 }
440
441 // Ugly last ditch effort, treat entire disk as partition
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700442 if (table == Table::kUnknown || !foundParts) {
443 LOG(WARNING) << mId << " has unknown partition table; trying entire device";
Jeff Sharkey63123c02015-06-26 11:16:14 -0700444
445 std::string fsType;
446 std::string unused;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600447 if (ReadMetadataUntrusted(mDevPath, &fsType, &unused, &unused) == OK) {
Jeff Sharkey63123c02015-06-26 11:16:14 -0700448 createPublicVolume(mDevice);
449 } else {
450 LOG(WARNING) << mId << " failed to identify, giving up";
451 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800452 }
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700453
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600454 auto listener = VolumeManager::Instance()->getListener();
455 if (listener) listener->onDiskScanned(getId());
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600456
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700457 mJustPartitioned = false;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800458 return OK;
459}
460
Risan82e90de2020-02-04 16:07:21 +0900461void Disk::initializePartition(std::shared_ptr<StubVolume> vol) {
462 CHECK(isStub());
463 CHECK(mVolumes.empty());
464 mVolumes.push_back(vol);
465}
466
Jeff Sharkey9c484982015-03-31 10:35:33 -0700467status_t Disk::unmountAll() {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700468 for (const auto& vol : mVolumes) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700469 vol->unmount();
470 }
471 return OK;
472}
473
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800474status_t Disk::partitionPublic() {
Jeff Sharkeydadccee2015-09-23 14:13:45 -0700475 int res;
476
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700477 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700478 mJustPartitioned = true;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800479
Jeff Sharkeydadccee2015-09-23 14:13:45 -0700480 // First nuke any existing partition table
481 std::vector<std::string> cmd;
482 cmd.push_back(kSgdiskPath);
483 cmd.push_back("--zap-all");
484 cmd.push_back(mDevPath);
485
486 // Zap sometimes returns an error when it actually succeeded, so
487 // just log as warning and keep rolling forward.
488 if ((res = ForkExecvp(cmd)) != 0) {
489 LOG(WARNING) << "Failed to zap; status " << res;
490 }
491
Jeff Sharkey68f1b8b2017-10-18 14:09:52 -0600492 // Now let's build the new MBR table. We heavily rely on sgdisk to
493 // force optimal alignment on the created partitions.
494 cmd.clear();
495 cmd.push_back(kSgdiskPath);
496 cmd.push_back("--new=0:0:-0");
497 cmd.push_back("--typecode=0:0c00");
498 cmd.push_back("--gpttombr=1");
499 cmd.push_back(mDevPath);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800500
Jeff Sharkey68f1b8b2017-10-18 14:09:52 -0600501 if ((res = ForkExecvp(cmd)) != 0) {
502 LOG(ERROR) << "Failed to partition; status " << res;
503 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800504 }
505
Jeff Sharkey68f1b8b2017-10-18 14:09:52 -0600506 return OK;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800507}
508
509status_t Disk::partitionPrivate() {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700510 return partitionMixed(0);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800511}
512
513status_t Disk::partitionMixed(int8_t ratio) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700514 int res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700515
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700516 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700517 mJustPartitioned = true;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700518
519 // First nuke any existing partition table
520 std::vector<std::string> cmd;
521 cmd.push_back(kSgdiskPath);
522 cmd.push_back("--zap-all");
523 cmd.push_back(mDevPath);
524
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700525 // Zap sometimes returns an error when it actually succeeded, so
526 // just log as warning and keep rolling forward.
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700527 if ((res = ForkExecvp(cmd)) != 0) {
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700528 LOG(WARNING) << "Failed to zap; status " << res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700529 }
530
531 // We've had some success above, so generate both the private partition
532 // GUID and encryption key and persist them.
533 std::string partGuidRaw;
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600534 if (GenerateRandomUuid(partGuidRaw) != OK) {
535 LOG(ERROR) << "Failed to generate GUID";
536 return -EIO;
537 }
538
Paul Crowleyb3d018a2020-02-12 11:04:05 -0800539 KeyBuffer key;
540 if (!generateStorageKey(cryptfs_get_keygen(), &key)) {
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600541 LOG(ERROR) << "Failed to generate key";
Jeff Sharkey9c484982015-03-31 10:35:33 -0700542 return -EIO;
543 }
Paul Crowleyb3d018a2020-02-12 11:04:05 -0800544 std::string keyRaw(key.begin(), key.end());
Jeff Sharkey9c484982015-03-31 10:35:33 -0700545
546 std::string partGuid;
547 StrToHex(partGuidRaw, partGuid);
548
549 if (!WriteStringToFile(keyRaw, BuildKeyPath(partGuid))) {
550 LOG(ERROR) << "Failed to persist key";
551 return -EIO;
552 } else {
553 LOG(DEBUG) << "Persisted key for GUID " << partGuid;
554 }
555
556 // Now let's build the new GPT table. We heavily rely on sgdisk to
557 // force optimal alignment on the created partitions.
558 cmd.clear();
559 cmd.push_back(kSgdiskPath);
560
561 // If requested, create a public partition first. Mixed-mode partitioning
562 // like this is an experimental feature.
563 if (ratio > 0) {
564 if (ratio < 10 || ratio > 90) {
565 LOG(ERROR) << "Mixed partition ratio must be between 10-90%";
566 return -EINVAL;
567 }
568
569 uint64_t splitMb = ((mSize / 100) * ratio) / 1024 / 1024;
570 cmd.push_back(StringPrintf("--new=0:0:+%" PRId64 "M", splitMb));
571 cmd.push_back(StringPrintf("--typecode=0:%s", kGptBasicData));
572 cmd.push_back("--change-name=0:shared");
573 }
574
575 // Define a metadata partition which is designed for future use; there
576 // should only be one of these per physical device, even if there are
577 // multiple private volumes.
578 cmd.push_back("--new=0:0:+16M");
579 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidMeta));
580 cmd.push_back("--change-name=0:android_meta");
581
582 // Define a single private partition filling the rest of disk.
583 cmd.push_back("--new=0:0:-0");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700584 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidExpand));
Jeff Sharkey9c484982015-03-31 10:35:33 -0700585 cmd.push_back(StringPrintf("--partition-guid=0:%s", partGuid.c_str()));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700586 cmd.push_back("--change-name=0:android_expand");
Jeff Sharkey9c484982015-03-31 10:35:33 -0700587
588 cmd.push_back(mDevPath);
589
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700590 if ((res = ForkExecvp(cmd)) != 0) {
591 LOG(ERROR) << "Failed to partition; status " << res;
592 return res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700593 }
594
595 return OK;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800596}
597
598int Disk::getMaxMinors() {
599 // Figure out maximum partition devices supported
Yu Ning942d4e82016-01-08 17:36:47 +0800600 unsigned int majorId = major(mDevice);
601 switch (majorId) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700602 case kMajorBlockLoop: {
603 std::string tmp;
604 if (!ReadFileToString(kSysfsLoopMaxMinors, &tmp)) {
605 LOG(ERROR) << "Failed to read max minors";
606 return -errno;
607 }
608 return std::stoi(tmp);
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600609 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700610 // clang-format off
611 case kMajorBlockScsiA: case kMajorBlockScsiB: case kMajorBlockScsiC:
612 case kMajorBlockScsiD: case kMajorBlockScsiE: case kMajorBlockScsiF:
613 case kMajorBlockScsiG: case kMajorBlockScsiH: case kMajorBlockScsiI:
614 case kMajorBlockScsiJ: case kMajorBlockScsiK: case kMajorBlockScsiL:
615 case kMajorBlockScsiM: case kMajorBlockScsiN: case kMajorBlockScsiO:
616 case kMajorBlockScsiP: {
617 // clang-format on
618 // Per Documentation/devices.txt this is static
Yu Ning942d4e82016-01-08 17:36:47 +0800619 return 15;
620 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700621 case kMajorBlockMmc: {
622 // Per Documentation/devices.txt this is dynamic
623 std::string tmp;
624 if (!ReadFileToString(kSysfsMmcMaxMinors, &tmp) &&
625 !ReadFileToString(kSysfsMmcMaxMinorsDeprecated, &tmp)) {
626 LOG(ERROR) << "Failed to read max minors";
627 return -errno;
628 }
629 return std::stoi(tmp);
Dmitry Shmidt06dc6e52018-05-11 17:22:42 -0700630 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700631 default: {
632 if (isVirtioBlkDevice(majorId)) {
633 // drivers/block/virtio_blk.c has "#define PART_BITS 4", so max is
634 // 2^4 - 1 = 15
635 return 15;
636 }
637 if (isNvmeBlkDevice(majorId, mSysPath)) {
638 // despite kernel nvme driver supports up to 1M minors,
639 // #define NVME_MINORS (1U << MINORBITS)
640 // sgdisk can not support more than 127 partitions, due to
641 // #define MAX_MBR_PARTS 128
642 return 127;
643 }
644 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800645 }
646
Yu Ning942d4e82016-01-08 17:36:47 +0800647 LOG(ERROR) << "Unsupported block major type " << majorId;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800648 return -ENOTSUP;
649}
650
651} // namespace vold
652} // namespace android