blob: 09dc360d87ede6f3786935d32b7d9553b9a6024a [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 Kaiseref9abab2018-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
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700135status_t Disk::create() {
136 CHECK(!mCreated);
137 mCreated = true;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600138
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600139 auto listener = VolumeManager::Instance()->getListener();
140 if (listener) listener->onDiskCreated(getId(), mFlags);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600141
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700142 readMetadata();
143 readPartitions();
144 return OK;
145}
146
147status_t Disk::destroy() {
148 CHECK(mCreated);
149 destroyAllVolumes();
150 mCreated = false;
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->onDiskDestroyed(getId());
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600154
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700155 return OK;
156}
157
158void Disk::createPublicVolume(dev_t device) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700159 auto vol = std::shared_ptr<VolumeBase>(new PublicVolume(device));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700160 if (mJustPartitioned) {
161 LOG(DEBUG) << "Device just partitioned; silently formatting";
162 vol->setSilent(true);
163 vol->create();
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700164 vol->format("auto");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700165 vol->destroy();
166 vol->setSilent(false);
167 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700168
Jeff Sharkey9c484982015-03-31 10:35:33 -0700169 mVolumes.push_back(vol);
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700170 vol->setDiskId(getId());
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700171 vol->create();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700172}
173
Jeff Sharkey9c484982015-03-31 10:35:33 -0700174void Disk::createPrivateVolume(dev_t device, const std::string& partGuid) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700175 std::string normalizedGuid;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700176 if (NormalizeHex(partGuid, normalizedGuid)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700177 LOG(WARNING) << "Invalid GUID " << partGuid;
178 return;
179 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700180
181 std::string keyRaw;
182 if (!ReadFileToString(BuildKeyPath(normalizedGuid), &keyRaw)) {
183 PLOG(ERROR) << "Failed to load key for GUID " << normalizedGuid;
184 return;
185 }
186
187 LOG(DEBUG) << "Found key for GUID " << normalizedGuid;
188
Paul Crowley3d98f5d2020-02-07 11:49:09 -0800189 auto keyBuffer = KeyBuffer(keyRaw.begin(), keyRaw.end());
190 auto vol = std::shared_ptr<VolumeBase>(new PrivateVolume(device, keyBuffer));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700191 if (mJustPartitioned) {
192 LOG(DEBUG) << "Device just partitioned; silently formatting";
193 vol->setSilent(true);
194 vol->create();
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700195 vol->format("auto");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700196 vol->destroy();
197 vol->setSilent(false);
198 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700199
200 mVolumes.push_back(vol);
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700201 vol->setDiskId(getId());
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700202 vol->setPartGuid(partGuid);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700203 vol->create();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700204}
205
206void Disk::destroyAllVolumes() {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700207 for (const auto& vol : mVolumes) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700208 vol->destroy();
209 }
210 mVolumes.clear();
211}
212
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800213status_t Disk::readMetadata() {
214 mSize = -1;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700215 mLabel.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800216
Oleksiy Avramchenko625dc782018-05-23 10:50:46 +0200217 if (GetBlockDevSize(mDevPath, &mSize) != OK) {
218 mSize = -1;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800219 }
220
Yu Ning942d4e82016-01-08 17:36:47 +0800221 unsigned int majorId = major(mDevice);
222 switch (majorId) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700223 case kMajorBlockLoop: {
Yu Ning942d4e82016-01-08 17:36:47 +0800224 mLabel = "Virtual";
225 break;
226 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700227 // clang-format off
228 case kMajorBlockScsiA: case kMajorBlockScsiB: case kMajorBlockScsiC:
229 case kMajorBlockScsiD: case kMajorBlockScsiE: case kMajorBlockScsiF:
230 case kMajorBlockScsiG: case kMajorBlockScsiH: case kMajorBlockScsiI:
231 case kMajorBlockScsiJ: case kMajorBlockScsiK: case kMajorBlockScsiL:
232 case kMajorBlockScsiM: case kMajorBlockScsiN: case kMajorBlockScsiO:
233 case kMajorBlockScsiP: {
234 // clang-format on
235 std::string path(mSysPath + "/device/vendor");
Dmitry Shmidt06dc6e52018-05-11 17:22:42 -0700236 std::string tmp;
237 if (!ReadFileToString(path, &tmp)) {
238 PLOG(WARNING) << "Failed to read vendor from " << path;
239 return -errno;
240 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700241 tmp = android::base::Trim(tmp);
Dmitry Shmidt06dc6e52018-05-11 17:22:42 -0700242 mLabel = tmp;
243 break;
244 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700245 case kMajorBlockMmc: {
246 std::string path(mSysPath + "/device/manfid");
247 std::string tmp;
248 if (!ReadFileToString(path, &tmp)) {
249 PLOG(WARNING) << "Failed to read manufacturer from " << path;
250 return -errno;
251 }
252 tmp = android::base::Trim(tmp);
253 int64_t manfid;
254 if (!android::base::ParseInt(tmp, &manfid)) {
255 PLOG(WARNING) << "Failed to parse manufacturer " << tmp;
256 return -EINVAL;
257 }
258 // Our goal here is to give the user a meaningful label, ideally
259 // matching whatever is silk-screened on the card. To reduce
260 // user confusion, this list doesn't contain white-label manfid.
261 switch (manfid) {
262 // clang-format off
263 case 0x000003: mLabel = "SanDisk"; break;
264 case 0x00001b: mLabel = "Samsung"; break;
265 case 0x000028: mLabel = "Lexar"; break;
266 case 0x000074: mLabel = "Transcend"; break;
267 // clang-format on
268 }
269 break;
270 }
271 default: {
Alistair Delvad3c230b2020-05-14 16:35:03 -0700272 if (IsVirtioBlkDevice(majorId)) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700273 LOG(DEBUG) << "Recognized experimental block major ID " << majorId
274 << " as virtio-blk (emulator's virtual SD card device)";
275 mLabel = "Virtual";
276 break;
277 }
278 if (isNvmeBlkDevice(majorId, mSysPath)) {
279 std::string path(mSysPath + "/device/model");
280 std::string tmp;
281 if (!ReadFileToString(path, &tmp)) {
282 PLOG(WARNING) << "Failed to read vendor from " << path;
283 return -errno;
284 }
285 mLabel = tmp;
286 break;
287 }
288 LOG(WARNING) << "Unsupported block major type " << majorId;
289 return -ENOTSUP;
290 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800291 }
292
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600293 auto listener = VolumeManager::Instance()->getListener();
Paul Crowley14c8c072018-09-18 13:30:21 -0700294 if (listener) listener->onDiskMetadataChanged(getId(), mSize, mLabel, mSysPath);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600295
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800296 return OK;
297}
298
299status_t Disk::readPartitions() {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600300 int maxMinors = getMaxMinors();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800301 if (maxMinors < 0) {
302 return -ENOTSUP;
303 }
304
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700305 destroyAllVolumes();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800306
307 // Parse partition table
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700308
309 std::vector<std::string> cmd;
310 cmd.push_back(kSgdiskPath);
311 cmd.push_back("--android-dump");
312 cmd.push_back(mDevPath);
313
314 std::vector<std::string> output;
Paul Crowleyde2d6202018-11-30 11:43:47 -0800315 status_t res = ForkExecvp(cmd, &output);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700316 if (res != OK) {
317 LOG(WARNING) << "sgdisk failed to scan " << mDevPath;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600318
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600319 auto listener = VolumeManager::Instance()->getListener();
320 if (listener) listener->onDiskScanned(getId());
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600321
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700322 mJustPartitioned = false;
323 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800324 }
325
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800326 Table table = Table::kUnknown;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700327 bool foundParts = false;
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700328 for (const auto& line : output) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600329 auto split = android::base::Split(line, kSgdiskToken);
330 auto it = split.begin();
331 if (it == split.end()) continue;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700332
Jeff Sharkey3472e522017-10-06 18:02:53 -0600333 if (*it == "DISK") {
334 if (++it == split.end()) continue;
335 if (*it == "mbr") {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800336 table = Table::kMbr;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600337 } else if (*it == "gpt") {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800338 table = Table::kGpt;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600339 } else {
340 LOG(WARNING) << "Invalid partition table " << *it;
341 continue;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800342 }
Jeff Sharkey3472e522017-10-06 18:02:53 -0600343 } else if (*it == "PART") {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700344 foundParts = true;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600345
346 if (++it == split.end()) continue;
347 int i = 0;
348 if (!android::base::ParseInt(*it, &i, 1, maxMinors)) {
349 LOG(WARNING) << "Invalid partition number " << *it;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800350 continue;
351 }
352 dev_t partDevice = makedev(major(mDevice), minor(mDevice) + i);
353
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800354 if (table == Table::kMbr) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600355 if (++it == split.end()) continue;
356 int type = 0;
357 if (!android::base::ParseInt("0x" + *it, &type)) {
358 LOG(WARNING) << "Invalid partition type " << *it;
359 continue;
360 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800361
Jeff Sharkey3472e522017-10-06 18:02:53 -0600362 switch (type) {
Jeff Sharkey37ba1252018-01-19 10:55:18 +0900363 case 0x06: // FAT16
364 case 0x07: // HPFS/NTFS/exFAT
365 case 0x0b: // W95 FAT32 (LBA)
366 case 0x0c: // W95 FAT32 (LBA)
367 case 0x0e: // W95 FAT16 (LBA)
368 createPublicVolume(partDevice);
369 break;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800370 }
371 } else if (table == Table::kGpt) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600372 if (++it == split.end()) continue;
373 auto typeGuid = *it;
374 if (++it == split.end()) continue;
375 auto partGuid = *it;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800376
Jeff Sharkey3472e522017-10-06 18:02:53 -0600377 if (android::base::EqualsIgnoreCase(typeGuid, kGptBasicData)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700378 createPublicVolume(partDevice);
Jeff Sharkey3472e522017-10-06 18:02:53 -0600379 } else if (android::base::EqualsIgnoreCase(typeGuid, kGptAndroidExpand)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700380 createPrivateVolume(partDevice, partGuid);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800381 }
382 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800383 }
384 }
385
386 // Ugly last ditch effort, treat entire disk as partition
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700387 if (table == Table::kUnknown || !foundParts) {
388 LOG(WARNING) << mId << " has unknown partition table; trying entire device";
Jeff Sharkey63123c02015-06-26 11:16:14 -0700389
390 std::string fsType;
391 std::string unused;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600392 if (ReadMetadataUntrusted(mDevPath, &fsType, &unused, &unused) == OK) {
Jeff Sharkey63123c02015-06-26 11:16:14 -0700393 createPublicVolume(mDevice);
394 } else {
395 LOG(WARNING) << mId << " failed to identify, giving up";
396 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800397 }
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700398
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600399 auto listener = VolumeManager::Instance()->getListener();
400 if (listener) listener->onDiskScanned(getId());
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600401
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700402 mJustPartitioned = false;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800403 return OK;
404}
405
Jeff Sharkey9c484982015-03-31 10:35:33 -0700406status_t Disk::unmountAll() {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700407 for (const auto& vol : mVolumes) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700408 vol->unmount();
409 }
410 return OK;
411}
412
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800413status_t Disk::partitionPublic() {
Jeff Sharkeydadccee2015-09-23 14:13:45 -0700414 int res;
415
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700416 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700417 mJustPartitioned = true;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800418
Jeff Sharkeydadccee2015-09-23 14:13:45 -0700419 // First nuke any existing partition table
420 std::vector<std::string> cmd;
421 cmd.push_back(kSgdiskPath);
422 cmd.push_back("--zap-all");
423 cmd.push_back(mDevPath);
424
425 // Zap sometimes returns an error when it actually succeeded, so
426 // just log as warning and keep rolling forward.
427 if ((res = ForkExecvp(cmd)) != 0) {
428 LOG(WARNING) << "Failed to zap; status " << res;
429 }
430
Jeff Sharkey68f1b8b2017-10-18 14:09:52 -0600431 // Now let's build the new MBR table. We heavily rely on sgdisk to
432 // force optimal alignment on the created partitions.
433 cmd.clear();
434 cmd.push_back(kSgdiskPath);
435 cmd.push_back("--new=0:0:-0");
436 cmd.push_back("--typecode=0:0c00");
437 cmd.push_back("--gpttombr=1");
438 cmd.push_back(mDevPath);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800439
Jeff Sharkey68f1b8b2017-10-18 14:09:52 -0600440 if ((res = ForkExecvp(cmd)) != 0) {
441 LOG(ERROR) << "Failed to partition; status " << res;
442 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800443 }
444
Jeff Sharkey68f1b8b2017-10-18 14:09:52 -0600445 return OK;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800446}
447
448status_t Disk::partitionPrivate() {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700449 return partitionMixed(0);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800450}
451
452status_t Disk::partitionMixed(int8_t ratio) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700453 int res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700454
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700455 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700456 mJustPartitioned = true;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700457
458 // First nuke any existing partition table
459 std::vector<std::string> cmd;
460 cmd.push_back(kSgdiskPath);
461 cmd.push_back("--zap-all");
462 cmd.push_back(mDevPath);
463
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700464 // Zap sometimes returns an error when it actually succeeded, so
465 // just log as warning and keep rolling forward.
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700466 if ((res = ForkExecvp(cmd)) != 0) {
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700467 LOG(WARNING) << "Failed to zap; status " << res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700468 }
469
470 // We've had some success above, so generate both the private partition
471 // GUID and encryption key and persist them.
472 std::string partGuidRaw;
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600473 if (GenerateRandomUuid(partGuidRaw) != OK) {
474 LOG(ERROR) << "Failed to generate GUID";
475 return -EIO;
476 }
477
Paul Crowley4eac2642020-02-12 11:04:05 -0800478 KeyBuffer key;
Paul Crowley886e5722020-02-07 12:51:56 -0800479 if (!generate_volume_key(&key)) {
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600480 LOG(ERROR) << "Failed to generate key";
Jeff Sharkey9c484982015-03-31 10:35:33 -0700481 return -EIO;
482 }
Paul Crowley4eac2642020-02-12 11:04:05 -0800483 std::string keyRaw(key.begin(), key.end());
Jeff Sharkey9c484982015-03-31 10:35:33 -0700484
485 std::string partGuid;
486 StrToHex(partGuidRaw, partGuid);
487
488 if (!WriteStringToFile(keyRaw, BuildKeyPath(partGuid))) {
489 LOG(ERROR) << "Failed to persist key";
490 return -EIO;
491 } else {
492 LOG(DEBUG) << "Persisted key for GUID " << partGuid;
493 }
494
495 // Now let's build the new GPT table. We heavily rely on sgdisk to
496 // force optimal alignment on the created partitions.
497 cmd.clear();
498 cmd.push_back(kSgdiskPath);
499
500 // If requested, create a public partition first. Mixed-mode partitioning
501 // like this is an experimental feature.
502 if (ratio > 0) {
503 if (ratio < 10 || ratio > 90) {
504 LOG(ERROR) << "Mixed partition ratio must be between 10-90%";
505 return -EINVAL;
506 }
507
508 uint64_t splitMb = ((mSize / 100) * ratio) / 1024 / 1024;
509 cmd.push_back(StringPrintf("--new=0:0:+%" PRId64 "M", splitMb));
510 cmd.push_back(StringPrintf("--typecode=0:%s", kGptBasicData));
511 cmd.push_back("--change-name=0:shared");
512 }
513
514 // Define a metadata partition which is designed for future use; there
515 // should only be one of these per physical device, even if there are
516 // multiple private volumes.
517 cmd.push_back("--new=0:0:+16M");
518 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidMeta));
519 cmd.push_back("--change-name=0:android_meta");
520
521 // Define a single private partition filling the rest of disk.
522 cmd.push_back("--new=0:0:-0");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700523 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidExpand));
Jeff Sharkey9c484982015-03-31 10:35:33 -0700524 cmd.push_back(StringPrintf("--partition-guid=0:%s", partGuid.c_str()));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700525 cmd.push_back("--change-name=0:android_expand");
Jeff Sharkey9c484982015-03-31 10:35:33 -0700526
527 cmd.push_back(mDevPath);
528
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700529 if ((res = ForkExecvp(cmd)) != 0) {
530 LOG(ERROR) << "Failed to partition; status " << res;
531 return res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700532 }
533
534 return OK;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800535}
536
537int Disk::getMaxMinors() {
538 // Figure out maximum partition devices supported
Yu Ning942d4e82016-01-08 17:36:47 +0800539 unsigned int majorId = major(mDevice);
540 switch (majorId) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700541 case kMajorBlockLoop: {
542 std::string tmp;
543 if (!ReadFileToString(kSysfsLoopMaxMinors, &tmp)) {
544 LOG(ERROR) << "Failed to read max minors";
545 return -errno;
546 }
547 return std::stoi(tmp);
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600548 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700549 // clang-format off
550 case kMajorBlockScsiA: case kMajorBlockScsiB: case kMajorBlockScsiC:
551 case kMajorBlockScsiD: case kMajorBlockScsiE: case kMajorBlockScsiF:
552 case kMajorBlockScsiG: case kMajorBlockScsiH: case kMajorBlockScsiI:
553 case kMajorBlockScsiJ: case kMajorBlockScsiK: case kMajorBlockScsiL:
554 case kMajorBlockScsiM: case kMajorBlockScsiN: case kMajorBlockScsiO:
555 case kMajorBlockScsiP: {
556 // clang-format on
557 // Per Documentation/devices.txt this is static
Yu Ning942d4e82016-01-08 17:36:47 +0800558 return 15;
559 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700560 case kMajorBlockMmc: {
561 // Per Documentation/devices.txt this is dynamic
562 std::string tmp;
563 if (!ReadFileToString(kSysfsMmcMaxMinors, &tmp) &&
564 !ReadFileToString(kSysfsMmcMaxMinorsDeprecated, &tmp)) {
565 LOG(ERROR) << "Failed to read max minors";
566 return -errno;
567 }
568 return std::stoi(tmp);
Dmitry Shmidt06dc6e52018-05-11 17:22:42 -0700569 }
Paul Crowley14c8c072018-09-18 13:30:21 -0700570 default: {
Alistair Delvad3c230b2020-05-14 16:35:03 -0700571 if (IsVirtioBlkDevice(majorId)) {
Paul Crowley14c8c072018-09-18 13:30:21 -0700572 // drivers/block/virtio_blk.c has "#define PART_BITS 4", so max is
573 // 2^4 - 1 = 15
574 return 15;
575 }
576 if (isNvmeBlkDevice(majorId, mSysPath)) {
577 // despite kernel nvme driver supports up to 1M minors,
578 // #define NVME_MINORS (1U << MINORBITS)
579 // sgdisk can not support more than 127 partitions, due to
580 // #define MAX_MBR_PARTS 128
581 return 127;
582 }
583 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800584 }
585
Yu Ning942d4e82016-01-08 17:36:47 +0800586 LOG(ERROR) << "Unsupported block major type " << majorId;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800587 return -ENOTSUP;
588}
589
590} // namespace vold
591} // namespace android