blob: a4324db11cd1383038cee48fd34b62bac707d94b [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"
Paul Crowley886e5722020-02-07 12:51:56 -080023#include "VolumeEncryption.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
34#include <fcntl.h>
Jeff Sharkey38cfc022015-03-30 21:22:07 -070035#include <inttypes.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080036#include <stdio.h>
37#include <stdlib.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070038#include <sys/mount.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080039#include <sys/stat.h>
Elliott Hughes0e08e842017-05-18 09:08:24 -070040#include <sys/sysmacros.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070041#include <sys/types.h>
42#include <vector>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080043
Dan Albertae9e8902015-03-16 10:35:17 -070044using android::base::ReadFileToString;
45using android::base::StringPrintf;
Paul Crowley14c8c072018-09-18 13:30:21 -070046using android::base::WriteStringToFile;
Dan Albertae9e8902015-03-16 10:35:17 -070047
Jeff Sharkeydeb24052015-03-02 21:01:40 -080048namespace android {
49namespace vold {
50
51static const char* kSgdiskPath = "/system/bin/sgdisk";
52static const char* kSgdiskToken = " \t\n";
53
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060054static const char* kSysfsLoopMaxMinors = "/sys/module/loop/parameters/max_part";
Pierre-Hugues Hussonf347cd02017-11-28 15:42:56 +010055static const char* kSysfsMmcMaxMinorsDeprecated = "/sys/module/mmcblk/parameters/perdev_minors";
56static const char* kSysfsMmcMaxMinors = "/sys/module/mmc_block/parameters/perdev_minors";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080057
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060058static const unsigned int kMajorBlockLoop = 7;
Jeff Sharkeyf3ee2002015-04-19 15:55:42 -070059static const unsigned int kMajorBlockScsiA = 8;
60static const unsigned int kMajorBlockScsiB = 65;
61static const unsigned int kMajorBlockScsiC = 66;
62static const unsigned int kMajorBlockScsiD = 67;
63static const unsigned int kMajorBlockScsiE = 68;
64static const unsigned int kMajorBlockScsiF = 69;
65static const unsigned int kMajorBlockScsiG = 70;
66static const unsigned int kMajorBlockScsiH = 71;
67static const unsigned int kMajorBlockScsiI = 128;
68static const unsigned int kMajorBlockScsiJ = 129;
69static const unsigned int kMajorBlockScsiK = 130;
70static const unsigned int kMajorBlockScsiL = 131;
71static const unsigned int kMajorBlockScsiM = 132;
72static const unsigned int kMajorBlockScsiN = 133;
73static const unsigned int kMajorBlockScsiO = 134;
74static const unsigned int kMajorBlockScsiP = 135;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080075static const unsigned int kMajorBlockMmc = 179;
Yu Ning942d4e82016-01-08 17:36:47 +080076static const unsigned int kMajorBlockExperimentalMin = 240;
77static const unsigned int kMajorBlockExperimentalMax = 254;
Dmitry Shmidt06dc6e52018-05-11 17:22:42 -070078static const unsigned int kMajorBlockDynamicMin = 234;
79static const unsigned int kMajorBlockDynamicMax = 512;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080080
81static const char* kGptBasicData = "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7";
82static const char* kGptAndroidMeta = "19A710A2-B3CA-11E4-B026-10604B889DCF";
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070083static const char* kGptAndroidExpand = "193D1EA4-B3CA-11E4-B075-10604B889DCF";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080084
85enum class Table {
86 kUnknown,
87 kMbr,
88 kGpt,
89};
90
Yu Ning942d4e82016-01-08 17:36:47 +080091static bool isVirtioBlkDevice(unsigned int major) {
92 /*
93 * The new emulator's "ranchu" virtual board no longer includes a goldfish
94 * MMC-based SD card device; instead, it emulates SD cards with virtio-blk,
95 * which has been supported by upstream kernel and QEMU for quite a while.
96 * Unfortunately, the virtio-blk block device driver does not use a fixed
97 * major number, but relies on the kernel to assign one from a specific
98 * range of block majors, which are allocated for "LOCAL/EXPERIMENAL USE"
99 * per Documentation/devices.txt. This is true even for the latest Linux
100 * kernel (4.4; see init() in drivers/block/virtio_blk.c).
101 *
102 * This makes it difficult for vold to detect a virtio-blk based SD card.
103 * The current solution checks two conditions (both must be met):
104 *
105 * a) If the running environment is the emulator;
106 * b) If the major number is an experimental block device major number (for
107 * x86/x86_64 3.10 ranchu kernels, virtio-blk always gets major number
108 * 253, but it is safer to match the range than just one value).
109 *
110 * Other conditions could be used, too, e.g. the hardware name should be
111 * "ranchu", the device's sysfs path should end with "/block/vd[d-z]", etc.
112 * But just having a) and b) is enough for now.
113 */
Paul Crowley14c8c072018-09-18 13:30:21 -0700114 return IsRunningInEmulator() && major >= kMajorBlockExperimentalMin &&
115 major <= kMajorBlockExperimentalMax;
Yu Ning942d4e82016-01-08 17:36:47 +0800116}
117
Dmitry Shmidt06dc6e52018-05-11 17:22:42 -0700118static bool isNvmeBlkDevice(unsigned int major, const std::string& sysPath) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700119 return sysPath.find("nvme") != std::string::npos && major >= kMajorBlockDynamicMin &&
120 major <= kMajorBlockDynamicMax;
Dmitry Shmidt06dc6e52018-05-11 17:22:42 -0700121}
122
Paul Crowley14c8c072018-09-18 13:30:21 -0700123Disk::Disk(const std::string& eventPath, dev_t device, const std::string& nickname, int flags)
124 : mDevice(device),
125 mSize(-1),
126 mNickname(nickname),
127 mFlags(flags),
128 mCreated(false),
129 mJustPartitioned(false) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700130 mId = StringPrintf("disk:%u,%u", major(device), minor(device));
131 mEventPath = eventPath;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800132 mSysPath = StringPrintf("/sys/%s", eventPath.c_str());
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700133 mDevPath = StringPrintf("/dev/block/vold/%s", mId.c_str());
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800134 CreateDeviceNode(mDevPath, mDevice);
135}
136
137Disk::~Disk() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700138 CHECK(!mCreated);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800139 DestroyDeviceNode(mDevPath);
140}
141
142std::shared_ptr<VolumeBase> Disk::findVolume(const std::string& id) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700143 for (auto vol : mVolumes) {
144 if (vol->getId() == id) {
145 return vol;
146 }
147 auto stackedVol = vol->findVolume(id);
148 if (stackedVol != nullptr) {
149 return stackedVol;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800150 }
151 }
152 return nullptr;
153}
154
Greg Kaiser2bc201e2018-12-18 08:42:08 -0800155void Disk::listVolumes(VolumeBase::Type type, std::list<std::string>& list) const {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700156 for (const auto& vol : mVolumes) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700157 if (vol->getType() == type) {
158 list.push_back(vol->getId());
159 }
160 // TODO: consider looking at stacked volumes
161 }
162}
163
Martijn Coenen0a7e9922020-01-24 16:17:32 +0100164std::vector<std::shared_ptr<VolumeBase>> Disk::getVolumes() const {
165 std::vector<std::shared_ptr<VolumeBase>> vols;
166 for (const auto& vol : mVolumes) {
167 vols.push_back(vol);
168 auto stackedVolumes = vol->getVolumes();
169 vols.insert(vols.end(), stackedVolumes.begin(), stackedVolumes.end());
170 }
171
172 return vols;
173}
174
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700175status_t Disk::create() {
176 CHECK(!mCreated);
177 mCreated = true;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600178
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600179 auto listener = VolumeManager::Instance()->getListener();
180 if (listener) listener->onDiskCreated(getId(), mFlags);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600181
Risan82e90de2020-02-04 16:07:21 +0900182 if (isStub()) {
183 createStubVolume();
184 return OK;
185 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700186 readMetadata();
187 readPartitions();
188 return OK;
189}
190
191status_t Disk::destroy() {
192 CHECK(mCreated);
193 destroyAllVolumes();
194 mCreated = false;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600195
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600196 auto listener = VolumeManager::Instance()->getListener();
197 if (listener) listener->onDiskDestroyed(getId());
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600198
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700199 return OK;
200}
201
202void Disk::createPublicVolume(dev_t device) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700203 auto vol = std::shared_ptr<VolumeBase>(new PublicVolume(device));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700204 if (mJustPartitioned) {
205 LOG(DEBUG) << "Device just partitioned; silently formatting";
206 vol->setSilent(true);
207 vol->create();
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700208 vol->format("auto");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700209 vol->destroy();
210 vol->setSilent(false);
211 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700212
Jeff Sharkey9c484982015-03-31 10:35:33 -0700213 mVolumes.push_back(vol);
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700214 vol->setDiskId(getId());
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700215 vol->create();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700216}
217
Jeff Sharkey9c484982015-03-31 10:35:33 -0700218void Disk::createPrivateVolume(dev_t device, const std::string& partGuid) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700219 std::string normalizedGuid;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700220 if (NormalizeHex(partGuid, normalizedGuid)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700221 LOG(WARNING) << "Invalid GUID " << partGuid;
222 return;
223 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700224
225 std::string keyRaw;
226 if (!ReadFileToString(BuildKeyPath(normalizedGuid), &keyRaw)) {
227 PLOG(ERROR) << "Failed to load key for GUID " << normalizedGuid;
228 return;
229 }
230
231 LOG(DEBUG) << "Found key for GUID " << normalizedGuid;
232
Paul Crowley3d98f5d2020-02-07 11:49:09 -0800233 auto keyBuffer = KeyBuffer(keyRaw.begin(), keyRaw.end());
234 auto vol = std::shared_ptr<VolumeBase>(new PrivateVolume(device, keyBuffer));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700235 if (mJustPartitioned) {
236 LOG(DEBUG) << "Device just partitioned; silently formatting";
237 vol->setSilent(true);
238 vol->create();
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700239 vol->format("auto");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700240 vol->destroy();
241 vol->setSilent(false);
242 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700243
244 mVolumes.push_back(vol);
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700245 vol->setDiskId(getId());
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700246 vol->setPartGuid(partGuid);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700247 vol->create();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700248}
249
Risan82e90de2020-02-04 16:07:21 +0900250void Disk::createStubVolume() {
251 CHECK(mVolumes.size() == 1);
252 auto listener = VolumeManager::Instance()->getListener();
253 if (listener) listener->onDiskMetadataChanged(getId(), mSize, mLabel, mSysPath);
254 if (listener) listener->onDiskScanned(getId());
255 mVolumes[0]->setDiskId(getId());
256 mVolumes[0]->create();
257}
258
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700259void Disk::destroyAllVolumes() {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700260 for (const auto& vol : mVolumes) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700261 vol->destroy();
262 }
263 mVolumes.clear();
264}
265
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800266status_t Disk::readMetadata() {
267 mSize = -1;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700268 mLabel.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800269
Oleksiy Avramchenko625dc782018-05-23 10:50:46 +0200270 if (GetBlockDevSize(mDevPath, &mSize) != OK) {
271 mSize = -1;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800272 }
273
Yu Ning942d4e82016-01-08 17:36:47 +0800274 unsigned int majorId = major(mDevice);
275 switch (majorId) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700276 case kMajorBlockLoop: {
Yu Ning942d4e82016-01-08 17:36:47 +0800277 mLabel = "Virtual";
278 break;
279 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700280 // clang-format off
281 case kMajorBlockScsiA: case kMajorBlockScsiB: case kMajorBlockScsiC:
282 case kMajorBlockScsiD: case kMajorBlockScsiE: case kMajorBlockScsiF:
283 case kMajorBlockScsiG: case kMajorBlockScsiH: case kMajorBlockScsiI:
284 case kMajorBlockScsiJ: case kMajorBlockScsiK: case kMajorBlockScsiL:
285 case kMajorBlockScsiM: case kMajorBlockScsiN: case kMajorBlockScsiO:
286 case kMajorBlockScsiP: {
287 // clang-format on
288 std::string path(mSysPath + "/device/vendor");
Dmitry Shmidt06dc6e52018-05-11 17:22:42 -0700289 std::string tmp;
290 if (!ReadFileToString(path, &tmp)) {
291 PLOG(WARNING) << "Failed to read vendor from " << path;
292 return -errno;
293 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700294 tmp = android::base::Trim(tmp);
Dmitry Shmidt06dc6e52018-05-11 17:22:42 -0700295 mLabel = tmp;
296 break;
297 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700298 case kMajorBlockMmc: {
299 std::string path(mSysPath + "/device/manfid");
300 std::string tmp;
301 if (!ReadFileToString(path, &tmp)) {
302 PLOG(WARNING) << "Failed to read manufacturer from " << path;
303 return -errno;
304 }
305 tmp = android::base::Trim(tmp);
306 int64_t manfid;
307 if (!android::base::ParseInt(tmp, &manfid)) {
308 PLOG(WARNING) << "Failed to parse manufacturer " << tmp;
309 return -EINVAL;
310 }
311 // Our goal here is to give the user a meaningful label, ideally
312 // matching whatever is silk-screened on the card. To reduce
313 // user confusion, this list doesn't contain white-label manfid.
314 switch (manfid) {
315 // clang-format off
316 case 0x000003: mLabel = "SanDisk"; break;
317 case 0x00001b: mLabel = "Samsung"; break;
318 case 0x000028: mLabel = "Lexar"; break;
319 case 0x000074: mLabel = "Transcend"; break;
320 // clang-format on
321 }
322 break;
323 }
324 default: {
325 if (isVirtioBlkDevice(majorId)) {
326 LOG(DEBUG) << "Recognized experimental block major ID " << majorId
327 << " as virtio-blk (emulator's virtual SD card device)";
328 mLabel = "Virtual";
329 break;
330 }
331 if (isNvmeBlkDevice(majorId, mSysPath)) {
332 std::string path(mSysPath + "/device/model");
333 std::string tmp;
334 if (!ReadFileToString(path, &tmp)) {
335 PLOG(WARNING) << "Failed to read vendor from " << path;
336 return -errno;
337 }
338 mLabel = tmp;
339 break;
340 }
341 LOG(WARNING) << "Unsupported block major type " << majorId;
342 return -ENOTSUP;
343 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800344 }
345
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600346 auto listener = VolumeManager::Instance()->getListener();
Paul Crowley14c8c072018-09-18 13:30:21 -0700347 if (listener) listener->onDiskMetadataChanged(getId(), mSize, mLabel, mSysPath);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600348
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800349 return OK;
350}
351
352status_t Disk::readPartitions() {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600353 int maxMinors = getMaxMinors();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800354 if (maxMinors < 0) {
355 return -ENOTSUP;
356 }
357
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700358 destroyAllVolumes();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800359
360 // Parse partition table
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700361
362 std::vector<std::string> cmd;
363 cmd.push_back(kSgdiskPath);
364 cmd.push_back("--android-dump");
365 cmd.push_back(mDevPath);
366
367 std::vector<std::string> output;
Paul Crowleyde2d6202018-11-30 11:43:47 -0800368 status_t res = ForkExecvp(cmd, &output);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700369 if (res != OK) {
370 LOG(WARNING) << "sgdisk failed to scan " << mDevPath;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600371
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600372 auto listener = VolumeManager::Instance()->getListener();
373 if (listener) listener->onDiskScanned(getId());
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600374
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700375 mJustPartitioned = false;
376 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800377 }
378
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800379 Table table = Table::kUnknown;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700380 bool foundParts = false;
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700381 for (const auto& line : output) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600382 auto split = android::base::Split(line, kSgdiskToken);
383 auto it = split.begin();
384 if (it == split.end()) continue;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700385
Jeff Sharkey3472e522017-10-06 18:02:53 -0600386 if (*it == "DISK") {
387 if (++it == split.end()) continue;
388 if (*it == "mbr") {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800389 table = Table::kMbr;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600390 } else if (*it == "gpt") {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800391 table = Table::kGpt;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600392 } else {
393 LOG(WARNING) << "Invalid partition table " << *it;
394 continue;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800395 }
Jeff Sharkey3472e522017-10-06 18:02:53 -0600396 } else if (*it == "PART") {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700397 foundParts = true;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600398
399 if (++it == split.end()) continue;
400 int i = 0;
401 if (!android::base::ParseInt(*it, &i, 1, maxMinors)) {
402 LOG(WARNING) << "Invalid partition number " << *it;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800403 continue;
404 }
405 dev_t partDevice = makedev(major(mDevice), minor(mDevice) + i);
406
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800407 if (table == Table::kMbr) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600408 if (++it == split.end()) continue;
409 int type = 0;
410 if (!android::base::ParseInt("0x" + *it, &type)) {
411 LOG(WARNING) << "Invalid partition type " << *it;
412 continue;
413 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800414
Jeff Sharkey3472e522017-10-06 18:02:53 -0600415 switch (type) {
Jeff Sharkey37ba1252018-01-19 10:55:18 +0900416 case 0x06: // FAT16
417 case 0x07: // HPFS/NTFS/exFAT
418 case 0x0b: // W95 FAT32 (LBA)
419 case 0x0c: // W95 FAT32 (LBA)
420 case 0x0e: // W95 FAT16 (LBA)
421 createPublicVolume(partDevice);
422 break;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800423 }
424 } else if (table == Table::kGpt) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600425 if (++it == split.end()) continue;
426 auto typeGuid = *it;
427 if (++it == split.end()) continue;
428 auto partGuid = *it;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800429
Jeff Sharkey3472e522017-10-06 18:02:53 -0600430 if (android::base::EqualsIgnoreCase(typeGuid, kGptBasicData)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700431 createPublicVolume(partDevice);
Jeff Sharkey3472e522017-10-06 18:02:53 -0600432 } else if (android::base::EqualsIgnoreCase(typeGuid, kGptAndroidExpand)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700433 createPrivateVolume(partDevice, partGuid);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800434 }
435 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800436 }
437 }
438
439 // Ugly last ditch effort, treat entire disk as partition
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700440 if (table == Table::kUnknown || !foundParts) {
441 LOG(WARNING) << mId << " has unknown partition table; trying entire device";
Jeff Sharkey63123c02015-06-26 11:16:14 -0700442
443 std::string fsType;
444 std::string unused;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600445 if (ReadMetadataUntrusted(mDevPath, &fsType, &unused, &unused) == OK) {
Jeff Sharkey63123c02015-06-26 11:16:14 -0700446 createPublicVolume(mDevice);
447 } else {
448 LOG(WARNING) << mId << " failed to identify, giving up";
449 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800450 }
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700451
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600452 auto listener = VolumeManager::Instance()->getListener();
453 if (listener) listener->onDiskScanned(getId());
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600454
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700455 mJustPartitioned = false;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800456 return OK;
457}
458
Risan82e90de2020-02-04 16:07:21 +0900459void Disk::initializePartition(std::shared_ptr<StubVolume> vol) {
460 CHECK(isStub());
461 CHECK(mVolumes.empty());
462 mVolumes.push_back(vol);
463}
464
Jeff Sharkey9c484982015-03-31 10:35:33 -0700465status_t Disk::unmountAll() {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700466 for (const auto& vol : mVolumes) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700467 vol->unmount();
468 }
469 return OK;
470}
471
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800472status_t Disk::partitionPublic() {
Jeff Sharkeydadccee2015-09-23 14:13:45 -0700473 int res;
474
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700475 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700476 mJustPartitioned = true;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800477
Jeff Sharkeydadccee2015-09-23 14:13:45 -0700478 // First nuke any existing partition table
479 std::vector<std::string> cmd;
480 cmd.push_back(kSgdiskPath);
481 cmd.push_back("--zap-all");
482 cmd.push_back(mDevPath);
483
484 // Zap sometimes returns an error when it actually succeeded, so
485 // just log as warning and keep rolling forward.
486 if ((res = ForkExecvp(cmd)) != 0) {
487 LOG(WARNING) << "Failed to zap; status " << res;
488 }
489
Jeff Sharkey68f1b8b2017-10-18 14:09:52 -0600490 // Now let's build the new MBR table. We heavily rely on sgdisk to
491 // force optimal alignment on the created partitions.
492 cmd.clear();
493 cmd.push_back(kSgdiskPath);
494 cmd.push_back("--new=0:0:-0");
495 cmd.push_back("--typecode=0:0c00");
496 cmd.push_back("--gpttombr=1");
497 cmd.push_back(mDevPath);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800498
Jeff Sharkey68f1b8b2017-10-18 14:09:52 -0600499 if ((res = ForkExecvp(cmd)) != 0) {
500 LOG(ERROR) << "Failed to partition; status " << res;
501 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800502 }
503
Jeff Sharkey68f1b8b2017-10-18 14:09:52 -0600504 return OK;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800505}
506
507status_t Disk::partitionPrivate() {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700508 return partitionMixed(0);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800509}
510
511status_t Disk::partitionMixed(int8_t ratio) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700512 int res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700513
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700514 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700515 mJustPartitioned = true;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700516
517 // First nuke any existing partition table
518 std::vector<std::string> cmd;
519 cmd.push_back(kSgdiskPath);
520 cmd.push_back("--zap-all");
521 cmd.push_back(mDevPath);
522
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700523 // Zap sometimes returns an error when it actually succeeded, so
524 // just log as warning and keep rolling forward.
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700525 if ((res = ForkExecvp(cmd)) != 0) {
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700526 LOG(WARNING) << "Failed to zap; status " << res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700527 }
528
529 // We've had some success above, so generate both the private partition
530 // GUID and encryption key and persist them.
531 std::string partGuidRaw;
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600532 if (GenerateRandomUuid(partGuidRaw) != OK) {
533 LOG(ERROR) << "Failed to generate GUID";
534 return -EIO;
535 }
536
Paul Crowleyb3d018a2020-02-12 11:04:05 -0800537 KeyBuffer key;
Paul Crowley886e5722020-02-07 12:51:56 -0800538 if (!generate_volume_key(&key)) {
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600539 LOG(ERROR) << "Failed to generate key";
Jeff Sharkey9c484982015-03-31 10:35:33 -0700540 return -EIO;
541 }
Paul Crowleyb3d018a2020-02-12 11:04:05 -0800542 std::string keyRaw(key.begin(), key.end());
Jeff Sharkey9c484982015-03-31 10:35:33 -0700543
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