blob: 0ca894b82705beee1d7e74dbac3d64e4039ee1ae [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
Fairphone ODM6c3a4e22021-09-14 15:15:31 +080026#include "fs/Exfat.h"
27
Elliott Hughes7e128fb2015-12-04 15:50:53 -080028#include <android-base/file.h>
Paul Crowley3b71fc52017-10-09 10:55:21 -070029#include <android-base/logging.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070030#include <android-base/parseint.h>
Jeff Sharkey46bb69f2017-06-21 13:52:23 -060031#include <android-base/properties.h>
Elliott Hughes7e128fb2015-12-04 15:50:53 -080032#include <android-base/stringprintf.h>
Jeff Sharkey3472e522017-10-06 18:02:53 -060033#include <android-base/strings.h>
Eric Biggersa701c452018-10-23 13:06:55 -070034#include <fscrypt/fscrypt.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080035
36#include <fcntl.h>
Jeff Sharkey38cfc022015-03-30 21:22:07 -070037#include <inttypes.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080038#include <stdio.h>
39#include <stdlib.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070040#include <sys/mount.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080041#include <sys/stat.h>
Elliott Hughes0e08e842017-05-18 09:08:24 -070042#include <sys/sysmacros.h>
Paul Crowley14c8c072018-09-18 13:30:21 -070043#include <sys/types.h>
44#include <vector>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080045
Dan Albertae9e8902015-03-16 10:35:17 -070046using android::base::ReadFileToString;
47using android::base::StringPrintf;
Paul Crowley14c8c072018-09-18 13:30:21 -070048using android::base::WriteStringToFile;
Dan Albertae9e8902015-03-16 10:35:17 -070049
Jeff Sharkeydeb24052015-03-02 21:01:40 -080050namespace android {
51namespace vold {
52
53static const char* kSgdiskPath = "/system/bin/sgdisk";
54static const char* kSgdiskToken = " \t\n";
55
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060056static const char* kSysfsLoopMaxMinors = "/sys/module/loop/parameters/max_part";
Pierre-Hugues Hussonf347cd02017-11-28 15:42:56 +010057static const char* kSysfsMmcMaxMinorsDeprecated = "/sys/module/mmcblk/parameters/perdev_minors";
Smita Ghoshe158b962020-04-08 17:25:20 -070058static const char* kSysfsMmcMaxMinors = "/sys/module/mmc_block/parameters/mmcblk.perdev_minors";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080059
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060060static const unsigned int kMajorBlockLoop = 7;
Jeff Sharkeyf3ee2002015-04-19 15:55:42 -070061static const unsigned int kMajorBlockScsiA = 8;
62static const unsigned int kMajorBlockScsiB = 65;
63static const unsigned int kMajorBlockScsiC = 66;
64static const unsigned int kMajorBlockScsiD = 67;
65static const unsigned int kMajorBlockScsiE = 68;
66static const unsigned int kMajorBlockScsiF = 69;
67static const unsigned int kMajorBlockScsiG = 70;
68static const unsigned int kMajorBlockScsiH = 71;
69static const unsigned int kMajorBlockScsiI = 128;
70static const unsigned int kMajorBlockScsiJ = 129;
71static const unsigned int kMajorBlockScsiK = 130;
72static const unsigned int kMajorBlockScsiL = 131;
73static const unsigned int kMajorBlockScsiM = 132;
74static const unsigned int kMajorBlockScsiN = 133;
75static const unsigned int kMajorBlockScsiO = 134;
76static const unsigned int kMajorBlockScsiP = 135;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080077static const unsigned int kMajorBlockMmc = 179;
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
Dmitry Shmidt06dc6e52018-05-11 17:22:42 -070091static bool isNvmeBlkDevice(unsigned int major, const std::string& sysPath) {
Paul Crowley14c8c072018-09-18 13:30:21 -070092 return sysPath.find("nvme") != std::string::npos && major >= kMajorBlockDynamicMin &&
93 major <= kMajorBlockDynamicMax;
Dmitry Shmidt06dc6e52018-05-11 17:22:42 -070094}
95
Paul Crowley14c8c072018-09-18 13:30:21 -070096Disk::Disk(const std::string& eventPath, dev_t device, const std::string& nickname, int flags)
97 : mDevice(device),
98 mSize(-1),
99 mNickname(nickname),
100 mFlags(flags),
101 mCreated(false),
102 mJustPartitioned(false) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700103 mId = StringPrintf("disk:%u,%u", major(device), minor(device));
104 mEventPath = eventPath;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800105 mSysPath = StringPrintf("/sys/%s", eventPath.c_str());
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700106 mDevPath = StringPrintf("/dev/block/vold/%s", mId.c_str());
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800107 CreateDeviceNode(mDevPath, mDevice);
108}
109
110Disk::~Disk() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700111 CHECK(!mCreated);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800112 DestroyDeviceNode(mDevPath);
113}
114
115std::shared_ptr<VolumeBase> Disk::findVolume(const std::string& id) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700116 for (auto vol : mVolumes) {
117 if (vol->getId() == id) {
118 return vol;
119 }
120 auto stackedVol = vol->findVolume(id);
121 if (stackedVol != nullptr) {
122 return stackedVol;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800123 }
124 }
125 return nullptr;
126}
127
Greg Kaiser2bc201e2018-12-18 08:42:08 -0800128void Disk::listVolumes(VolumeBase::Type type, std::list<std::string>& list) const {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700129 for (const auto& vol : mVolumes) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700130 if (vol->getType() == type) {
131 list.push_back(vol->getId());
132 }
133 // TODO: consider looking at stacked volumes
134 }
135}
136
Martijn Coenen0a7e9922020-01-24 16:17:32 +0100137std::vector<std::shared_ptr<VolumeBase>> Disk::getVolumes() const {
138 std::vector<std::shared_ptr<VolumeBase>> vols;
139 for (const auto& vol : mVolumes) {
140 vols.push_back(vol);
141 auto stackedVolumes = vol->getVolumes();
142 vols.insert(vols.end(), stackedVolumes.begin(), stackedVolumes.end());
143 }
144
145 return vols;
146}
147
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700148status_t Disk::create() {
149 CHECK(!mCreated);
150 mCreated = true;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600151
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600152 auto listener = VolumeManager::Instance()->getListener();
153 if (listener) listener->onDiskCreated(getId(), mFlags);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600154
Risan82e90de2020-02-04 16:07:21 +0900155 if (isStub()) {
156 createStubVolume();
157 return OK;
158 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700159 readMetadata();
160 readPartitions();
161 return OK;
162}
163
164status_t Disk::destroy() {
165 CHECK(mCreated);
166 destroyAllVolumes();
167 mCreated = false;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600168
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600169 auto listener = VolumeManager::Instance()->getListener();
170 if (listener) listener->onDiskDestroyed(getId());
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600171
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700172 return OK;
173}
174
175void Disk::createPublicVolume(dev_t device) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700176 auto vol = std::shared_ptr<VolumeBase>(new PublicVolume(device));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700177 if (mJustPartitioned) {
178 LOG(DEBUG) << "Device just partitioned; silently formatting";
179 vol->setSilent(true);
180 vol->create();
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700181 vol->format("auto");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700182 vol->destroy();
183 vol->setSilent(false);
184 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700185
Jeff Sharkey9c484982015-03-31 10:35:33 -0700186 mVolumes.push_back(vol);
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700187 vol->setDiskId(getId());
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700188 vol->create();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700189}
190
Jeff Sharkey9c484982015-03-31 10:35:33 -0700191void Disk::createPrivateVolume(dev_t device, const std::string& partGuid) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700192 std::string normalizedGuid;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700193 if (NormalizeHex(partGuid, normalizedGuid)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700194 LOG(WARNING) << "Invalid GUID " << partGuid;
195 return;
196 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700197
198 std::string keyRaw;
199 if (!ReadFileToString(BuildKeyPath(normalizedGuid), &keyRaw)) {
200 PLOG(ERROR) << "Failed to load key for GUID " << normalizedGuid;
201 return;
202 }
203
204 LOG(DEBUG) << "Found key for GUID " << normalizedGuid;
205
Paul Crowley3d98f5d2020-02-07 11:49:09 -0800206 auto keyBuffer = KeyBuffer(keyRaw.begin(), keyRaw.end());
207 auto vol = std::shared_ptr<VolumeBase>(new PrivateVolume(device, keyBuffer));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700208 if (mJustPartitioned) {
209 LOG(DEBUG) << "Device just partitioned; silently formatting";
210 vol->setSilent(true);
211 vol->create();
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700212 vol->format("auto");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700213 vol->destroy();
214 vol->setSilent(false);
215 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700216
217 mVolumes.push_back(vol);
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700218 vol->setDiskId(getId());
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700219 vol->setPartGuid(partGuid);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700220 vol->create();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700221}
222
Risan82e90de2020-02-04 16:07:21 +0900223void Disk::createStubVolume() {
224 CHECK(mVolumes.size() == 1);
225 auto listener = VolumeManager::Instance()->getListener();
226 if (listener) listener->onDiskMetadataChanged(getId(), mSize, mLabel, mSysPath);
227 if (listener) listener->onDiskScanned(getId());
228 mVolumes[0]->setDiskId(getId());
229 mVolumes[0]->create();
230}
231
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700232void Disk::destroyAllVolumes() {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700233 for (const auto& vol : mVolumes) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700234 vol->destroy();
235 }
236 mVolumes.clear();
237}
238
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800239status_t Disk::readMetadata() {
240 mSize = -1;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700241 mLabel.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800242
Oleksiy Avramchenko625dc782018-05-23 10:50:46 +0200243 if (GetBlockDevSize(mDevPath, &mSize) != OK) {
244 mSize = -1;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800245 }
246
Yu Ning942d4e82016-01-08 17:36:47 +0800247 unsigned int majorId = major(mDevice);
248 switch (majorId) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700249 case kMajorBlockLoop: {
Yu Ning942d4e82016-01-08 17:36:47 +0800250 mLabel = "Virtual";
251 break;
252 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700253 // clang-format off
254 case kMajorBlockScsiA: case kMajorBlockScsiB: case kMajorBlockScsiC:
255 case kMajorBlockScsiD: case kMajorBlockScsiE: case kMajorBlockScsiF:
256 case kMajorBlockScsiG: case kMajorBlockScsiH: case kMajorBlockScsiI:
257 case kMajorBlockScsiJ: case kMajorBlockScsiK: case kMajorBlockScsiL:
258 case kMajorBlockScsiM: case kMajorBlockScsiN: case kMajorBlockScsiO:
259 case kMajorBlockScsiP: {
260 // clang-format on
261 std::string path(mSysPath + "/device/vendor");
Dmitry Shmidt06dc6e52018-05-11 17:22:42 -0700262 std::string tmp;
263 if (!ReadFileToString(path, &tmp)) {
264 PLOG(WARNING) << "Failed to read vendor from " << path;
265 return -errno;
266 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700267 tmp = android::base::Trim(tmp);
Dmitry Shmidt06dc6e52018-05-11 17:22:42 -0700268 mLabel = tmp;
269 break;
270 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700271 case kMajorBlockMmc: {
272 std::string path(mSysPath + "/device/manfid");
273 std::string tmp;
274 if (!ReadFileToString(path, &tmp)) {
275 PLOG(WARNING) << "Failed to read manufacturer from " << path;
276 return -errno;
277 }
278 tmp = android::base::Trim(tmp);
279 int64_t manfid;
280 if (!android::base::ParseInt(tmp, &manfid)) {
281 PLOG(WARNING) << "Failed to parse manufacturer " << tmp;
282 return -EINVAL;
283 }
284 // Our goal here is to give the user a meaningful label, ideally
285 // matching whatever is silk-screened on the card. To reduce
286 // user confusion, this list doesn't contain white-label manfid.
287 switch (manfid) {
288 // clang-format off
289 case 0x000003: mLabel = "SanDisk"; break;
290 case 0x00001b: mLabel = "Samsung"; break;
291 case 0x000028: mLabel = "Lexar"; break;
292 case 0x000074: mLabel = "Transcend"; break;
293 // clang-format on
294 }
295 break;
296 }
297 default: {
Alistair Delvaff1fc9b2020-05-14 16:35:03 -0700298 if (IsVirtioBlkDevice(majorId)) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700299 LOG(DEBUG) << "Recognized experimental block major ID " << majorId
300 << " as virtio-blk (emulator's virtual SD card device)";
301 mLabel = "Virtual";
302 break;
303 }
304 if (isNvmeBlkDevice(majorId, mSysPath)) {
305 std::string path(mSysPath + "/device/model");
306 std::string tmp;
307 if (!ReadFileToString(path, &tmp)) {
308 PLOG(WARNING) << "Failed to read vendor from " << path;
309 return -errno;
310 }
311 mLabel = tmp;
312 break;
313 }
314 LOG(WARNING) << "Unsupported block major type " << majorId;
315 return -ENOTSUP;
316 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800317 }
318
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600319 auto listener = VolumeManager::Instance()->getListener();
Paul Crowley14c8c072018-09-18 13:30:21 -0700320 if (listener) listener->onDiskMetadataChanged(getId(), mSize, mLabel, mSysPath);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600321
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800322 return OK;
323}
324
325status_t Disk::readPartitions() {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600326 int maxMinors = getMaxMinors();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800327 if (maxMinors < 0) {
328 return -ENOTSUP;
329 }
330
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700331 destroyAllVolumes();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800332
333 // Parse partition table
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700334
335 std::vector<std::string> cmd;
336 cmd.push_back(kSgdiskPath);
337 cmd.push_back("--android-dump");
338 cmd.push_back(mDevPath);
339
340 std::vector<std::string> output;
Paul Crowleyde2d6202018-11-30 11:43:47 -0800341 status_t res = ForkExecvp(cmd, &output);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700342 if (res != OK) {
343 LOG(WARNING) << "sgdisk failed to scan " << mDevPath;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600344
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600345 auto listener = VolumeManager::Instance()->getListener();
346 if (listener) listener->onDiskScanned(getId());
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600347
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700348 mJustPartitioned = false;
349 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800350 }
351
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800352 Table table = Table::kUnknown;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700353 bool foundParts = false;
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700354 for (const auto& line : output) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600355 auto split = android::base::Split(line, kSgdiskToken);
356 auto it = split.begin();
357 if (it == split.end()) continue;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700358
Jeff Sharkey3472e522017-10-06 18:02:53 -0600359 if (*it == "DISK") {
360 if (++it == split.end()) continue;
361 if (*it == "mbr") {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800362 table = Table::kMbr;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600363 } else if (*it == "gpt") {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800364 table = Table::kGpt;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600365 } else {
366 LOG(WARNING) << "Invalid partition table " << *it;
367 continue;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800368 }
Jeff Sharkey3472e522017-10-06 18:02:53 -0600369 } else if (*it == "PART") {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700370 foundParts = true;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600371
372 if (++it == split.end()) continue;
373 int i = 0;
374 if (!android::base::ParseInt(*it, &i, 1, maxMinors)) {
375 LOG(WARNING) << "Invalid partition number " << *it;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800376 continue;
377 }
378 dev_t partDevice = makedev(major(mDevice), minor(mDevice) + i);
379
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800380 if (table == Table::kMbr) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600381 if (++it == split.end()) continue;
382 int type = 0;
383 if (!android::base::ParseInt("0x" + *it, &type)) {
384 LOG(WARNING) << "Invalid partition type " << *it;
385 continue;
386 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800387
Jeff Sharkey3472e522017-10-06 18:02:53 -0600388 switch (type) {
Fairphone ODM6c3a4e22021-09-14 15:15:31 +0800389 case 0x01: // FAT12 (in first 32 MB of disk)
390 case 0x04: // FAT16 (in first 32 MB of disk)
Jeff Sharkey37ba1252018-01-19 10:55:18 +0900391 case 0x06: // FAT16
392 case 0x07: // HPFS/NTFS/exFAT
393 case 0x0b: // W95 FAT32 (LBA)
394 case 0x0c: // W95 FAT32 (LBA)
395 case 0x0e: // W95 FAT16 (LBA)
396 createPublicVolume(partDevice);
397 break;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800398 }
399 } else if (table == Table::kGpt) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600400 if (++it == split.end()) continue;
401 auto typeGuid = *it;
402 if (++it == split.end()) continue;
403 auto partGuid = *it;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800404
Jeff Sharkey3472e522017-10-06 18:02:53 -0600405 if (android::base::EqualsIgnoreCase(typeGuid, kGptBasicData)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700406 createPublicVolume(partDevice);
Jeff Sharkey3472e522017-10-06 18:02:53 -0600407 } else if (android::base::EqualsIgnoreCase(typeGuid, kGptAndroidExpand)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700408 createPrivateVolume(partDevice, partGuid);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800409 }
410 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800411 }
412 }
413
414 // Ugly last ditch effort, treat entire disk as partition
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700415 if (table == Table::kUnknown || !foundParts) {
416 LOG(WARNING) << mId << " has unknown partition table; trying entire device";
Jeff Sharkey63123c02015-06-26 11:16:14 -0700417
418 std::string fsType;
419 std::string unused;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600420 if (ReadMetadataUntrusted(mDevPath, &fsType, &unused, &unused) == OK) {
Jeff Sharkey63123c02015-06-26 11:16:14 -0700421 createPublicVolume(mDevice);
422 } else {
423 LOG(WARNING) << mId << " failed to identify, giving up";
424 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800425 }
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700426
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600427 auto listener = VolumeManager::Instance()->getListener();
428 if (listener) listener->onDiskScanned(getId());
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600429
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700430 mJustPartitioned = false;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800431 return OK;
432}
433
Risan82e90de2020-02-04 16:07:21 +0900434void Disk::initializePartition(std::shared_ptr<StubVolume> vol) {
435 CHECK(isStub());
436 CHECK(mVolumes.empty());
437 mVolumes.push_back(vol);
438}
439
Jeff Sharkey9c484982015-03-31 10:35:33 -0700440status_t Disk::unmountAll() {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700441 for (const auto& vol : mVolumes) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700442 vol->unmount();
443 }
444 return OK;
445}
446
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800447status_t Disk::partitionPublic() {
Jeff Sharkeydadccee2015-09-23 14:13:45 -0700448 int res;
449
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700450 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700451 mJustPartitioned = true;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800452
Jeff Sharkeydadccee2015-09-23 14:13:45 -0700453 // First nuke any existing partition table
454 std::vector<std::string> cmd;
455 cmd.push_back(kSgdiskPath);
456 cmd.push_back("--zap-all");
457 cmd.push_back(mDevPath);
458
459 // Zap sometimes returns an error when it actually succeeded, so
460 // just log as warning and keep rolling forward.
461 if ((res = ForkExecvp(cmd)) != 0) {
462 LOG(WARNING) << "Failed to zap; status " << res;
463 }
464
Fairphone ODM6c3a4e22021-09-14 15:15:31 +0800465 if (exfat::CheckSize(mDevPath, false) == 1) {
466 /* Should format filesystem as exFAT, so we must partition the device
467 * according to SDXC rules. */
468 return exfat::PartitionDisk(mDevPath);
469 }
470
Jeff Sharkey68f1b8b2017-10-18 14:09:52 -0600471 // Now let's build the new MBR table. We heavily rely on sgdisk to
472 // force optimal alignment on the created partitions.
473 cmd.clear();
474 cmd.push_back(kSgdiskPath);
475 cmd.push_back("--new=0:0:-0");
476 cmd.push_back("--typecode=0:0c00");
477 cmd.push_back("--gpttombr=1");
478 cmd.push_back(mDevPath);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800479
Jeff Sharkey68f1b8b2017-10-18 14:09:52 -0600480 if ((res = ForkExecvp(cmd)) != 0) {
481 LOG(ERROR) << "Failed to partition; status " << res;
482 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800483 }
484
Jeff Sharkey68f1b8b2017-10-18 14:09:52 -0600485 return OK;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800486}
487
488status_t Disk::partitionPrivate() {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700489 return partitionMixed(0);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800490}
491
492status_t Disk::partitionMixed(int8_t ratio) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700493 int res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700494
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700495 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700496 mJustPartitioned = true;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700497
498 // First nuke any existing partition table
499 std::vector<std::string> cmd;
500 cmd.push_back(kSgdiskPath);
501 cmd.push_back("--zap-all");
502 cmd.push_back(mDevPath);
503
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700504 // Zap sometimes returns an error when it actually succeeded, so
505 // just log as warning and keep rolling forward.
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700506 if ((res = ForkExecvp(cmd)) != 0) {
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700507 LOG(WARNING) << "Failed to zap; status " << res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700508 }
509
510 // We've had some success above, so generate both the private partition
511 // GUID and encryption key and persist them.
512 std::string partGuidRaw;
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600513 if (GenerateRandomUuid(partGuidRaw) != OK) {
514 LOG(ERROR) << "Failed to generate GUID";
515 return -EIO;
516 }
517
Paul Crowleyb3d018a2020-02-12 11:04:05 -0800518 KeyBuffer key;
Paul Crowley886e5722020-02-07 12:51:56 -0800519 if (!generate_volume_key(&key)) {
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600520 LOG(ERROR) << "Failed to generate key";
Jeff Sharkey9c484982015-03-31 10:35:33 -0700521 return -EIO;
522 }
Paul Crowleyb3d018a2020-02-12 11:04:05 -0800523 std::string keyRaw(key.begin(), key.end());
Jeff Sharkey9c484982015-03-31 10:35:33 -0700524
525 std::string partGuid;
526 StrToHex(partGuidRaw, partGuid);
527
528 if (!WriteStringToFile(keyRaw, BuildKeyPath(partGuid))) {
529 LOG(ERROR) << "Failed to persist key";
530 return -EIO;
531 } else {
532 LOG(DEBUG) << "Persisted key for GUID " << partGuid;
533 }
534
535 // Now let's build the new GPT table. We heavily rely on sgdisk to
536 // force optimal alignment on the created partitions.
537 cmd.clear();
538 cmd.push_back(kSgdiskPath);
539
540 // If requested, create a public partition first. Mixed-mode partitioning
541 // like this is an experimental feature.
542 if (ratio > 0) {
543 if (ratio < 10 || ratio > 90) {
544 LOG(ERROR) << "Mixed partition ratio must be between 10-90%";
545 return -EINVAL;
546 }
547
548 uint64_t splitMb = ((mSize / 100) * ratio) / 1024 / 1024;
549 cmd.push_back(StringPrintf("--new=0:0:+%" PRId64 "M", splitMb));
550 cmd.push_back(StringPrintf("--typecode=0:%s", kGptBasicData));
551 cmd.push_back("--change-name=0:shared");
552 }
553
554 // Define a metadata partition which is designed for future use; there
555 // should only be one of these per physical device, even if there are
556 // multiple private volumes.
557 cmd.push_back("--new=0:0:+16M");
558 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidMeta));
559 cmd.push_back("--change-name=0:android_meta");
560
561 // Define a single private partition filling the rest of disk.
562 cmd.push_back("--new=0:0:-0");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700563 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidExpand));
Jeff Sharkey9c484982015-03-31 10:35:33 -0700564 cmd.push_back(StringPrintf("--partition-guid=0:%s", partGuid.c_str()));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700565 cmd.push_back("--change-name=0:android_expand");
Jeff Sharkey9c484982015-03-31 10:35:33 -0700566
567 cmd.push_back(mDevPath);
568
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700569 if ((res = ForkExecvp(cmd)) != 0) {
570 LOG(ERROR) << "Failed to partition; status " << res;
571 return res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700572 }
573
574 return OK;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800575}
576
577int Disk::getMaxMinors() {
578 // Figure out maximum partition devices supported
Yu Ning942d4e82016-01-08 17:36:47 +0800579 unsigned int majorId = major(mDevice);
580 switch (majorId) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700581 case kMajorBlockLoop: {
582 std::string tmp;
583 if (!ReadFileToString(kSysfsLoopMaxMinors, &tmp)) {
584 LOG(ERROR) << "Failed to read max minors";
585 return -errno;
586 }
587 return std::stoi(tmp);
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600588 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700589 // clang-format off
590 case kMajorBlockScsiA: case kMajorBlockScsiB: case kMajorBlockScsiC:
591 case kMajorBlockScsiD: case kMajorBlockScsiE: case kMajorBlockScsiF:
592 case kMajorBlockScsiG: case kMajorBlockScsiH: case kMajorBlockScsiI:
593 case kMajorBlockScsiJ: case kMajorBlockScsiK: case kMajorBlockScsiL:
594 case kMajorBlockScsiM: case kMajorBlockScsiN: case kMajorBlockScsiO:
595 case kMajorBlockScsiP: {
596 // clang-format on
597 // Per Documentation/devices.txt this is static
Yu Ning942d4e82016-01-08 17:36:47 +0800598 return 15;
599 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700600 case kMajorBlockMmc: {
601 // Per Documentation/devices.txt this is dynamic
602 std::string tmp;
603 if (!ReadFileToString(kSysfsMmcMaxMinors, &tmp) &&
604 !ReadFileToString(kSysfsMmcMaxMinorsDeprecated, &tmp)) {
605 LOG(ERROR) << "Failed to read max minors";
606 return -errno;
607 }
608 return std::stoi(tmp);
Dmitry Shmidt06dc6e52018-05-11 17:22:42 -0700609 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700610 default: {
Alistair Delvaff1fc9b2020-05-14 16:35:03 -0700611 if (IsVirtioBlkDevice(majorId)) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700612 // drivers/block/virtio_blk.c has "#define PART_BITS 4", so max is
613 // 2^4 - 1 = 15
614 return 15;
615 }
616 if (isNvmeBlkDevice(majorId, mSysPath)) {
617 // despite kernel nvme driver supports up to 1M minors,
618 // #define NVME_MINORS (1U << MINORBITS)
619 // sgdisk can not support more than 127 partitions, due to
620 // #define MAX_MBR_PARTS 128
621 return 127;
622 }
623 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800624 }
625
Yu Ning942d4e82016-01-08 17:36:47 +0800626 LOG(ERROR) << "Unsupported block major type " << majorId;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800627 return -ENOTSUP;
628}
629
630} // namespace vold
631} // namespace android