blob: 4df4e9dd2b5f445cad444b05426fc81e5df8d818 [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;
Dmitry Shmidt06dc6e52018-05-11 17:22:42 -070076static const unsigned int kMajorBlockDynamicMin = 234;
77static const unsigned int kMajorBlockDynamicMax = 512;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080078
79static const char* kGptBasicData = "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7";
80static const char* kGptAndroidMeta = "19A710A2-B3CA-11E4-B026-10604B889DCF";
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070081static const char* kGptAndroidExpand = "193D1EA4-B3CA-11E4-B075-10604B889DCF";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080082
83enum class Table {
84 kUnknown,
85 kMbr,
86 kGpt,
87};
88
Dmitry Shmidt06dc6e52018-05-11 17:22:42 -070089static bool isNvmeBlkDevice(unsigned int major, const std::string& sysPath) {
Paul Crowley14c8c072018-09-18 13:30:21 -070090 return sysPath.find("nvme") != std::string::npos && major >= kMajorBlockDynamicMin &&
91 major <= kMajorBlockDynamicMax;
Dmitry Shmidt06dc6e52018-05-11 17:22:42 -070092}
93
Paul Crowley14c8c072018-09-18 13:30:21 -070094Disk::Disk(const std::string& eventPath, dev_t device, const std::string& nickname, int flags)
95 : mDevice(device),
96 mSize(-1),
97 mNickname(nickname),
98 mFlags(flags),
99 mCreated(false),
100 mJustPartitioned(false) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700101 mId = StringPrintf("disk:%u,%u", major(device), minor(device));
102 mEventPath = eventPath;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800103 mSysPath = StringPrintf("/sys/%s", eventPath.c_str());
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700104 mDevPath = StringPrintf("/dev/block/vold/%s", mId.c_str());
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800105 CreateDeviceNode(mDevPath, mDevice);
106}
107
108Disk::~Disk() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700109 CHECK(!mCreated);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800110 DestroyDeviceNode(mDevPath);
111}
112
113std::shared_ptr<VolumeBase> Disk::findVolume(const std::string& id) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700114 for (auto vol : mVolumes) {
115 if (vol->getId() == id) {
116 return vol;
117 }
118 auto stackedVol = vol->findVolume(id);
119 if (stackedVol != nullptr) {
120 return stackedVol;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800121 }
122 }
123 return nullptr;
124}
125
Greg Kaiser2bc201e2018-12-18 08:42:08 -0800126void Disk::listVolumes(VolumeBase::Type type, std::list<std::string>& list) const {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700127 for (const auto& vol : mVolumes) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700128 if (vol->getType() == type) {
129 list.push_back(vol->getId());
130 }
131 // TODO: consider looking at stacked volumes
132 }
133}
134
Martijn Coenen0a7e9922020-01-24 16:17:32 +0100135std::vector<std::shared_ptr<VolumeBase>> Disk::getVolumes() const {
136 std::vector<std::shared_ptr<VolumeBase>> vols;
137 for (const auto& vol : mVolumes) {
138 vols.push_back(vol);
139 auto stackedVolumes = vol->getVolumes();
140 vols.insert(vols.end(), stackedVolumes.begin(), stackedVolumes.end());
141 }
142
143 return vols;
144}
145
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700146status_t Disk::create() {
147 CHECK(!mCreated);
148 mCreated = true;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600149
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600150 auto listener = VolumeManager::Instance()->getListener();
151 if (listener) listener->onDiskCreated(getId(), mFlags);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600152
Risan82e90de2020-02-04 16:07:21 +0900153 if (isStub()) {
154 createStubVolume();
155 return OK;
156 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700157 readMetadata();
158 readPartitions();
159 return OK;
160}
161
162status_t Disk::destroy() {
163 CHECK(mCreated);
164 destroyAllVolumes();
165 mCreated = false;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600166
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600167 auto listener = VolumeManager::Instance()->getListener();
168 if (listener) listener->onDiskDestroyed(getId());
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600169
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700170 return OK;
171}
172
173void Disk::createPublicVolume(dev_t device) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700174 auto vol = std::shared_ptr<VolumeBase>(new PublicVolume(device));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700175 if (mJustPartitioned) {
176 LOG(DEBUG) << "Device just partitioned; silently formatting";
177 vol->setSilent(true);
178 vol->create();
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700179 vol->format("auto");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700180 vol->destroy();
181 vol->setSilent(false);
182 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700183
Jeff Sharkey9c484982015-03-31 10:35:33 -0700184 mVolumes.push_back(vol);
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700185 vol->setDiskId(getId());
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700186 vol->create();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700187}
188
Jeff Sharkey9c484982015-03-31 10:35:33 -0700189void Disk::createPrivateVolume(dev_t device, const std::string& partGuid) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700190 std::string normalizedGuid;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700191 if (NormalizeHex(partGuid, normalizedGuid)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700192 LOG(WARNING) << "Invalid GUID " << partGuid;
193 return;
194 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700195
196 std::string keyRaw;
197 if (!ReadFileToString(BuildKeyPath(normalizedGuid), &keyRaw)) {
198 PLOG(ERROR) << "Failed to load key for GUID " << normalizedGuid;
199 return;
200 }
201
202 LOG(DEBUG) << "Found key for GUID " << normalizedGuid;
203
Paul Crowley3d98f5d2020-02-07 11:49:09 -0800204 auto keyBuffer = KeyBuffer(keyRaw.begin(), keyRaw.end());
205 auto vol = std::shared_ptr<VolumeBase>(new PrivateVolume(device, keyBuffer));
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 Sharkey9c484982015-03-31 10:35:33 -0700214
215 mVolumes.push_back(vol);
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700216 vol->setDiskId(getId());
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700217 vol->setPartGuid(partGuid);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700218 vol->create();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700219}
220
Risan82e90de2020-02-04 16:07:21 +0900221void Disk::createStubVolume() {
222 CHECK(mVolumes.size() == 1);
223 auto listener = VolumeManager::Instance()->getListener();
224 if (listener) listener->onDiskMetadataChanged(getId(), mSize, mLabel, mSysPath);
225 if (listener) listener->onDiskScanned(getId());
226 mVolumes[0]->setDiskId(getId());
227 mVolumes[0]->create();
228}
229
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700230void Disk::destroyAllVolumes() {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700231 for (const auto& vol : mVolumes) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700232 vol->destroy();
233 }
234 mVolumes.clear();
235}
236
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800237status_t Disk::readMetadata() {
238 mSize = -1;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700239 mLabel.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800240
Oleksiy Avramchenko625dc782018-05-23 10:50:46 +0200241 if (GetBlockDevSize(mDevPath, &mSize) != OK) {
242 mSize = -1;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800243 }
244
Yu Ning942d4e82016-01-08 17:36:47 +0800245 unsigned int majorId = major(mDevice);
246 switch (majorId) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700247 case kMajorBlockLoop: {
Yu Ning942d4e82016-01-08 17:36:47 +0800248 mLabel = "Virtual";
249 break;
250 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700251 // clang-format off
252 case kMajorBlockScsiA: case kMajorBlockScsiB: case kMajorBlockScsiC:
253 case kMajorBlockScsiD: case kMajorBlockScsiE: case kMajorBlockScsiF:
254 case kMajorBlockScsiG: case kMajorBlockScsiH: case kMajorBlockScsiI:
255 case kMajorBlockScsiJ: case kMajorBlockScsiK: case kMajorBlockScsiL:
256 case kMajorBlockScsiM: case kMajorBlockScsiN: case kMajorBlockScsiO:
257 case kMajorBlockScsiP: {
258 // clang-format on
259 std::string path(mSysPath + "/device/vendor");
Dmitry Shmidt06dc6e52018-05-11 17:22:42 -0700260 std::string tmp;
261 if (!ReadFileToString(path, &tmp)) {
262 PLOG(WARNING) << "Failed to read vendor from " << path;
263 return -errno;
264 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700265 tmp = android::base::Trim(tmp);
Dmitry Shmidt06dc6e52018-05-11 17:22:42 -0700266 mLabel = tmp;
267 break;
268 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700269 case kMajorBlockMmc: {
270 std::string path(mSysPath + "/device/manfid");
271 std::string tmp;
272 if (!ReadFileToString(path, &tmp)) {
273 PLOG(WARNING) << "Failed to read manufacturer from " << path;
274 return -errno;
275 }
276 tmp = android::base::Trim(tmp);
277 int64_t manfid;
278 if (!android::base::ParseInt(tmp, &manfid)) {
279 PLOG(WARNING) << "Failed to parse manufacturer " << tmp;
280 return -EINVAL;
281 }
282 // Our goal here is to give the user a meaningful label, ideally
283 // matching whatever is silk-screened on the card. To reduce
284 // user confusion, this list doesn't contain white-label manfid.
285 switch (manfid) {
286 // clang-format off
287 case 0x000003: mLabel = "SanDisk"; break;
288 case 0x00001b: mLabel = "Samsung"; break;
289 case 0x000028: mLabel = "Lexar"; break;
290 case 0x000074: mLabel = "Transcend"; break;
291 // clang-format on
292 }
293 break;
294 }
295 default: {
Alistair Delvaff1fc9b2020-05-14 16:35:03 -0700296 if (IsVirtioBlkDevice(majorId)) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700297 LOG(DEBUG) << "Recognized experimental block major ID " << majorId
298 << " as virtio-blk (emulator's virtual SD card device)";
299 mLabel = "Virtual";
300 break;
301 }
302 if (isNvmeBlkDevice(majorId, mSysPath)) {
303 std::string path(mSysPath + "/device/model");
304 std::string tmp;
305 if (!ReadFileToString(path, &tmp)) {
306 PLOG(WARNING) << "Failed to read vendor from " << path;
307 return -errno;
308 }
309 mLabel = tmp;
310 break;
311 }
312 LOG(WARNING) << "Unsupported block major type " << majorId;
313 return -ENOTSUP;
314 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800315 }
316
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600317 auto listener = VolumeManager::Instance()->getListener();
Paul Crowley14c8c072018-09-18 13:30:21 -0700318 if (listener) listener->onDiskMetadataChanged(getId(), mSize, mLabel, mSysPath);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600319
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800320 return OK;
321}
322
323status_t Disk::readPartitions() {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600324 int maxMinors = getMaxMinors();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800325 if (maxMinors < 0) {
326 return -ENOTSUP;
327 }
328
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700329 destroyAllVolumes();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800330
331 // Parse partition table
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700332
333 std::vector<std::string> cmd;
334 cmd.push_back(kSgdiskPath);
335 cmd.push_back("--android-dump");
336 cmd.push_back(mDevPath);
337
338 std::vector<std::string> output;
Paul Crowleyde2d6202018-11-30 11:43:47 -0800339 status_t res = ForkExecvp(cmd, &output);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700340 if (res != OK) {
341 LOG(WARNING) << "sgdisk failed to scan " << mDevPath;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600342
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600343 auto listener = VolumeManager::Instance()->getListener();
344 if (listener) listener->onDiskScanned(getId());
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600345
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700346 mJustPartitioned = false;
347 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800348 }
349
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800350 Table table = Table::kUnknown;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700351 bool foundParts = false;
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700352 for (const auto& line : output) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600353 auto split = android::base::Split(line, kSgdiskToken);
354 auto it = split.begin();
355 if (it == split.end()) continue;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700356
Jeff Sharkey3472e522017-10-06 18:02:53 -0600357 if (*it == "DISK") {
358 if (++it == split.end()) continue;
359 if (*it == "mbr") {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800360 table = Table::kMbr;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600361 } else if (*it == "gpt") {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800362 table = Table::kGpt;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600363 } else {
364 LOG(WARNING) << "Invalid partition table " << *it;
365 continue;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800366 }
Jeff Sharkey3472e522017-10-06 18:02:53 -0600367 } else if (*it == "PART") {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700368 foundParts = true;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600369
370 if (++it == split.end()) continue;
371 int i = 0;
372 if (!android::base::ParseInt(*it, &i, 1, maxMinors)) {
373 LOG(WARNING) << "Invalid partition number " << *it;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800374 continue;
375 }
376 dev_t partDevice = makedev(major(mDevice), minor(mDevice) + i);
377
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800378 if (table == Table::kMbr) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600379 if (++it == split.end()) continue;
380 int type = 0;
381 if (!android::base::ParseInt("0x" + *it, &type)) {
382 LOG(WARNING) << "Invalid partition type " << *it;
383 continue;
384 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800385
Jeff Sharkey3472e522017-10-06 18:02:53 -0600386 switch (type) {
Jeff Sharkey37ba1252018-01-19 10:55:18 +0900387 case 0x06: // FAT16
388 case 0x07: // HPFS/NTFS/exFAT
389 case 0x0b: // W95 FAT32 (LBA)
390 case 0x0c: // W95 FAT32 (LBA)
391 case 0x0e: // W95 FAT16 (LBA)
392 createPublicVolume(partDevice);
393 break;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800394 }
395 } else if (table == Table::kGpt) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600396 if (++it == split.end()) continue;
397 auto typeGuid = *it;
398 if (++it == split.end()) continue;
399 auto partGuid = *it;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800400
Jeff Sharkey3472e522017-10-06 18:02:53 -0600401 if (android::base::EqualsIgnoreCase(typeGuid, kGptBasicData)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700402 createPublicVolume(partDevice);
Jeff Sharkey3472e522017-10-06 18:02:53 -0600403 } else if (android::base::EqualsIgnoreCase(typeGuid, kGptAndroidExpand)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700404 createPrivateVolume(partDevice, partGuid);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800405 }
406 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800407 }
408 }
409
410 // Ugly last ditch effort, treat entire disk as partition
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700411 if (table == Table::kUnknown || !foundParts) {
412 LOG(WARNING) << mId << " has unknown partition table; trying entire device";
Jeff Sharkey63123c02015-06-26 11:16:14 -0700413
414 std::string fsType;
415 std::string unused;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600416 if (ReadMetadataUntrusted(mDevPath, &fsType, &unused, &unused) == OK) {
Jeff Sharkey63123c02015-06-26 11:16:14 -0700417 createPublicVolume(mDevice);
418 } else {
419 LOG(WARNING) << mId << " failed to identify, giving up";
420 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800421 }
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700422
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600423 auto listener = VolumeManager::Instance()->getListener();
424 if (listener) listener->onDiskScanned(getId());
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600425
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700426 mJustPartitioned = false;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800427 return OK;
428}
429
Risan82e90de2020-02-04 16:07:21 +0900430void Disk::initializePartition(std::shared_ptr<StubVolume> vol) {
431 CHECK(isStub());
432 CHECK(mVolumes.empty());
433 mVolumes.push_back(vol);
434}
435
Jeff Sharkey9c484982015-03-31 10:35:33 -0700436status_t Disk::unmountAll() {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700437 for (const auto& vol : mVolumes) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700438 vol->unmount();
439 }
440 return OK;
441}
442
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800443status_t Disk::partitionPublic() {
Jeff Sharkeydadccee2015-09-23 14:13:45 -0700444 int res;
445
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700446 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700447 mJustPartitioned = true;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800448
Jeff Sharkeydadccee2015-09-23 14:13:45 -0700449 // First nuke any existing partition table
450 std::vector<std::string> cmd;
451 cmd.push_back(kSgdiskPath);
452 cmd.push_back("--zap-all");
453 cmd.push_back(mDevPath);
454
455 // Zap sometimes returns an error when it actually succeeded, so
456 // just log as warning and keep rolling forward.
457 if ((res = ForkExecvp(cmd)) != 0) {
458 LOG(WARNING) << "Failed to zap; status " << res;
459 }
460
Jeff Sharkey68f1b8b2017-10-18 14:09:52 -0600461 // Now let's build the new MBR table. We heavily rely on sgdisk to
462 // force optimal alignment on the created partitions.
463 cmd.clear();
464 cmd.push_back(kSgdiskPath);
465 cmd.push_back("--new=0:0:-0");
466 cmd.push_back("--typecode=0:0c00");
467 cmd.push_back("--gpttombr=1");
468 cmd.push_back(mDevPath);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800469
Jeff Sharkey68f1b8b2017-10-18 14:09:52 -0600470 if ((res = ForkExecvp(cmd)) != 0) {
471 LOG(ERROR) << "Failed to partition; status " << res;
472 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800473 }
474
Jeff Sharkey68f1b8b2017-10-18 14:09:52 -0600475 return OK;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800476}
477
478status_t Disk::partitionPrivate() {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700479 return partitionMixed(0);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800480}
481
482status_t Disk::partitionMixed(int8_t ratio) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700483 int res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700484
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700485 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700486 mJustPartitioned = true;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700487
488 // First nuke any existing partition table
489 std::vector<std::string> cmd;
490 cmd.push_back(kSgdiskPath);
491 cmd.push_back("--zap-all");
492 cmd.push_back(mDevPath);
493
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700494 // Zap sometimes returns an error when it actually succeeded, so
495 // just log as warning and keep rolling forward.
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700496 if ((res = ForkExecvp(cmd)) != 0) {
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700497 LOG(WARNING) << "Failed to zap; status " << res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700498 }
499
500 // We've had some success above, so generate both the private partition
501 // GUID and encryption key and persist them.
502 std::string partGuidRaw;
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600503 if (GenerateRandomUuid(partGuidRaw) != OK) {
504 LOG(ERROR) << "Failed to generate GUID";
505 return -EIO;
506 }
507
Paul Crowleyb3d018a2020-02-12 11:04:05 -0800508 KeyBuffer key;
Paul Crowley886e5722020-02-07 12:51:56 -0800509 if (!generate_volume_key(&key)) {
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600510 LOG(ERROR) << "Failed to generate key";
Jeff Sharkey9c484982015-03-31 10:35:33 -0700511 return -EIO;
512 }
Paul Crowleyb3d018a2020-02-12 11:04:05 -0800513 std::string keyRaw(key.begin(), key.end());
Jeff Sharkey9c484982015-03-31 10:35:33 -0700514
515 std::string partGuid;
516 StrToHex(partGuidRaw, partGuid);
517
518 if (!WriteStringToFile(keyRaw, BuildKeyPath(partGuid))) {
519 LOG(ERROR) << "Failed to persist key";
520 return -EIO;
521 } else {
522 LOG(DEBUG) << "Persisted key for GUID " << partGuid;
523 }
524
525 // Now let's build the new GPT table. We heavily rely on sgdisk to
526 // force optimal alignment on the created partitions.
527 cmd.clear();
528 cmd.push_back(kSgdiskPath);
529
530 // If requested, create a public partition first. Mixed-mode partitioning
531 // like this is an experimental feature.
532 if (ratio > 0) {
533 if (ratio < 10 || ratio > 90) {
534 LOG(ERROR) << "Mixed partition ratio must be between 10-90%";
535 return -EINVAL;
536 }
537
538 uint64_t splitMb = ((mSize / 100) * ratio) / 1024 / 1024;
539 cmd.push_back(StringPrintf("--new=0:0:+%" PRId64 "M", splitMb));
540 cmd.push_back(StringPrintf("--typecode=0:%s", kGptBasicData));
541 cmd.push_back("--change-name=0:shared");
542 }
543
544 // Define a metadata partition which is designed for future use; there
545 // should only be one of these per physical device, even if there are
546 // multiple private volumes.
547 cmd.push_back("--new=0:0:+16M");
548 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidMeta));
549 cmd.push_back("--change-name=0:android_meta");
550
551 // Define a single private partition filling the rest of disk.
552 cmd.push_back("--new=0:0:-0");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700553 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidExpand));
Jeff Sharkey9c484982015-03-31 10:35:33 -0700554 cmd.push_back(StringPrintf("--partition-guid=0:%s", partGuid.c_str()));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700555 cmd.push_back("--change-name=0:android_expand");
Jeff Sharkey9c484982015-03-31 10:35:33 -0700556
557 cmd.push_back(mDevPath);
558
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700559 if ((res = ForkExecvp(cmd)) != 0) {
560 LOG(ERROR) << "Failed to partition; status " << res;
561 return res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700562 }
563
564 return OK;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800565}
566
567int Disk::getMaxMinors() {
568 // Figure out maximum partition devices supported
Yu Ning942d4e82016-01-08 17:36:47 +0800569 unsigned int majorId = major(mDevice);
570 switch (majorId) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700571 case kMajorBlockLoop: {
572 std::string tmp;
573 if (!ReadFileToString(kSysfsLoopMaxMinors, &tmp)) {
574 LOG(ERROR) << "Failed to read max minors";
575 return -errno;
576 }
577 return std::stoi(tmp);
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600578 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700579 // clang-format off
580 case kMajorBlockScsiA: case kMajorBlockScsiB: case kMajorBlockScsiC:
581 case kMajorBlockScsiD: case kMajorBlockScsiE: case kMajorBlockScsiF:
582 case kMajorBlockScsiG: case kMajorBlockScsiH: case kMajorBlockScsiI:
583 case kMajorBlockScsiJ: case kMajorBlockScsiK: case kMajorBlockScsiL:
584 case kMajorBlockScsiM: case kMajorBlockScsiN: case kMajorBlockScsiO:
585 case kMajorBlockScsiP: {
586 // clang-format on
587 // Per Documentation/devices.txt this is static
Yu Ning942d4e82016-01-08 17:36:47 +0800588 return 15;
589 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700590 case kMajorBlockMmc: {
591 // Per Documentation/devices.txt this is dynamic
592 std::string tmp;
593 if (!ReadFileToString(kSysfsMmcMaxMinors, &tmp) &&
594 !ReadFileToString(kSysfsMmcMaxMinorsDeprecated, &tmp)) {
595 LOG(ERROR) << "Failed to read max minors";
596 return -errno;
597 }
598 return std::stoi(tmp);
Dmitry Shmidt06dc6e52018-05-11 17:22:42 -0700599 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700600 default: {
Alistair Delvaff1fc9b2020-05-14 16:35:03 -0700601 if (IsVirtioBlkDevice(majorId)) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700602 // drivers/block/virtio_blk.c has "#define PART_BITS 4", so max is
603 // 2^4 - 1 = 15
604 return 15;
605 }
606 if (isNvmeBlkDevice(majorId, mSysPath)) {
607 // despite kernel nvme driver supports up to 1M minors,
608 // #define NVME_MINORS (1U << MINORBITS)
609 // sgdisk can not support more than 127 partitions, due to
610 // #define MAX_MBR_PARTS 128
611 return 127;
612 }
613 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800614 }
615
Yu Ning942d4e82016-01-08 17:36:47 +0800616 LOG(ERROR) << "Unsupported block major type " << majorId;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800617 return -ENOTSUP;
618}
619
620} // namespace vold
621} // namespace android