blob: 01cd0c3629f533d5b9bec6e818619066b7e71ead [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 Sharkey3472e522017-10-06 18:02:53 -0600368
369 if (++it == split.end()) continue;
370 int i = 0;
371 if (!android::base::ParseInt(*it, &i, 1, maxMinors)) {
372 LOG(WARNING) << "Invalid partition number " << *it;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800373 continue;
374 }
375 dev_t partDevice = makedev(major(mDevice), minor(mDevice) + i);
376
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800377 if (table == Table::kMbr) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600378 if (++it == split.end()) continue;
379 int type = 0;
380 if (!android::base::ParseInt("0x" + *it, &type)) {
381 LOG(WARNING) << "Invalid partition type " << *it;
382 continue;
383 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800384
Jeff Sharkey3472e522017-10-06 18:02:53 -0600385 switch (type) {
Jeff Sharkey37ba1252018-01-19 10:55:18 +0900386 case 0x06: // FAT16
387 case 0x07: // HPFS/NTFS/exFAT
388 case 0x0b: // W95 FAT32 (LBA)
389 case 0x0c: // W95 FAT32 (LBA)
390 case 0x0e: // W95 FAT16 (LBA)
391 createPublicVolume(partDevice);
Xiuqin Wang22fa5342020-08-11 14:49:39 +0800392 foundParts = true;
Jeff Sharkey37ba1252018-01-19 10:55:18 +0900393 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);
Xiuqin Wang22fa5342020-08-11 14:49:39 +0800403 foundParts = true;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600404 } else if (android::base::EqualsIgnoreCase(typeGuid, kGptAndroidExpand)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700405 createPrivateVolume(partDevice, partGuid);
Xiuqin Wang22fa5342020-08-11 14:49:39 +0800406 foundParts = true;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800407 }
408 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800409 }
410 }
411
412 // Ugly last ditch effort, treat entire disk as partition
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700413 if (table == Table::kUnknown || !foundParts) {
414 LOG(WARNING) << mId << " has unknown partition table; trying entire device";
Jeff Sharkey63123c02015-06-26 11:16:14 -0700415
416 std::string fsType;
417 std::string unused;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600418 if (ReadMetadataUntrusted(mDevPath, &fsType, &unused, &unused) == OK) {
Jeff Sharkey63123c02015-06-26 11:16:14 -0700419 createPublicVolume(mDevice);
420 } else {
421 LOG(WARNING) << mId << " failed to identify, giving up";
422 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800423 }
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700424
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600425 auto listener = VolumeManager::Instance()->getListener();
426 if (listener) listener->onDiskScanned(getId());
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600427
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700428 mJustPartitioned = false;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800429 return OK;
430}
431
Risan82e90de2020-02-04 16:07:21 +0900432void Disk::initializePartition(std::shared_ptr<StubVolume> vol) {
433 CHECK(isStub());
434 CHECK(mVolumes.empty());
435 mVolumes.push_back(vol);
436}
437
Jeff Sharkey9c484982015-03-31 10:35:33 -0700438status_t Disk::unmountAll() {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700439 for (const auto& vol : mVolumes) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700440 vol->unmount();
441 }
442 return OK;
443}
444
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800445status_t Disk::partitionPublic() {
Jeff Sharkeydadccee2015-09-23 14:13:45 -0700446 int res;
447
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700448 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700449 mJustPartitioned = true;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800450
Jeff Sharkeydadccee2015-09-23 14:13:45 -0700451 // First nuke any existing partition table
452 std::vector<std::string> cmd;
453 cmd.push_back(kSgdiskPath);
454 cmd.push_back("--zap-all");
455 cmd.push_back(mDevPath);
456
457 // Zap sometimes returns an error when it actually succeeded, so
458 // just log as warning and keep rolling forward.
459 if ((res = ForkExecvp(cmd)) != 0) {
460 LOG(WARNING) << "Failed to zap; status " << res;
461 }
462
Jeff Sharkey68f1b8b2017-10-18 14:09:52 -0600463 // Now let's build the new MBR table. We heavily rely on sgdisk to
464 // force optimal alignment on the created partitions.
465 cmd.clear();
466 cmd.push_back(kSgdiskPath);
467 cmd.push_back("--new=0:0:-0");
468 cmd.push_back("--typecode=0:0c00");
469 cmd.push_back("--gpttombr=1");
470 cmd.push_back(mDevPath);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800471
Jeff Sharkey68f1b8b2017-10-18 14:09:52 -0600472 if ((res = ForkExecvp(cmd)) != 0) {
473 LOG(ERROR) << "Failed to partition; status " << res;
474 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800475 }
476
Jeff Sharkey68f1b8b2017-10-18 14:09:52 -0600477 return OK;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800478}
479
480status_t Disk::partitionPrivate() {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700481 return partitionMixed(0);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800482}
483
484status_t Disk::partitionMixed(int8_t ratio) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700485 int res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700486
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700487 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700488 mJustPartitioned = true;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700489
490 // First nuke any existing partition table
491 std::vector<std::string> cmd;
492 cmd.push_back(kSgdiskPath);
493 cmd.push_back("--zap-all");
494 cmd.push_back(mDevPath);
495
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700496 // Zap sometimes returns an error when it actually succeeded, so
497 // just log as warning and keep rolling forward.
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700498 if ((res = ForkExecvp(cmd)) != 0) {
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700499 LOG(WARNING) << "Failed to zap; status " << res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700500 }
501
502 // We've had some success above, so generate both the private partition
503 // GUID and encryption key and persist them.
504 std::string partGuidRaw;
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600505 if (GenerateRandomUuid(partGuidRaw) != OK) {
506 LOG(ERROR) << "Failed to generate GUID";
507 return -EIO;
508 }
509
Paul Crowleyb3d018a2020-02-12 11:04:05 -0800510 KeyBuffer key;
Paul Crowley886e5722020-02-07 12:51:56 -0800511 if (!generate_volume_key(&key)) {
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600512 LOG(ERROR) << "Failed to generate key";
Jeff Sharkey9c484982015-03-31 10:35:33 -0700513 return -EIO;
514 }
Paul Crowleyb3d018a2020-02-12 11:04:05 -0800515 std::string keyRaw(key.begin(), key.end());
Jeff Sharkey9c484982015-03-31 10:35:33 -0700516
517 std::string partGuid;
518 StrToHex(partGuidRaw, partGuid);
519
520 if (!WriteStringToFile(keyRaw, BuildKeyPath(partGuid))) {
521 LOG(ERROR) << "Failed to persist key";
522 return -EIO;
523 } else {
524 LOG(DEBUG) << "Persisted key for GUID " << partGuid;
525 }
526
527 // Now let's build the new GPT table. We heavily rely on sgdisk to
528 // force optimal alignment on the created partitions.
529 cmd.clear();
530 cmd.push_back(kSgdiskPath);
531
532 // If requested, create a public partition first. Mixed-mode partitioning
533 // like this is an experimental feature.
534 if (ratio > 0) {
535 if (ratio < 10 || ratio > 90) {
536 LOG(ERROR) << "Mixed partition ratio must be between 10-90%";
537 return -EINVAL;
538 }
539
540 uint64_t splitMb = ((mSize / 100) * ratio) / 1024 / 1024;
541 cmd.push_back(StringPrintf("--new=0:0:+%" PRId64 "M", splitMb));
542 cmd.push_back(StringPrintf("--typecode=0:%s", kGptBasicData));
543 cmd.push_back("--change-name=0:shared");
544 }
545
546 // Define a metadata partition which is designed for future use; there
547 // should only be one of these per physical device, even if there are
548 // multiple private volumes.
549 cmd.push_back("--new=0:0:+16M");
550 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidMeta));
551 cmd.push_back("--change-name=0:android_meta");
552
553 // Define a single private partition filling the rest of disk.
554 cmd.push_back("--new=0:0:-0");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700555 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidExpand));
Jeff Sharkey9c484982015-03-31 10:35:33 -0700556 cmd.push_back(StringPrintf("--partition-guid=0:%s", partGuid.c_str()));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700557 cmd.push_back("--change-name=0:android_expand");
Jeff Sharkey9c484982015-03-31 10:35:33 -0700558
559 cmd.push_back(mDevPath);
560
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700561 if ((res = ForkExecvp(cmd)) != 0) {
562 LOG(ERROR) << "Failed to partition; status " << res;
563 return res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700564 }
565
566 return OK;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800567}
568
569int Disk::getMaxMinors() {
570 // Figure out maximum partition devices supported
Yu Ning942d4e82016-01-08 17:36:47 +0800571 unsigned int majorId = major(mDevice);
572 switch (majorId) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700573 case kMajorBlockLoop: {
574 std::string tmp;
575 if (!ReadFileToString(kSysfsLoopMaxMinors, &tmp)) {
576 LOG(ERROR) << "Failed to read max minors";
577 return -errno;
578 }
579 return std::stoi(tmp);
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600580 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700581 // clang-format off
582 case kMajorBlockScsiA: case kMajorBlockScsiB: case kMajorBlockScsiC:
583 case kMajorBlockScsiD: case kMajorBlockScsiE: case kMajorBlockScsiF:
584 case kMajorBlockScsiG: case kMajorBlockScsiH: case kMajorBlockScsiI:
585 case kMajorBlockScsiJ: case kMajorBlockScsiK: case kMajorBlockScsiL:
586 case kMajorBlockScsiM: case kMajorBlockScsiN: case kMajorBlockScsiO:
587 case kMajorBlockScsiP: {
588 // clang-format on
589 // Per Documentation/devices.txt this is static
Yu Ning942d4e82016-01-08 17:36:47 +0800590 return 15;
591 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700592 case kMajorBlockMmc: {
593 // Per Documentation/devices.txt this is dynamic
594 std::string tmp;
595 if (!ReadFileToString(kSysfsMmcMaxMinors, &tmp) &&
596 !ReadFileToString(kSysfsMmcMaxMinorsDeprecated, &tmp)) {
597 LOG(ERROR) << "Failed to read max minors";
598 return -errno;
599 }
600 return std::stoi(tmp);
Dmitry Shmidt06dc6e52018-05-11 17:22:42 -0700601 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700602 default: {
Alistair Delvaff1fc9b2020-05-14 16:35:03 -0700603 if (IsVirtioBlkDevice(majorId)) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700604 // drivers/block/virtio_blk.c has "#define PART_BITS 4", so max is
605 // 2^4 - 1 = 15
606 return 15;
607 }
608 if (isNvmeBlkDevice(majorId, mSysPath)) {
609 // despite kernel nvme driver supports up to 1M minors,
610 // #define NVME_MINORS (1U << MINORBITS)
611 // sgdisk can not support more than 127 partitions, due to
612 // #define MAX_MBR_PARTS 128
613 return 127;
614 }
615 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800616 }
617
Yu Ning942d4e82016-01-08 17:36:47 +0800618 LOG(ERROR) << "Unsupported block major type " << majorId;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800619 return -ENOTSUP;
620}
621
622} // namespace vold
623} // namespace android