blob: 7ecc89e282d4b52f394a44f14add6522656ce0ff [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"
Jeff Sharkey9c484982015-03-31 10:35:33 -070019#include "PrivateVolume.h"
Paul Crowley14c8c072018-09-18 13:30:21 -070020#include "PublicVolume.h"
Jeff Sharkeydeb24052015-03-02 21:01:40 -080021#include "Utils.h"
22#include "VolumeBase.h"
Jeff Sharkey36801cc2015-03-13 16:09:20 -070023#include "VolumeManager.h"
Jeff Sharkeydeb24052015-03-02 21:01:40 -080024
Elliott Hughes7e128fb2015-12-04 15:50:53 -080025#include <android-base/file.h>
Paul Crowley3b71fc52017-10-09 10:55:21 -070026#include <android-base/logging.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070027#include <android-base/parseint.h>
Jeff Sharkey46bb69f2017-06-21 13:52:23 -060028#include <android-base/properties.h>
Elliott Hughes7e128fb2015-12-04 15:50:53 -080029#include <android-base/stringprintf.h>
Jeff Sharkey3472e522017-10-06 18:02:53 -060030#include <android-base/strings.h>
Eric Biggersa701c452018-10-23 13:06:55 -070031#include <fscrypt/fscrypt.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080032
Greg Kaiser57f9af62018-02-16 13:13:58 -080033#include "cryptfs.h"
34
Jeff Sharkeydeb24052015-03-02 21:01:40 -080035#include <fcntl.h>
Jeff Sharkey38cfc022015-03-30 21:22:07 -070036#include <inttypes.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080037#include <stdio.h>
38#include <stdlib.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070039#include <sys/mount.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080040#include <sys/stat.h>
Elliott Hughes0e08e842017-05-18 09:08:24 -070041#include <sys/sysmacros.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070042#include <sys/types.h>
43#include <vector>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080044
Dan Albertae9e8902015-03-16 10:35:17 -070045using android::base::ReadFileToString;
46using android::base::StringPrintf;
Paul Crowley14c8c072018-09-18 13:30:21 -070047using android::base::WriteStringToFile;
Dan Albertae9e8902015-03-16 10:35:17 -070048
Jeff Sharkeydeb24052015-03-02 21:01:40 -080049namespace android {
50namespace vold {
51
52static const char* kSgdiskPath = "/system/bin/sgdisk";
53static const char* kSgdiskToken = " \t\n";
54
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060055static const char* kSysfsLoopMaxMinors = "/sys/module/loop/parameters/max_part";
Pierre-Hugues Hussonf347cd02017-11-28 15:42:56 +010056static const char* kSysfsMmcMaxMinorsDeprecated = "/sys/module/mmcblk/parameters/perdev_minors";
57static const char* kSysfsMmcMaxMinors = "/sys/module/mmc_block/parameters/perdev_minors";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080058
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060059static const unsigned int kMajorBlockLoop = 7;
Jeff Sharkeyf3ee2002015-04-19 15:55:42 -070060static const unsigned int kMajorBlockScsiA = 8;
61static const unsigned int kMajorBlockScsiB = 65;
62static const unsigned int kMajorBlockScsiC = 66;
63static const unsigned int kMajorBlockScsiD = 67;
64static const unsigned int kMajorBlockScsiE = 68;
65static const unsigned int kMajorBlockScsiF = 69;
66static const unsigned int kMajorBlockScsiG = 70;
67static const unsigned int kMajorBlockScsiH = 71;
68static const unsigned int kMajorBlockScsiI = 128;
69static const unsigned int kMajorBlockScsiJ = 129;
70static const unsigned int kMajorBlockScsiK = 130;
71static const unsigned int kMajorBlockScsiL = 131;
72static const unsigned int kMajorBlockScsiM = 132;
73static const unsigned int kMajorBlockScsiN = 133;
74static const unsigned int kMajorBlockScsiO = 134;
75static const unsigned int kMajorBlockScsiP = 135;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080076static const unsigned int kMajorBlockMmc = 179;
Yu Ning942d4e82016-01-08 17:36:47 +080077static const unsigned int kMajorBlockExperimentalMin = 240;
78static const unsigned int kMajorBlockExperimentalMax = 254;
Dmitry Shmidt06dc6e52018-05-11 17:22:42 -070079static const unsigned int kMajorBlockDynamicMin = 234;
80static const unsigned int kMajorBlockDynamicMax = 512;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080081
82static const char* kGptBasicData = "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7";
83static const char* kGptAndroidMeta = "19A710A2-B3CA-11E4-B026-10604B889DCF";
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070084static const char* kGptAndroidExpand = "193D1EA4-B3CA-11E4-B075-10604B889DCF";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080085
86enum class Table {
87 kUnknown,
88 kMbr,
89 kGpt,
90};
91
Yu Ning942d4e82016-01-08 17:36:47 +080092static bool isVirtioBlkDevice(unsigned int major) {
93 /*
94 * The new emulator's "ranchu" virtual board no longer includes a goldfish
95 * MMC-based SD card device; instead, it emulates SD cards with virtio-blk,
96 * which has been supported by upstream kernel and QEMU for quite a while.
97 * Unfortunately, the virtio-blk block device driver does not use a fixed
98 * major number, but relies on the kernel to assign one from a specific
99 * range of block majors, which are allocated for "LOCAL/EXPERIMENAL USE"
100 * per Documentation/devices.txt. This is true even for the latest Linux
101 * kernel (4.4; see init() in drivers/block/virtio_blk.c).
102 *
103 * This makes it difficult for vold to detect a virtio-blk based SD card.
104 * The current solution checks two conditions (both must be met):
105 *
106 * a) If the running environment is the emulator;
107 * b) If the major number is an experimental block device major number (for
108 * x86/x86_64 3.10 ranchu kernels, virtio-blk always gets major number
109 * 253, but it is safer to match the range than just one value).
110 *
111 * Other conditions could be used, too, e.g. the hardware name should be
112 * "ranchu", the device's sysfs path should end with "/block/vd[d-z]", etc.
113 * But just having a) and b) is enough for now.
114 */
Paul Crowley14c8c072018-09-18 13:30:21 -0700115 return IsRunningInEmulator() && major >= kMajorBlockExperimentalMin &&
116 major <= kMajorBlockExperimentalMax;
Yu Ning942d4e82016-01-08 17:36:47 +0800117}
118
Dmitry Shmidt06dc6e52018-05-11 17:22:42 -0700119static bool isNvmeBlkDevice(unsigned int major, const std::string& sysPath) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700120 return sysPath.find("nvme") != std::string::npos && major >= kMajorBlockDynamicMin &&
121 major <= kMajorBlockDynamicMax;
Dmitry Shmidt06dc6e52018-05-11 17:22:42 -0700122}
123
Paul Crowley14c8c072018-09-18 13:30:21 -0700124Disk::Disk(const std::string& eventPath, dev_t device, const std::string& nickname, int flags)
125 : mDevice(device),
126 mSize(-1),
127 mNickname(nickname),
128 mFlags(flags),
129 mCreated(false),
130 mJustPartitioned(false) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700131 mId = StringPrintf("disk:%u,%u", major(device), minor(device));
132 mEventPath = eventPath;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800133 mSysPath = StringPrintf("/sys/%s", eventPath.c_str());
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700134 mDevPath = StringPrintf("/dev/block/vold/%s", mId.c_str());
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800135 CreateDeviceNode(mDevPath, mDevice);
136}
137
138Disk::~Disk() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700139 CHECK(!mCreated);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800140 DestroyDeviceNode(mDevPath);
141}
142
143std::shared_ptr<VolumeBase> Disk::findVolume(const std::string& id) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700144 for (auto vol : mVolumes) {
145 if (vol->getId() == id) {
146 return vol;
147 }
148 auto stackedVol = vol->findVolume(id);
149 if (stackedVol != nullptr) {
150 return stackedVol;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800151 }
152 }
153 return nullptr;
154}
155
Greg Kaiser2bc201e2018-12-18 08:42:08 -0800156void Disk::listVolumes(VolumeBase::Type type, std::list<std::string>& list) const {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700157 for (const auto& vol : mVolumes) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700158 if (vol->getType() == type) {
159 list.push_back(vol->getId());
160 }
161 // TODO: consider looking at stacked volumes
162 }
163}
164
Martijn Coenen0a7e9922020-01-24 16:17:32 +0100165std::vector<std::shared_ptr<VolumeBase>> Disk::getVolumes() const {
166 std::vector<std::shared_ptr<VolumeBase>> vols;
167 for (const auto& vol : mVolumes) {
168 vols.push_back(vol);
169 auto stackedVolumes = vol->getVolumes();
170 vols.insert(vols.end(), stackedVolumes.begin(), stackedVolumes.end());
171 }
172
173 return vols;
174}
175
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700176status_t Disk::create() {
177 CHECK(!mCreated);
178 mCreated = true;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600179
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600180 auto listener = VolumeManager::Instance()->getListener();
181 if (listener) listener->onDiskCreated(getId(), mFlags);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600182
Risan82e90de2020-02-04 16:07:21 +0900183 if (isStub()) {
184 createStubVolume();
185 return OK;
186 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700187 readMetadata();
188 readPartitions();
189 return OK;
190}
191
192status_t Disk::destroy() {
193 CHECK(mCreated);
194 destroyAllVolumes();
195 mCreated = false;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600196
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600197 auto listener = VolumeManager::Instance()->getListener();
198 if (listener) listener->onDiskDestroyed(getId());
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600199
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700200 return OK;
201}
202
203void Disk::createPublicVolume(dev_t device) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700204 auto vol = std::shared_ptr<VolumeBase>(new PublicVolume(device));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700205 if (mJustPartitioned) {
206 LOG(DEBUG) << "Device just partitioned; silently formatting";
207 vol->setSilent(true);
208 vol->create();
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700209 vol->format("auto");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700210 vol->destroy();
211 vol->setSilent(false);
212 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700213
Jeff Sharkey9c484982015-03-31 10:35:33 -0700214 mVolumes.push_back(vol);
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700215 vol->setDiskId(getId());
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700216 vol->create();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700217}
218
Jeff Sharkey9c484982015-03-31 10:35:33 -0700219void Disk::createPrivateVolume(dev_t device, const std::string& partGuid) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700220 std::string normalizedGuid;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700221 if (NormalizeHex(partGuid, normalizedGuid)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700222 LOG(WARNING) << "Invalid GUID " << partGuid;
223 return;
224 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700225
226 std::string keyRaw;
227 if (!ReadFileToString(BuildKeyPath(normalizedGuid), &keyRaw)) {
228 PLOG(ERROR) << "Failed to load key for GUID " << normalizedGuid;
229 return;
230 }
231
232 LOG(DEBUG) << "Found key for GUID " << normalizedGuid;
233
Paul Crowley3d98f5d2020-02-07 11:49:09 -0800234 auto keyBuffer = KeyBuffer(keyRaw.begin(), keyRaw.end());
235 auto vol = std::shared_ptr<VolumeBase>(new PrivateVolume(device, keyBuffer));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700236 if (mJustPartitioned) {
237 LOG(DEBUG) << "Device just partitioned; silently formatting";
238 vol->setSilent(true);
239 vol->create();
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700240 vol->format("auto");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700241 vol->destroy();
242 vol->setSilent(false);
243 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700244
245 mVolumes.push_back(vol);
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700246 vol->setDiskId(getId());
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700247 vol->setPartGuid(partGuid);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700248 vol->create();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700249}
250
Risan82e90de2020-02-04 16:07:21 +0900251void Disk::createStubVolume() {
252 CHECK(mVolumes.size() == 1);
253 auto listener = VolumeManager::Instance()->getListener();
254 if (listener) listener->onDiskMetadataChanged(getId(), mSize, mLabel, mSysPath);
255 if (listener) listener->onDiskScanned(getId());
256 mVolumes[0]->setDiskId(getId());
257 mVolumes[0]->create();
258}
259
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700260void Disk::destroyAllVolumes() {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700261 for (const auto& vol : mVolumes) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700262 vol->destroy();
263 }
264 mVolumes.clear();
265}
266
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800267status_t Disk::readMetadata() {
268 mSize = -1;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700269 mLabel.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800270
Oleksiy Avramchenko625dc782018-05-23 10:50:46 +0200271 if (GetBlockDevSize(mDevPath, &mSize) != OK) {
272 mSize = -1;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800273 }
274
Yu Ning942d4e82016-01-08 17:36:47 +0800275 unsigned int majorId = major(mDevice);
276 switch (majorId) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700277 case kMajorBlockLoop: {
Yu Ning942d4e82016-01-08 17:36:47 +0800278 mLabel = "Virtual";
279 break;
280 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700281 // clang-format off
282 case kMajorBlockScsiA: case kMajorBlockScsiB: case kMajorBlockScsiC:
283 case kMajorBlockScsiD: case kMajorBlockScsiE: case kMajorBlockScsiF:
284 case kMajorBlockScsiG: case kMajorBlockScsiH: case kMajorBlockScsiI:
285 case kMajorBlockScsiJ: case kMajorBlockScsiK: case kMajorBlockScsiL:
286 case kMajorBlockScsiM: case kMajorBlockScsiN: case kMajorBlockScsiO:
287 case kMajorBlockScsiP: {
288 // clang-format on
289 std::string path(mSysPath + "/device/vendor");
Dmitry Shmidt06dc6e52018-05-11 17:22:42 -0700290 std::string tmp;
291 if (!ReadFileToString(path, &tmp)) {
292 PLOG(WARNING) << "Failed to read vendor from " << path;
293 return -errno;
294 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700295 tmp = android::base::Trim(tmp);
Dmitry Shmidt06dc6e52018-05-11 17:22:42 -0700296 mLabel = tmp;
297 break;
298 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700299 case kMajorBlockMmc: {
300 std::string path(mSysPath + "/device/manfid");
301 std::string tmp;
302 if (!ReadFileToString(path, &tmp)) {
303 PLOG(WARNING) << "Failed to read manufacturer from " << path;
304 return -errno;
305 }
306 tmp = android::base::Trim(tmp);
307 int64_t manfid;
308 if (!android::base::ParseInt(tmp, &manfid)) {
309 PLOG(WARNING) << "Failed to parse manufacturer " << tmp;
310 return -EINVAL;
311 }
312 // Our goal here is to give the user a meaningful label, ideally
313 // matching whatever is silk-screened on the card. To reduce
314 // user confusion, this list doesn't contain white-label manfid.
315 switch (manfid) {
316 // clang-format off
317 case 0x000003: mLabel = "SanDisk"; break;
318 case 0x00001b: mLabel = "Samsung"; break;
319 case 0x000028: mLabel = "Lexar"; break;
320 case 0x000074: mLabel = "Transcend"; break;
321 // clang-format on
322 }
323 break;
324 }
325 default: {
326 if (isVirtioBlkDevice(majorId)) {
327 LOG(DEBUG) << "Recognized experimental block major ID " << majorId
328 << " as virtio-blk (emulator's virtual SD card device)";
329 mLabel = "Virtual";
330 break;
331 }
332 if (isNvmeBlkDevice(majorId, mSysPath)) {
333 std::string path(mSysPath + "/device/model");
334 std::string tmp;
335 if (!ReadFileToString(path, &tmp)) {
336 PLOG(WARNING) << "Failed to read vendor from " << path;
337 return -errno;
338 }
339 mLabel = tmp;
340 break;
341 }
342 LOG(WARNING) << "Unsupported block major type " << majorId;
343 return -ENOTSUP;
344 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800345 }
346
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600347 auto listener = VolumeManager::Instance()->getListener();
Paul Crowley14c8c072018-09-18 13:30:21 -0700348 if (listener) listener->onDiskMetadataChanged(getId(), mSize, mLabel, mSysPath);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600349
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800350 return OK;
351}
352
353status_t Disk::readPartitions() {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600354 int maxMinors = getMaxMinors();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800355 if (maxMinors < 0) {
356 return -ENOTSUP;
357 }
358
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700359 destroyAllVolumes();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800360
361 // Parse partition table
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700362
363 std::vector<std::string> cmd;
364 cmd.push_back(kSgdiskPath);
365 cmd.push_back("--android-dump");
366 cmd.push_back(mDevPath);
367
368 std::vector<std::string> output;
Paul Crowleyde2d6202018-11-30 11:43:47 -0800369 status_t res = ForkExecvp(cmd, &output);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700370 if (res != OK) {
371 LOG(WARNING) << "sgdisk failed to scan " << mDevPath;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600372
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600373 auto listener = VolumeManager::Instance()->getListener();
374 if (listener) listener->onDiskScanned(getId());
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600375
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700376 mJustPartitioned = false;
377 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800378 }
379
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800380 Table table = Table::kUnknown;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700381 bool foundParts = false;
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700382 for (const auto& line : output) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600383 auto split = android::base::Split(line, kSgdiskToken);
384 auto it = split.begin();
385 if (it == split.end()) continue;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700386
Jeff Sharkey3472e522017-10-06 18:02:53 -0600387 if (*it == "DISK") {
388 if (++it == split.end()) continue;
389 if (*it == "mbr") {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800390 table = Table::kMbr;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600391 } else if (*it == "gpt") {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800392 table = Table::kGpt;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600393 } else {
394 LOG(WARNING) << "Invalid partition table " << *it;
395 continue;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800396 }
Jeff Sharkey3472e522017-10-06 18:02:53 -0600397 } else if (*it == "PART") {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700398 foundParts = true;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600399
400 if (++it == split.end()) continue;
401 int i = 0;
402 if (!android::base::ParseInt(*it, &i, 1, maxMinors)) {
403 LOG(WARNING) << "Invalid partition number " << *it;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800404 continue;
405 }
406 dev_t partDevice = makedev(major(mDevice), minor(mDevice) + i);
407
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800408 if (table == Table::kMbr) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600409 if (++it == split.end()) continue;
410 int type = 0;
411 if (!android::base::ParseInt("0x" + *it, &type)) {
412 LOG(WARNING) << "Invalid partition type " << *it;
413 continue;
414 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800415
Jeff Sharkey3472e522017-10-06 18:02:53 -0600416 switch (type) {
Jeff Sharkey37ba1252018-01-19 10:55:18 +0900417 case 0x06: // FAT16
418 case 0x07: // HPFS/NTFS/exFAT
419 case 0x0b: // W95 FAT32 (LBA)
420 case 0x0c: // W95 FAT32 (LBA)
421 case 0x0e: // W95 FAT16 (LBA)
422 createPublicVolume(partDevice);
423 break;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800424 }
425 } else if (table == Table::kGpt) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600426 if (++it == split.end()) continue;
427 auto typeGuid = *it;
428 if (++it == split.end()) continue;
429 auto partGuid = *it;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800430
Jeff Sharkey3472e522017-10-06 18:02:53 -0600431 if (android::base::EqualsIgnoreCase(typeGuid, kGptBasicData)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700432 createPublicVolume(partDevice);
Jeff Sharkey3472e522017-10-06 18:02:53 -0600433 } else if (android::base::EqualsIgnoreCase(typeGuid, kGptAndroidExpand)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700434 createPrivateVolume(partDevice, partGuid);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800435 }
436 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800437 }
438 }
439
440 // Ugly last ditch effort, treat entire disk as partition
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700441 if (table == Table::kUnknown || !foundParts) {
442 LOG(WARNING) << mId << " has unknown partition table; trying entire device";
Jeff Sharkey63123c02015-06-26 11:16:14 -0700443
444 std::string fsType;
445 std::string unused;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600446 if (ReadMetadataUntrusted(mDevPath, &fsType, &unused, &unused) == OK) {
Jeff Sharkey63123c02015-06-26 11:16:14 -0700447 createPublicVolume(mDevice);
448 } else {
449 LOG(WARNING) << mId << " failed to identify, giving up";
450 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800451 }
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700452
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600453 auto listener = VolumeManager::Instance()->getListener();
454 if (listener) listener->onDiskScanned(getId());
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600455
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700456 mJustPartitioned = false;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800457 return OK;
458}
459
Risan82e90de2020-02-04 16:07:21 +0900460void Disk::initializePartition(std::shared_ptr<StubVolume> vol) {
461 CHECK(isStub());
462 CHECK(mVolumes.empty());
463 mVolumes.push_back(vol);
464}
465
Jeff Sharkey9c484982015-03-31 10:35:33 -0700466status_t Disk::unmountAll() {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700467 for (const auto& vol : mVolumes) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700468 vol->unmount();
469 }
470 return OK;
471}
472
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800473status_t Disk::partitionPublic() {
Jeff Sharkeydadccee2015-09-23 14:13:45 -0700474 int res;
475
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700476 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700477 mJustPartitioned = true;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800478
Jeff Sharkeydadccee2015-09-23 14:13:45 -0700479 // First nuke any existing partition table
480 std::vector<std::string> cmd;
481 cmd.push_back(kSgdiskPath);
482 cmd.push_back("--zap-all");
483 cmd.push_back(mDevPath);
484
485 // Zap sometimes returns an error when it actually succeeded, so
486 // just log as warning and keep rolling forward.
487 if ((res = ForkExecvp(cmd)) != 0) {
488 LOG(WARNING) << "Failed to zap; status " << res;
489 }
490
Jeff Sharkey68f1b8b2017-10-18 14:09:52 -0600491 // Now let's build the new MBR table. We heavily rely on sgdisk to
492 // force optimal alignment on the created partitions.
493 cmd.clear();
494 cmd.push_back(kSgdiskPath);
495 cmd.push_back("--new=0:0:-0");
496 cmd.push_back("--typecode=0:0c00");
497 cmd.push_back("--gpttombr=1");
498 cmd.push_back(mDevPath);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800499
Jeff Sharkey68f1b8b2017-10-18 14:09:52 -0600500 if ((res = ForkExecvp(cmd)) != 0) {
501 LOG(ERROR) << "Failed to partition; status " << res;
502 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800503 }
504
Jeff Sharkey68f1b8b2017-10-18 14:09:52 -0600505 return OK;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800506}
507
508status_t Disk::partitionPrivate() {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700509 return partitionMixed(0);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800510}
511
512status_t Disk::partitionMixed(int8_t ratio) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700513 int res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700514
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700515 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700516 mJustPartitioned = true;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700517
518 // First nuke any existing partition table
519 std::vector<std::string> cmd;
520 cmd.push_back(kSgdiskPath);
521 cmd.push_back("--zap-all");
522 cmd.push_back(mDevPath);
523
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700524 // Zap sometimes returns an error when it actually succeeded, so
525 // just log as warning and keep rolling forward.
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700526 if ((res = ForkExecvp(cmd)) != 0) {
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700527 LOG(WARNING) << "Failed to zap; status " << res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700528 }
529
530 // We've had some success above, so generate both the private partition
531 // GUID and encryption key and persist them.
532 std::string partGuidRaw;
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600533 if (GenerateRandomUuid(partGuidRaw) != OK) {
534 LOG(ERROR) << "Failed to generate GUID";
535 return -EIO;
536 }
537
Jeff Sharkey9c484982015-03-31 10:35:33 -0700538 std::string keyRaw;
Greg Kaiser57f9af62018-02-16 13:13:58 -0800539 if (ReadRandomBytes(cryptfs_get_keysize(), keyRaw) != OK) {
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600540 LOG(ERROR) << "Failed to generate key";
Jeff Sharkey9c484982015-03-31 10:35:33 -0700541 return -EIO;
542 }
543
544 std::string partGuid;
545 StrToHex(partGuidRaw, partGuid);
546
547 if (!WriteStringToFile(keyRaw, BuildKeyPath(partGuid))) {
548 LOG(ERROR) << "Failed to persist key";
549 return -EIO;
550 } else {
551 LOG(DEBUG) << "Persisted key for GUID " << partGuid;
552 }
553
554 // Now let's build the new GPT table. We heavily rely on sgdisk to
555 // force optimal alignment on the created partitions.
556 cmd.clear();
557 cmd.push_back(kSgdiskPath);
558
559 // If requested, create a public partition first. Mixed-mode partitioning
560 // like this is an experimental feature.
561 if (ratio > 0) {
562 if (ratio < 10 || ratio > 90) {
563 LOG(ERROR) << "Mixed partition ratio must be between 10-90%";
564 return -EINVAL;
565 }
566
567 uint64_t splitMb = ((mSize / 100) * ratio) / 1024 / 1024;
568 cmd.push_back(StringPrintf("--new=0:0:+%" PRId64 "M", splitMb));
569 cmd.push_back(StringPrintf("--typecode=0:%s", kGptBasicData));
570 cmd.push_back("--change-name=0:shared");
571 }
572
573 // Define a metadata partition which is designed for future use; there
574 // should only be one of these per physical device, even if there are
575 // multiple private volumes.
576 cmd.push_back("--new=0:0:+16M");
577 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidMeta));
578 cmd.push_back("--change-name=0:android_meta");
579
580 // Define a single private partition filling the rest of disk.
581 cmd.push_back("--new=0:0:-0");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700582 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidExpand));
Jeff Sharkey9c484982015-03-31 10:35:33 -0700583 cmd.push_back(StringPrintf("--partition-guid=0:%s", partGuid.c_str()));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700584 cmd.push_back("--change-name=0:android_expand");
Jeff Sharkey9c484982015-03-31 10:35:33 -0700585
586 cmd.push_back(mDevPath);
587
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700588 if ((res = ForkExecvp(cmd)) != 0) {
589 LOG(ERROR) << "Failed to partition; status " << res;
590 return res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700591 }
592
593 return OK;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800594}
595
596int Disk::getMaxMinors() {
597 // Figure out maximum partition devices supported
Yu Ning942d4e82016-01-08 17:36:47 +0800598 unsigned int majorId = major(mDevice);
599 switch (majorId) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700600 case kMajorBlockLoop: {
601 std::string tmp;
602 if (!ReadFileToString(kSysfsLoopMaxMinors, &tmp)) {
603 LOG(ERROR) << "Failed to read max minors";
604 return -errno;
605 }
606 return std::stoi(tmp);
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600607 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700608 // clang-format off
609 case kMajorBlockScsiA: case kMajorBlockScsiB: case kMajorBlockScsiC:
610 case kMajorBlockScsiD: case kMajorBlockScsiE: case kMajorBlockScsiF:
611 case kMajorBlockScsiG: case kMajorBlockScsiH: case kMajorBlockScsiI:
612 case kMajorBlockScsiJ: case kMajorBlockScsiK: case kMajorBlockScsiL:
613 case kMajorBlockScsiM: case kMajorBlockScsiN: case kMajorBlockScsiO:
614 case kMajorBlockScsiP: {
615 // clang-format on
616 // Per Documentation/devices.txt this is static
Yu Ning942d4e82016-01-08 17:36:47 +0800617 return 15;
618 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700619 case kMajorBlockMmc: {
620 // Per Documentation/devices.txt this is dynamic
621 std::string tmp;
622 if (!ReadFileToString(kSysfsMmcMaxMinors, &tmp) &&
623 !ReadFileToString(kSysfsMmcMaxMinorsDeprecated, &tmp)) {
624 LOG(ERROR) << "Failed to read max minors";
625 return -errno;
626 }
627 return std::stoi(tmp);
Dmitry Shmidt06dc6e52018-05-11 17:22:42 -0700628 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700629 default: {
630 if (isVirtioBlkDevice(majorId)) {
631 // drivers/block/virtio_blk.c has "#define PART_BITS 4", so max is
632 // 2^4 - 1 = 15
633 return 15;
634 }
635 if (isNvmeBlkDevice(majorId, mSysPath)) {
636 // despite kernel nvme driver supports up to 1M minors,
637 // #define NVME_MINORS (1U << MINORBITS)
638 // sgdisk can not support more than 127 partitions, due to
639 // #define MAX_MBR_PARTS 128
640 return 127;
641 }
642 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800643 }
644
Yu Ning942d4e82016-01-08 17:36:47 +0800645 LOG(ERROR) << "Unsupported block major type " << majorId;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800646 return -ENOTSUP;
647}
648
649} // namespace vold
650} // namespace android