blob: 42dbd4f7bc159c946b1bc5af42a93abd9a618474 [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),
Scott Mertzad5b4f92016-03-31 13:09:56 -0700100 mJustPartitioned(false),
101 mSkipChange(false) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700102 mId = StringPrintf("disk:%u,%u", major(device), minor(device));
103 mEventPath = eventPath;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800104 mSysPath = StringPrintf("/sys/%s", eventPath.c_str());
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700105 mDevPath = StringPrintf("/dev/block/vold/%s", mId.c_str());
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800106 CreateDeviceNode(mDevPath, mDevice);
107}
108
109Disk::~Disk() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700110 CHECK(!mCreated);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800111 DestroyDeviceNode(mDevPath);
112}
113
114std::shared_ptr<VolumeBase> Disk::findVolume(const std::string& id) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700115 for (auto vol : mVolumes) {
116 if (vol->getId() == id) {
117 return vol;
118 }
119 auto stackedVol = vol->findVolume(id);
120 if (stackedVol != nullptr) {
121 return stackedVol;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800122 }
123 }
124 return nullptr;
125}
126
Greg Kaiser2bc201e2018-12-18 08:42:08 -0800127void Disk::listVolumes(VolumeBase::Type type, std::list<std::string>& list) const {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700128 for (const auto& vol : mVolumes) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700129 if (vol->getType() == type) {
130 list.push_back(vol->getId());
131 }
132 // TODO: consider looking at stacked volumes
133 }
134}
135
Martijn Coenen0a7e9922020-01-24 16:17:32 +0100136std::vector<std::shared_ptr<VolumeBase>> Disk::getVolumes() const {
137 std::vector<std::shared_ptr<VolumeBase>> vols;
138 for (const auto& vol : mVolumes) {
139 vols.push_back(vol);
140 auto stackedVolumes = vol->getVolumes();
141 vols.insert(vols.end(), stackedVolumes.begin(), stackedVolumes.end());
142 }
143
144 return vols;
145}
146
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700147status_t Disk::create() {
148 CHECK(!mCreated);
149 mCreated = true;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600150
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600151 auto listener = VolumeManager::Instance()->getListener();
152 if (listener) listener->onDiskCreated(getId(), mFlags);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600153
Risan82e90de2020-02-04 16:07:21 +0900154 if (isStub()) {
155 createStubVolume();
156 return OK;
157 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700158 readMetadata();
159 readPartitions();
160 return OK;
161}
162
163status_t Disk::destroy() {
164 CHECK(mCreated);
165 destroyAllVolumes();
166 mCreated = false;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600167
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600168 auto listener = VolumeManager::Instance()->getListener();
169 if (listener) listener->onDiskDestroyed(getId());
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600170
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700171 return OK;
172}
173
Alexander Martinz72268882023-09-28 14:43:16 +0200174void Disk::createPublicVolume(dev_t device) {
175 auto vol = std::shared_ptr<VolumeBase>(new PublicVolume(device));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700176 if (mJustPartitioned) {
177 LOG(DEBUG) << "Device just partitioned; silently formatting";
178 vol->setSilent(true);
179 vol->create();
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700180 vol->format("auto");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700181 vol->destroy();
182 vol->setSilent(false);
183 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700184
Jeff Sharkey9c484982015-03-31 10:35:33 -0700185 mVolumes.push_back(vol);
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700186 vol->setDiskId(getId());
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700187 vol->create();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700188}
189
Jeff Sharkey9c484982015-03-31 10:35:33 -0700190void Disk::createPrivateVolume(dev_t device, const std::string& partGuid) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700191 std::string normalizedGuid;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700192 if (NormalizeHex(partGuid, normalizedGuid)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700193 LOG(WARNING) << "Invalid GUID " << partGuid;
194 return;
195 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700196
197 std::string keyRaw;
198 if (!ReadFileToString(BuildKeyPath(normalizedGuid), &keyRaw)) {
199 PLOG(ERROR) << "Failed to load key for GUID " << normalizedGuid;
200 return;
201 }
202
203 LOG(DEBUG) << "Found key for GUID " << normalizedGuid;
204
Paul Crowley3d98f5d2020-02-07 11:49:09 -0800205 auto keyBuffer = KeyBuffer(keyRaw.begin(), keyRaw.end());
206 auto vol = std::shared_ptr<VolumeBase>(new PrivateVolume(device, keyBuffer));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700207 if (mJustPartitioned) {
208 LOG(DEBUG) << "Device just partitioned; silently formatting";
209 vol->setSilent(true);
210 vol->create();
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700211 vol->format("auto");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700212 vol->destroy();
213 vol->setSilent(false);
214 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700215
216 mVolumes.push_back(vol);
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700217 vol->setDiskId(getId());
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700218 vol->setPartGuid(partGuid);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700219 vol->create();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700220}
221
Risan82e90de2020-02-04 16:07:21 +0900222void Disk::createStubVolume() {
223 CHECK(mVolumes.size() == 1);
224 auto listener = VolumeManager::Instance()->getListener();
225 if (listener) listener->onDiskMetadataChanged(getId(), mSize, mLabel, mSysPath);
226 if (listener) listener->onDiskScanned(getId());
227 mVolumes[0]->setDiskId(getId());
228 mVolumes[0]->create();
229}
230
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700231void Disk::destroyAllVolumes() {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700232 for (const auto& vol : mVolumes) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700233 vol->destroy();
234 }
235 mVolumes.clear();
236}
237
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800238status_t Disk::readMetadata() {
Scott Mertzad5b4f92016-03-31 13:09:56 -0700239
240 if (mSkipChange) {
241 return OK;
242 }
243
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800244 mSize = -1;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700245 mLabel.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800246
Oleksiy Avramchenko625dc782018-05-23 10:50:46 +0200247 if (GetBlockDevSize(mDevPath, &mSize) != OK) {
248 mSize = -1;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800249 }
250
Yu Ning942d4e82016-01-08 17:36:47 +0800251 unsigned int majorId = major(mDevice);
252 switch (majorId) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700253 case kMajorBlockLoop: {
Yu Ning942d4e82016-01-08 17:36:47 +0800254 mLabel = "Virtual";
255 break;
256 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700257 // clang-format off
258 case kMajorBlockScsiA: case kMajorBlockScsiB: case kMajorBlockScsiC:
259 case kMajorBlockScsiD: case kMajorBlockScsiE: case kMajorBlockScsiF:
260 case kMajorBlockScsiG: case kMajorBlockScsiH: case kMajorBlockScsiI:
261 case kMajorBlockScsiJ: case kMajorBlockScsiK: case kMajorBlockScsiL:
262 case kMajorBlockScsiM: case kMajorBlockScsiN: case kMajorBlockScsiO:
263 case kMajorBlockScsiP: {
264 // clang-format on
265 std::string path(mSysPath + "/device/vendor");
Dmitry Shmidt06dc6e52018-05-11 17:22:42 -0700266 std::string tmp;
267 if (!ReadFileToString(path, &tmp)) {
268 PLOG(WARNING) << "Failed to read vendor from " << path;
269 return -errno;
270 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700271 tmp = android::base::Trim(tmp);
Dmitry Shmidt06dc6e52018-05-11 17:22:42 -0700272 mLabel = tmp;
273 break;
274 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700275 case kMajorBlockMmc: {
276 std::string path(mSysPath + "/device/manfid");
277 std::string tmp;
278 if (!ReadFileToString(path, &tmp)) {
279 PLOG(WARNING) << "Failed to read manufacturer from " << path;
280 return -errno;
281 }
282 tmp = android::base::Trim(tmp);
283 int64_t manfid;
284 if (!android::base::ParseInt(tmp, &manfid)) {
285 PLOG(WARNING) << "Failed to parse manufacturer " << tmp;
286 return -EINVAL;
287 }
288 // Our goal here is to give the user a meaningful label, ideally
289 // matching whatever is silk-screened on the card. To reduce
290 // user confusion, this list doesn't contain white-label manfid.
291 switch (manfid) {
292 // clang-format off
293 case 0x000003: mLabel = "SanDisk"; break;
294 case 0x00001b: mLabel = "Samsung"; break;
295 case 0x000028: mLabel = "Lexar"; break;
296 case 0x000074: mLabel = "Transcend"; break;
297 // clang-format on
298 }
299 break;
300 }
301 default: {
Alistair Delvaff1fc9b2020-05-14 16:35:03 -0700302 if (IsVirtioBlkDevice(majorId)) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700303 LOG(DEBUG) << "Recognized experimental block major ID " << majorId
304 << " as virtio-blk (emulator's virtual SD card device)";
305 mLabel = "Virtual";
306 break;
307 }
308 if (isNvmeBlkDevice(majorId, mSysPath)) {
309 std::string path(mSysPath + "/device/model");
310 std::string tmp;
311 if (!ReadFileToString(path, &tmp)) {
312 PLOG(WARNING) << "Failed to read vendor from " << path;
313 return -errno;
314 }
315 mLabel = tmp;
316 break;
317 }
318 LOG(WARNING) << "Unsupported block major type " << majorId;
319 return -ENOTSUP;
320 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800321 }
322
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600323 auto listener = VolumeManager::Instance()->getListener();
Paul Crowley14c8c072018-09-18 13:30:21 -0700324 if (listener) listener->onDiskMetadataChanged(getId(), mSize, mLabel, mSysPath);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600325
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800326 return OK;
327}
328
329status_t Disk::readPartitions() {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600330 int maxMinors = getMaxMinors();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800331 if (maxMinors < 0) {
332 return -ENOTSUP;
333 }
334
Scott Mertzad5b4f92016-03-31 13:09:56 -0700335 if (mSkipChange) {
336 mSkipChange = false;
337 LOG(INFO) << "Skip first change";
338 return OK;
339 }
340
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700341 destroyAllVolumes();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800342
343 // Parse partition table
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700344
345 std::vector<std::string> cmd;
346 cmd.push_back(kSgdiskPath);
347 cmd.push_back("--android-dump");
348 cmd.push_back(mDevPath);
349
350 std::vector<std::string> output;
Paul Crowleyde2d6202018-11-30 11:43:47 -0800351 status_t res = ForkExecvp(cmd, &output);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700352 if (res != OK) {
353 LOG(WARNING) << "sgdisk failed to scan " << mDevPath;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600354
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600355 auto listener = VolumeManager::Instance()->getListener();
356 if (listener) listener->onDiskScanned(getId());
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600357
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700358 mJustPartitioned = false;
359 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800360 }
361
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800362 Table table = Table::kUnknown;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700363 bool foundParts = false;
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700364 for (const auto& line : output) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600365 auto split = android::base::Split(line, kSgdiskToken);
366 auto it = split.begin();
367 if (it == split.end()) continue;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700368
Jeff Sharkey3472e522017-10-06 18:02:53 -0600369 if (*it == "DISK") {
370 if (++it == split.end()) continue;
371 if (*it == "mbr") {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800372 table = Table::kMbr;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600373 } else if (*it == "gpt") {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800374 table = Table::kGpt;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600375 } else {
376 LOG(WARNING) << "Invalid partition table " << *it;
377 continue;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800378 }
Jeff Sharkey3472e522017-10-06 18:02:53 -0600379 } else if (*it == "PART") {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700380 foundParts = true;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600381
382 if (++it == split.end()) continue;
383 int i = 0;
384 if (!android::base::ParseInt(*it, &i, 1, maxMinors)) {
385 LOG(WARNING) << "Invalid partition number " << *it;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800386 continue;
387 }
388 dev_t partDevice = makedev(major(mDevice), minor(mDevice) + i);
389
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800390 if (table == Table::kMbr) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600391 if (++it == split.end()) continue;
392 int type = 0;
393 if (!android::base::ParseInt("0x" + *it, &type)) {
394 LOG(WARNING) << "Invalid partition type " << *it;
395 continue;
396 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800397
Jeff Sharkey3472e522017-10-06 18:02:53 -0600398 switch (type) {
Jeff Sharkey37ba1252018-01-19 10:55:18 +0900399 case 0x06: // FAT16
400 case 0x07: // HPFS/NTFS/exFAT
401 case 0x0b: // W95 FAT32 (LBA)
402 case 0x0c: // W95 FAT32 (LBA)
403 case 0x0e: // W95 FAT16 (LBA)
404 createPublicVolume(partDevice);
405 break;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800406 }
407 } else if (table == Table::kGpt) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600408 if (++it == split.end()) continue;
409 auto typeGuid = *it;
410 if (++it == split.end()) continue;
411 auto partGuid = *it;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800412
Alexander Martinz72268882023-09-28 14:43:16 +0200413 if (android::base::EqualsIgnoreCase(typeGuid, kGptBasicData)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700414 createPublicVolume(partDevice);
Jeff Sharkey3472e522017-10-06 18:02:53 -0600415 } else if (android::base::EqualsIgnoreCase(typeGuid, kGptAndroidExpand)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700416 createPrivateVolume(partDevice, partGuid);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800417 }
418 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800419 }
420 }
421
422 // Ugly last ditch effort, treat entire disk as partition
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700423 if (table == Table::kUnknown || !foundParts) {
424 LOG(WARNING) << mId << " has unknown partition table; trying entire device";
Jeff Sharkey63123c02015-06-26 11:16:14 -0700425
426 std::string fsType;
427 std::string unused;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600428 if (ReadMetadataUntrusted(mDevPath, &fsType, &unused, &unused) == OK) {
Jeff Sharkey63123c02015-06-26 11:16:14 -0700429 createPublicVolume(mDevice);
430 } else {
431 LOG(WARNING) << mId << " failed to identify, giving up";
432 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800433 }
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700434
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600435 auto listener = VolumeManager::Instance()->getListener();
436 if (listener) listener->onDiskScanned(getId());
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600437
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700438 mJustPartitioned = false;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800439 return OK;
440}
441
Risan82e90de2020-02-04 16:07:21 +0900442void Disk::initializePartition(std::shared_ptr<StubVolume> vol) {
443 CHECK(isStub());
444 CHECK(mVolumes.empty());
445 mVolumes.push_back(vol);
446}
447
Jeff Sharkey9c484982015-03-31 10:35:33 -0700448status_t Disk::unmountAll() {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700449 for (const auto& vol : mVolumes) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700450 vol->unmount();
451 }
452 return OK;
453}
454
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800455status_t Disk::partitionPublic() {
Jeff Sharkeydadccee2015-09-23 14:13:45 -0700456 int res;
457
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700458 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700459 mJustPartitioned = true;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800460
Scott Mertzad5b4f92016-03-31 13:09:56 -0700461 // Determine if we're coming from MBR
Jeff Sharkeydadccee2015-09-23 14:13:45 -0700462 std::vector<std::string> cmd;
463 cmd.push_back(kSgdiskPath);
Scott Mertzad5b4f92016-03-31 13:09:56 -0700464 cmd.push_back("--android-dump");
465 cmd.push_back(mDevPath);
466
467 std::vector<std::string> output;
468 res = ForkExecvp(cmd, &output);
Scott Mertzad5b4f92016-03-31 13:09:56 -0700469 Table table = Table::kUnknown;
nift42711bf72023-07-01 11:49:12 +0200470 // fails when there is no partition table, it's okay
471 if (res == OK) {
472 for (auto line : output) {
473 char* cline = (char*) line.c_str();
474 char* token = strtok(cline, kSgdiskToken);
475 if (token == nullptr) continue;
Scott Mertzad5b4f92016-03-31 13:09:56 -0700476
nift42711bf72023-07-01 11:49:12 +0200477 if (!strcmp(token, "DISK")) {
478 const char* type = strtok(nullptr, kSgdiskToken);
479 if (!strcmp(type, "mbr")) {
480 table = Table::kMbr;
481 break;
482 } else if (!strcmp(type, "gpt")) {
483 table = Table::kGpt;
484 break;
485 }
Scott Mertzad5b4f92016-03-31 13:09:56 -0700486 }
487 }
488 }
489
490 if (table == Table::kMbr) {
491 LOG(INFO) << "skip first disk change event due to MBR -> GPT switch";
492 mSkipChange = true;
493 }
494
495 // First nuke any existing partition table
496 cmd.clear();
497 cmd.push_back(kSgdiskPath);
Jeff Sharkeydadccee2015-09-23 14:13:45 -0700498 cmd.push_back("--zap-all");
499 cmd.push_back(mDevPath);
500
501 // Zap sometimes returns an error when it actually succeeded, so
502 // just log as warning and keep rolling forward.
503 if ((res = ForkExecvp(cmd)) != 0) {
504 LOG(WARNING) << "Failed to zap; status " << res;
505 }
506
Jeff Sharkey68f1b8b2017-10-18 14:09:52 -0600507 // Now let's build the new MBR table. We heavily rely on sgdisk to
508 // force optimal alignment on the created partitions.
509 cmd.clear();
510 cmd.push_back(kSgdiskPath);
511 cmd.push_back("--new=0:0:-0");
512 cmd.push_back("--typecode=0:0c00");
513 cmd.push_back("--gpttombr=1");
514 cmd.push_back(mDevPath);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800515
Jeff Sharkey68f1b8b2017-10-18 14:09:52 -0600516 if ((res = ForkExecvp(cmd)) != 0) {
517 LOG(ERROR) << "Failed to partition; status " << res;
518 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800519 }
520
Jeff Sharkey68f1b8b2017-10-18 14:09:52 -0600521 return OK;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800522}
523
524status_t Disk::partitionPrivate() {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700525 return partitionMixed(0);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800526}
527
528status_t Disk::partitionMixed(int8_t ratio) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700529 int res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700530
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700531 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700532 mJustPartitioned = true;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700533
534 // First nuke any existing partition table
535 std::vector<std::string> cmd;
536 cmd.push_back(kSgdiskPath);
537 cmd.push_back("--zap-all");
538 cmd.push_back(mDevPath);
539
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700540 // Zap sometimes returns an error when it actually succeeded, so
541 // just log as warning and keep rolling forward.
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700542 if ((res = ForkExecvp(cmd)) != 0) {
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700543 LOG(WARNING) << "Failed to zap; status " << res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700544 }
545
546 // We've had some success above, so generate both the private partition
547 // GUID and encryption key and persist them.
548 std::string partGuidRaw;
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600549 if (GenerateRandomUuid(partGuidRaw) != OK) {
550 LOG(ERROR) << "Failed to generate GUID";
551 return -EIO;
552 }
553
Paul Crowleyb3d018a2020-02-12 11:04:05 -0800554 KeyBuffer key;
Paul Crowley886e5722020-02-07 12:51:56 -0800555 if (!generate_volume_key(&key)) {
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600556 LOG(ERROR) << "Failed to generate key";
Jeff Sharkey9c484982015-03-31 10:35:33 -0700557 return -EIO;
558 }
Paul Crowleyb3d018a2020-02-12 11:04:05 -0800559 std::string keyRaw(key.begin(), key.end());
Jeff Sharkey9c484982015-03-31 10:35:33 -0700560
561 std::string partGuid;
562 StrToHex(partGuidRaw, partGuid);
563
564 if (!WriteStringToFile(keyRaw, BuildKeyPath(partGuid))) {
565 LOG(ERROR) << "Failed to persist key";
566 return -EIO;
567 } else {
568 LOG(DEBUG) << "Persisted key for GUID " << partGuid;
569 }
570
571 // Now let's build the new GPT table. We heavily rely on sgdisk to
572 // force optimal alignment on the created partitions.
573 cmd.clear();
574 cmd.push_back(kSgdiskPath);
575
576 // If requested, create a public partition first. Mixed-mode partitioning
577 // like this is an experimental feature.
578 if (ratio > 0) {
579 if (ratio < 10 || ratio > 90) {
580 LOG(ERROR) << "Mixed partition ratio must be between 10-90%";
581 return -EINVAL;
582 }
583
584 uint64_t splitMb = ((mSize / 100) * ratio) / 1024 / 1024;
585 cmd.push_back(StringPrintf("--new=0:0:+%" PRId64 "M", splitMb));
586 cmd.push_back(StringPrintf("--typecode=0:%s", kGptBasicData));
587 cmd.push_back("--change-name=0:shared");
588 }
589
590 // Define a metadata partition which is designed for future use; there
591 // should only be one of these per physical device, even if there are
592 // multiple private volumes.
593 cmd.push_back("--new=0:0:+16M");
594 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidMeta));
595 cmd.push_back("--change-name=0:android_meta");
596
597 // Define a single private partition filling the rest of disk.
598 cmd.push_back("--new=0:0:-0");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700599 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidExpand));
Jeff Sharkey9c484982015-03-31 10:35:33 -0700600 cmd.push_back(StringPrintf("--partition-guid=0:%s", partGuid.c_str()));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700601 cmd.push_back("--change-name=0:android_expand");
Jeff Sharkey9c484982015-03-31 10:35:33 -0700602
603 cmd.push_back(mDevPath);
604
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700605 if ((res = ForkExecvp(cmd)) != 0) {
606 LOG(ERROR) << "Failed to partition; status " << res;
607 return res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700608 }
609
610 return OK;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800611}
612
613int Disk::getMaxMinors() {
614 // Figure out maximum partition devices supported
Yu Ning942d4e82016-01-08 17:36:47 +0800615 unsigned int majorId = major(mDevice);
616 switch (majorId) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700617 case kMajorBlockLoop: {
618 std::string tmp;
619 if (!ReadFileToString(kSysfsLoopMaxMinors, &tmp)) {
620 LOG(ERROR) << "Failed to read max minors";
621 return -errno;
622 }
623 return std::stoi(tmp);
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600624 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700625 // clang-format off
626 case kMajorBlockScsiA: case kMajorBlockScsiB: case kMajorBlockScsiC:
627 case kMajorBlockScsiD: case kMajorBlockScsiE: case kMajorBlockScsiF:
628 case kMajorBlockScsiG: case kMajorBlockScsiH: case kMajorBlockScsiI:
629 case kMajorBlockScsiJ: case kMajorBlockScsiK: case kMajorBlockScsiL:
630 case kMajorBlockScsiM: case kMajorBlockScsiN: case kMajorBlockScsiO:
631 case kMajorBlockScsiP: {
632 // clang-format on
633 // Per Documentation/devices.txt this is static
Yu Ning942d4e82016-01-08 17:36:47 +0800634 return 15;
635 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700636 case kMajorBlockMmc: {
637 // Per Documentation/devices.txt this is dynamic
638 std::string tmp;
639 if (!ReadFileToString(kSysfsMmcMaxMinors, &tmp) &&
640 !ReadFileToString(kSysfsMmcMaxMinorsDeprecated, &tmp)) {
641 LOG(ERROR) << "Failed to read max minors";
642 return -errno;
643 }
644 return std::stoi(tmp);
Dmitry Shmidt06dc6e52018-05-11 17:22:42 -0700645 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700646 default: {
Alistair Delvaff1fc9b2020-05-14 16:35:03 -0700647 if (IsVirtioBlkDevice(majorId)) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700648 // drivers/block/virtio_blk.c has "#define PART_BITS 4", so max is
649 // 2^4 - 1 = 15
650 return 15;
651 }
652 if (isNvmeBlkDevice(majorId, mSysPath)) {
653 // despite kernel nvme driver supports up to 1M minors,
654 // #define NVME_MINORS (1U << MINORBITS)
655 // sgdisk can not support more than 127 partitions, due to
656 // #define MAX_MBR_PARTS 128
657 return 127;
658 }
659 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800660 }
661
Yu Ning942d4e82016-01-08 17:36:47 +0800662 LOG(ERROR) << "Unsupported block major type " << majorId;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800663 return -ENOTSUP;
664}
665
666} // namespace vold
667} // namespace android