blob: b8a0cbe60982a9c6e9e7167f62b351f55919d257 [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"
18#include "PublicVolume.h"
Jeff Sharkey9c484982015-03-31 10:35:33 -070019#include "PrivateVolume.h"
Jeff Sharkeydeb24052015-03-02 21:01:40 -080020#include "Utils.h"
21#include "VolumeBase.h"
Jeff Sharkey36801cc2015-03-13 16:09:20 -070022#include "VolumeManager.h"
Jeff Sharkey15717512016-08-23 13:48:50 -060023#include "Ext4Crypt.h"
Jeff Sharkeydeb24052015-03-02 21:01:40 -080024
Elliott Hughes7e128fb2015-12-04 15:50:53 -080025#include <android-base/file.h>
Paul Crowley3b71fc52017-10-09 10:55:21 -070026#include <android-base/logging.h>
Jeff Sharkey46bb69f2017-06-21 13:52:23 -060027#include <android-base/properties.h>
Elliott Hughes7e128fb2015-12-04 15:50:53 -080028#include <android-base/stringprintf.h>
Jeff Sharkey3472e522017-10-06 18:02:53 -060029#include <android-base/strings.h>
30#include <android-base/parseint.h>
Paul Crowley3b71fc52017-10-09 10:55:21 -070031#include <ext4_utils/ext4_crypt.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080032
Greg Kaiser57f9af62018-02-16 13:13:58 -080033#include "cryptfs.h"
34
Jeff Sharkey9c484982015-03-31 10:35:33 -070035#include <vector>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080036#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>
40#include <sys/types.h>
41#include <sys/stat.h>
Elliott Hughes0e08e842017-05-18 09:08:24 -070042#include <sys/sysmacros.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080043#include <sys/mount.h>
44
Dan Albertae9e8902015-03-16 10:35:17 -070045using android::base::ReadFileToString;
Jeff Sharkey9c484982015-03-31 10:35:33 -070046using android::base::WriteStringToFile;
Dan Albertae9e8902015-03-16 10:35:17 -070047using android::base::StringPrintf;
48
Jeff Sharkeydeb24052015-03-02 21:01:40 -080049namespace android {
50namespace vold {
51
52static const char* kSgdiskPath = "/system/bin/sgdisk";
53static const char* kSgdiskToken = " \t\n";
54
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060055static const char* kSysfsLoopMaxMinors = "/sys/module/loop/parameters/max_part";
Pierre-Hugues Hussonf347cd02017-11-28 15:42:56 +010056static const char* kSysfsMmcMaxMinorsDeprecated = "/sys/module/mmcblk/parameters/perdev_minors";
57static const char* kSysfsMmcMaxMinors = "/sys/module/mmc_block/parameters/perdev_minors";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080058
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060059static const unsigned int kMajorBlockLoop = 7;
Jeff Sharkeyf3ee2002015-04-19 15:55:42 -070060static const unsigned int kMajorBlockScsiA = 8;
61static const unsigned int kMajorBlockScsiB = 65;
62static const unsigned int kMajorBlockScsiC = 66;
63static const unsigned int kMajorBlockScsiD = 67;
64static const unsigned int kMajorBlockScsiE = 68;
65static const unsigned int kMajorBlockScsiF = 69;
66static const unsigned int kMajorBlockScsiG = 70;
67static const unsigned int kMajorBlockScsiH = 71;
68static const unsigned int kMajorBlockScsiI = 128;
69static const unsigned int kMajorBlockScsiJ = 129;
70static const unsigned int kMajorBlockScsiK = 130;
71static const unsigned int kMajorBlockScsiL = 131;
72static const unsigned int kMajorBlockScsiM = 132;
73static const unsigned int kMajorBlockScsiN = 133;
74static const unsigned int kMajorBlockScsiO = 134;
75static const unsigned int kMajorBlockScsiP = 135;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080076static const unsigned int kMajorBlockMmc = 179;
Yu Ning942d4e82016-01-08 17:36:47 +080077static const unsigned int kMajorBlockExperimentalMin = 240;
78static const unsigned int kMajorBlockExperimentalMax = 254;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080079
80static const char* kGptBasicData = "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7";
lambdadroid9d8ce082018-01-06 14:48:44 +010081static const char* kGptLinuxFilesystem = "0FC63DAF-8483-4772-8E79-3D69D8477DE4";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080082static 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
Yu Ning942d4e82016-01-08 17:36:47 +080091static bool isVirtioBlkDevice(unsigned int major) {
92 /*
93 * The new emulator's "ranchu" virtual board no longer includes a goldfish
94 * MMC-based SD card device; instead, it emulates SD cards with virtio-blk,
95 * which has been supported by upstream kernel and QEMU for quite a while.
96 * Unfortunately, the virtio-blk block device driver does not use a fixed
97 * major number, but relies on the kernel to assign one from a specific
98 * range of block majors, which are allocated for "LOCAL/EXPERIMENAL USE"
99 * per Documentation/devices.txt. This is true even for the latest Linux
100 * kernel (4.4; see init() in drivers/block/virtio_blk.c).
101 *
102 * This makes it difficult for vold to detect a virtio-blk based SD card.
103 * The current solution checks two conditions (both must be met):
104 *
105 * a) If the running environment is the emulator;
106 * b) If the major number is an experimental block device major number (for
107 * x86/x86_64 3.10 ranchu kernels, virtio-blk always gets major number
108 * 253, but it is safer to match the range than just one value).
109 *
110 * Other conditions could be used, too, e.g. the hardware name should be
111 * "ranchu", the device's sysfs path should end with "/block/vd[d-z]", etc.
112 * But just having a) and b) is enough for now.
113 */
114 return IsRunningInEmulator() && major >= kMajorBlockExperimentalMin
115 && major <= kMajorBlockExperimentalMax;
116}
117
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700118Disk::Disk(const std::string& eventPath, dev_t device,
119 const std::string& nickname, int flags) :
120 mDevice(device), mSize(-1), mNickname(nickname), mFlags(flags), mCreated(
Scott Mertzfadb5452016-03-31 13:09:56 -0700121 false), mJustPartitioned(false), mSkipChange(false) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700122 mId = StringPrintf("disk:%u,%u", major(device), minor(device));
123 mEventPath = eventPath;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800124 mSysPath = StringPrintf("/sys/%s", eventPath.c_str());
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700125 mDevPath = StringPrintf("/dev/block/vold/%s", mId.c_str());
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800126 CreateDeviceNode(mDevPath, mDevice);
127}
128
129Disk::~Disk() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700130 CHECK(!mCreated);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800131 DestroyDeviceNode(mDevPath);
132}
133
134std::shared_ptr<VolumeBase> Disk::findVolume(const std::string& id) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700135 for (auto vol : mVolumes) {
136 if (vol->getId() == id) {
137 return vol;
138 }
139 auto stackedVol = vol->findVolume(id);
140 if (stackedVol != nullptr) {
141 return stackedVol;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800142 }
143 }
144 return nullptr;
145}
146
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700147void Disk::listVolumes(VolumeBase::Type type, std::list<std::string>& list) {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700148 for (const auto& vol : mVolumes) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700149 if (vol->getType() == type) {
150 list.push_back(vol->getId());
151 }
152 // TODO: consider looking at stacked volumes
153 }
154}
155
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700156status_t Disk::create() {
157 CHECK(!mCreated);
158 mCreated = true;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600159
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600160 auto listener = VolumeManager::Instance()->getListener();
161 if (listener) listener->onDiskCreated(getId(), mFlags);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600162
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700163 readMetadata();
164 readPartitions();
165 return OK;
166}
167
168status_t Disk::destroy() {
169 CHECK(mCreated);
170 destroyAllVolumes();
171 mCreated = false;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600172
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600173 auto listener = VolumeManager::Instance()->getListener();
174 if (listener) listener->onDiskDestroyed(getId());
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600175
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700176 return OK;
177}
178
Tom Marshalld18795d2015-11-05 11:20:54 -0800179void Disk::createPublicVolume(dev_t device,
180 const std::string& fstype /* = "" */,
181 const std::string& mntopts /* = "" */) {
182 auto vol = std::shared_ptr<VolumeBase>(new PublicVolume(device, fstype, mntopts));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700183 if (mJustPartitioned) {
184 LOG(DEBUG) << "Device just partitioned; silently formatting";
185 vol->setSilent(true);
186 vol->create();
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700187 vol->format("auto");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700188 vol->destroy();
189 vol->setSilent(false);
190 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700191
Jeff Sharkey9c484982015-03-31 10:35:33 -0700192 mVolumes.push_back(vol);
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700193 vol->setDiskId(getId());
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700194 vol->create();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700195}
196
Jeff Sharkey9c484982015-03-31 10:35:33 -0700197void Disk::createPrivateVolume(dev_t device, const std::string& partGuid) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700198 std::string normalizedGuid;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700199 if (NormalizeHex(partGuid, normalizedGuid)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700200 LOG(WARNING) << "Invalid GUID " << partGuid;
201 return;
202 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700203
204 std::string keyRaw;
205 if (!ReadFileToString(BuildKeyPath(normalizedGuid), &keyRaw)) {
206 PLOG(ERROR) << "Failed to load key for GUID " << normalizedGuid;
207 return;
208 }
209
210 LOG(DEBUG) << "Found key for GUID " << normalizedGuid;
211
212 auto vol = std::shared_ptr<VolumeBase>(new PrivateVolume(device, keyRaw));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700213 if (mJustPartitioned) {
214 LOG(DEBUG) << "Device just partitioned; silently formatting";
215 vol->setSilent(true);
216 vol->create();
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700217 vol->format("auto");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700218 vol->destroy();
219 vol->setSilent(false);
220 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700221
222 mVolumes.push_back(vol);
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700223 vol->setDiskId(getId());
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700224 vol->setPartGuid(partGuid);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700225 vol->create();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700226}
227
228void Disk::destroyAllVolumes() {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700229 for (const auto& vol : mVolumes) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700230 vol->destroy();
231 }
232 mVolumes.clear();
233}
234
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800235status_t Disk::readMetadata() {
Scott Mertzfadb5452016-03-31 13:09:56 -0700236
237 if (mSkipChange) {
238 return OK;
239 }
240
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800241 mSize = -1;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700242 mLabel.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800243
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700244 int fd = open(mDevPath.c_str(), O_RDONLY | O_CLOEXEC);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700245 if (fd != -1) {
246 if (ioctl(fd, BLKGETSIZE64, &mSize)) {
247 mSize = -1;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800248 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700249 close(fd);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800250 }
251
Yu Ning942d4e82016-01-08 17:36:47 +0800252 unsigned int majorId = major(mDevice);
253 switch (majorId) {
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600254 case kMajorBlockLoop: {
255 mLabel = "Virtual";
256 break;
257 }
Jeff Sharkeyf3ee2002015-04-19 15:55:42 -0700258 case kMajorBlockScsiA: case kMajorBlockScsiB: case kMajorBlockScsiC: case kMajorBlockScsiD:
259 case kMajorBlockScsiE: case kMajorBlockScsiF: case kMajorBlockScsiG: case kMajorBlockScsiH:
260 case kMajorBlockScsiI: case kMajorBlockScsiJ: case kMajorBlockScsiK: case kMajorBlockScsiL:
261 case kMajorBlockScsiM: case kMajorBlockScsiN: case kMajorBlockScsiO: case kMajorBlockScsiP: {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800262 std::string path(mSysPath + "/device/vendor");
263 std::string tmp;
264 if (!ReadFileToString(path, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700265 PLOG(WARNING) << "Failed to read vendor from " << path;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800266 return -errno;
267 }
Jeff Sharkeye50314d2018-02-24 18:23:35 -0700268 tmp = android::base::Trim(tmp);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800269 mLabel = tmp;
270 break;
271 }
272 case kMajorBlockMmc: {
273 std::string path(mSysPath + "/device/manfid");
274 std::string tmp;
275 if (!ReadFileToString(path, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700276 PLOG(WARNING) << "Failed to read manufacturer from " << path;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800277 return -errno;
278 }
Jeff Sharkeye50314d2018-02-24 18:23:35 -0700279 tmp = android::base::Trim(tmp);
Jeff Sharkey3472e522017-10-06 18:02:53 -0600280 int64_t manfid;
281 if (!android::base::ParseInt(tmp, &manfid)) {
282 PLOG(WARNING) << "Failed to parse manufacturer " << tmp;
283 return -EINVAL;
284 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800285 // Our goal here is to give the user a meaningful label, ideally
286 // matching whatever is silk-screened on the card. To reduce
287 // user confusion, this list doesn't contain white-label manfid.
288 switch (manfid) {
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 }
294 break;
295 }
296 default: {
Yu Ning942d4e82016-01-08 17:36:47 +0800297 if (isVirtioBlkDevice(majorId)) {
298 LOG(DEBUG) << "Recognized experimental block major ID " << majorId
299 << " as virtio-blk (emulator's virtual SD card device)";
300 mLabel = "Virtual";
301 break;
302 }
303 LOG(WARNING) << "Unsupported block major type " << majorId;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800304 return -ENOTSUP;
305 }
306 }
307
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600308 auto listener = VolumeManager::Instance()->getListener();
309 if (listener) listener->onDiskMetadataChanged(getId(),
310 mSize, mLabel, mSysPath);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600311
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800312 return OK;
313}
314
315status_t Disk::readPartitions() {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600316 int maxMinors = getMaxMinors();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800317 if (maxMinors < 0) {
318 return -ENOTSUP;
319 }
320
Scott Mertzfadb5452016-03-31 13:09:56 -0700321 if (mSkipChange) {
322 mSkipChange = false;
323 LOG(INFO) << "Skip first change";
324 return OK;
325 }
326
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700327 destroyAllVolumes();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800328
329 // Parse partition table
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700330
331 std::vector<std::string> cmd;
332 cmd.push_back(kSgdiskPath);
333 cmd.push_back("--android-dump");
334 cmd.push_back(mDevPath);
335
336 std::vector<std::string> output;
337 status_t res = ForkExecvp(cmd, output);
338 if (res != OK) {
339 LOG(WARNING) << "sgdisk failed to scan " << mDevPath;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600340
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600341 auto listener = VolumeManager::Instance()->getListener();
342 if (listener) listener->onDiskScanned(getId());
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600343
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700344 mJustPartitioned = false;
345 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800346 }
347
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800348 Table table = Table::kUnknown;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700349 bool foundParts = false;
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700350 for (const auto& line : output) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600351 auto split = android::base::Split(line, kSgdiskToken);
352 auto it = split.begin();
353 if (it == split.end()) continue;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700354
Jeff Sharkey3472e522017-10-06 18:02:53 -0600355 if (*it == "DISK") {
356 if (++it == split.end()) continue;
357 if (*it == "mbr") {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800358 table = Table::kMbr;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600359 } else if (*it == "gpt") {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800360 table = Table::kGpt;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600361 } else {
362 LOG(WARNING) << "Invalid partition table " << *it;
363 continue;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800364 }
Jeff Sharkey3472e522017-10-06 18:02:53 -0600365 } else if (*it == "PART") {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700366 foundParts = true;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600367
368 if (++it == split.end()) continue;
369 int i = 0;
370 if (!android::base::ParseInt(*it, &i, 1, maxMinors)) {
371 LOG(WARNING) << "Invalid partition number " << *it;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800372 continue;
373 }
374 dev_t partDevice = makedev(major(mDevice), minor(mDevice) + i);
375
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800376 if (table == Table::kMbr) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600377 if (++it == split.end()) continue;
378 int type = 0;
379 if (!android::base::ParseInt("0x" + *it, &type)) {
380 LOG(WARNING) << "Invalid partition type " << *it;
381 continue;
382 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800383
Jeff Sharkey3472e522017-10-06 18:02:53 -0600384 switch (type) {
Jeff Sharkey37ba1252018-01-19 10:55:18 +0900385 case 0x06: // FAT16
386 case 0x07: // HPFS/NTFS/exFAT
387 case 0x0b: // W95 FAT32 (LBA)
388 case 0x0c: // W95 FAT32 (LBA)
389 case 0x0e: // W95 FAT16 (LBA)
Dan Pasanend1952662015-10-27 22:52:37 -0500390 case 0x83: // Linux EXT4/F2FS/...
Jeff Sharkey37ba1252018-01-19 10:55:18 +0900391 createPublicVolume(partDevice);
392 break;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800393 }
394 } else if (table == Table::kGpt) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600395 if (++it == split.end()) continue;
396 auto typeGuid = *it;
397 if (++it == split.end()) continue;
398 auto partGuid = *it;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800399
lambdadroid9d8ce082018-01-06 14:48:44 +0100400 if (android::base::EqualsIgnoreCase(typeGuid, kGptBasicData)
401 || android::base::EqualsIgnoreCase(typeGuid, kGptLinuxFilesystem)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700402 createPublicVolume(partDevice);
Jeff Sharkey3472e522017-10-06 18:02:53 -0600403 } else if (android::base::EqualsIgnoreCase(typeGuid, kGptAndroidExpand)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700404 createPrivateVolume(partDevice, partGuid);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800405 }
406 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800407 }
408 }
409
410 // Ugly last ditch effort, treat entire disk as partition
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700411 if (table == Table::kUnknown || !foundParts) {
412 LOG(WARNING) << mId << " has unknown partition table; trying entire device";
Jeff Sharkey63123c02015-06-26 11:16:14 -0700413
414 std::string fsType;
415 std::string unused;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600416 if (ReadMetadataUntrusted(mDevPath, &fsType, &unused, &unused) == OK) {
Jeff Sharkey63123c02015-06-26 11:16:14 -0700417 createPublicVolume(mDevice);
418 } else {
419 LOG(WARNING) << mId << " failed to identify, giving up";
420 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800421 }
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700422
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600423 auto listener = VolumeManager::Instance()->getListener();
424 if (listener) listener->onDiskScanned(getId());
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600425
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700426 mJustPartitioned = false;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800427 return OK;
428}
429
Jeff Sharkey9c484982015-03-31 10:35:33 -0700430status_t Disk::unmountAll() {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700431 for (const auto& vol : mVolumes) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700432 vol->unmount();
433 }
434 return OK;
435}
436
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800437status_t Disk::partitionPublic() {
Jeff Sharkeydadccee2015-09-23 14:13:45 -0700438 int res;
439
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700440 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700441 mJustPartitioned = true;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800442
Scott Mertzfadb5452016-03-31 13:09:56 -0700443 // Determine if we're coming from MBR
Jeff Sharkeydadccee2015-09-23 14:13:45 -0700444 std::vector<std::string> cmd;
445 cmd.push_back(kSgdiskPath);
Scott Mertzfadb5452016-03-31 13:09:56 -0700446 cmd.push_back("--android-dump");
447 cmd.push_back(mDevPath);
448
449 std::vector<std::string> output;
450 res = ForkExecvp(cmd, output);
451 if (res != OK) {
452 LOG(WARNING) << "sgdisk failed to scan " << mDevPath;
453 mJustPartitioned = false;
454 return res;
455 }
456
457 Table table = Table::kUnknown;
458 for (auto line : output) {
459 char* cline = (char*) line.c_str();
460 char* token = strtok(cline, kSgdiskToken);
461 if (token == nullptr) continue;
462
463 if (!strcmp(token, "DISK")) {
464 const char* type = strtok(nullptr, kSgdiskToken);
465 if (!strcmp(type, "mbr")) {
466 table = Table::kMbr;
467 break;
468 } else if (!strcmp(type, "gpt")) {
469 table = Table::kGpt;
470 break;
471 }
472 }
473 }
474
475 if (table == Table::kMbr) {
476 LOG(INFO) << "skip first disk change event due to MBR -> GPT switch";
477 mSkipChange = true;
478 }
479
480 // First nuke any existing partition table
481 cmd.clear();
482 cmd.push_back(kSgdiskPath);
Jeff Sharkeydadccee2015-09-23 14:13:45 -0700483 cmd.push_back("--zap-all");
484 cmd.push_back(mDevPath);
485
486 // Zap sometimes returns an error when it actually succeeded, so
487 // just log as warning and keep rolling forward.
488 if ((res = ForkExecvp(cmd)) != 0) {
489 LOG(WARNING) << "Failed to zap; status " << res;
490 }
491
Jeff Sharkey68f1b8b2017-10-18 14:09:52 -0600492 // Now let's build the new MBR table. We heavily rely on sgdisk to
493 // force optimal alignment on the created partitions.
494 cmd.clear();
495 cmd.push_back(kSgdiskPath);
496 cmd.push_back("--new=0:0:-0");
497 cmd.push_back("--typecode=0:0c00");
498 cmd.push_back("--gpttombr=1");
499 cmd.push_back(mDevPath);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800500
Jeff Sharkey68f1b8b2017-10-18 14:09:52 -0600501 if ((res = ForkExecvp(cmd)) != 0) {
502 LOG(ERROR) << "Failed to partition; status " << res;
503 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800504 }
505
Jeff Sharkey68f1b8b2017-10-18 14:09:52 -0600506 return OK;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800507}
508
509status_t Disk::partitionPrivate() {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700510 return partitionMixed(0);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800511}
512
513status_t Disk::partitionMixed(int8_t ratio) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700514 int res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700515
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700516 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700517 mJustPartitioned = true;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700518
519 // First nuke any existing partition table
520 std::vector<std::string> cmd;
521 cmd.push_back(kSgdiskPath);
522 cmd.push_back("--zap-all");
523 cmd.push_back(mDevPath);
524
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700525 // Zap sometimes returns an error when it actually succeeded, so
526 // just log as warning and keep rolling forward.
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700527 if ((res = ForkExecvp(cmd)) != 0) {
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700528 LOG(WARNING) << "Failed to zap; status " << res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700529 }
530
531 // We've had some success above, so generate both the private partition
532 // GUID and encryption key and persist them.
533 std::string partGuidRaw;
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600534 if (GenerateRandomUuid(partGuidRaw) != OK) {
535 LOG(ERROR) << "Failed to generate GUID";
536 return -EIO;
537 }
538
Jeff Sharkey9c484982015-03-31 10:35:33 -0700539 std::string keyRaw;
Greg Kaiser57f9af62018-02-16 13:13:58 -0800540 if (ReadRandomBytes(cryptfs_get_keysize(), keyRaw) != OK) {
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600541 LOG(ERROR) << "Failed to generate key";
Jeff Sharkey9c484982015-03-31 10:35:33 -0700542 return -EIO;
543 }
544
545 std::string partGuid;
546 StrToHex(partGuidRaw, partGuid);
547
548 if (!WriteStringToFile(keyRaw, BuildKeyPath(partGuid))) {
549 LOG(ERROR) << "Failed to persist key";
550 return -EIO;
551 } else {
552 LOG(DEBUG) << "Persisted key for GUID " << partGuid;
553 }
554
555 // Now let's build the new GPT table. We heavily rely on sgdisk to
556 // force optimal alignment on the created partitions.
557 cmd.clear();
558 cmd.push_back(kSgdiskPath);
559
560 // If requested, create a public partition first. Mixed-mode partitioning
561 // like this is an experimental feature.
562 if (ratio > 0) {
563 if (ratio < 10 || ratio > 90) {
564 LOG(ERROR) << "Mixed partition ratio must be between 10-90%";
565 return -EINVAL;
566 }
567
568 uint64_t splitMb = ((mSize / 100) * ratio) / 1024 / 1024;
569 cmd.push_back(StringPrintf("--new=0:0:+%" PRId64 "M", splitMb));
570 cmd.push_back(StringPrintf("--typecode=0:%s", kGptBasicData));
571 cmd.push_back("--change-name=0:shared");
572 }
573
574 // Define a metadata partition which is designed for future use; there
575 // should only be one of these per physical device, even if there are
576 // multiple private volumes.
577 cmd.push_back("--new=0:0:+16M");
578 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidMeta));
579 cmd.push_back("--change-name=0:android_meta");
580
581 // Define a single private partition filling the rest of disk.
582 cmd.push_back("--new=0:0:-0");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700583 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidExpand));
Jeff Sharkey9c484982015-03-31 10:35:33 -0700584 cmd.push_back(StringPrintf("--partition-guid=0:%s", partGuid.c_str()));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700585 cmd.push_back("--change-name=0:android_expand");
Jeff Sharkey9c484982015-03-31 10:35:33 -0700586
587 cmd.push_back(mDevPath);
588
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700589 if ((res = ForkExecvp(cmd)) != 0) {
590 LOG(ERROR) << "Failed to partition; status " << res;
591 return res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700592 }
593
594 return OK;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800595}
596
597int Disk::getMaxMinors() {
598 // Figure out maximum partition devices supported
Yu Ning942d4e82016-01-08 17:36:47 +0800599 unsigned int majorId = major(mDevice);
600 switch (majorId) {
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600601 case kMajorBlockLoop: {
602 std::string tmp;
603 if (!ReadFileToString(kSysfsLoopMaxMinors, &tmp)) {
604 LOG(ERROR) << "Failed to read max minors";
605 return -errno;
606 }
Jeff Sharkey95440eb2017-09-18 18:19:28 -0600607 return std::stoi(tmp);
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600608 }
Jeff Sharkeyf3ee2002015-04-19 15:55:42 -0700609 case kMajorBlockScsiA: case kMajorBlockScsiB: case kMajorBlockScsiC: case kMajorBlockScsiD:
610 case kMajorBlockScsiE: case kMajorBlockScsiF: case kMajorBlockScsiG: case kMajorBlockScsiH:
611 case kMajorBlockScsiI: case kMajorBlockScsiJ: case kMajorBlockScsiK: case kMajorBlockScsiL:
612 case kMajorBlockScsiM: case kMajorBlockScsiN: case kMajorBlockScsiO: case kMajorBlockScsiP: {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800613 // Per Documentation/devices.txt this is static
614 return 15;
615 }
616 case kMajorBlockMmc: {
617 // Per Documentation/devices.txt this is dynamic
618 std::string tmp;
Pierre-Hugues Hussonf347cd02017-11-28 15:42:56 +0100619 if (!ReadFileToString(kSysfsMmcMaxMinors, &tmp) &&
620 !ReadFileToString(kSysfsMmcMaxMinorsDeprecated, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700621 LOG(ERROR) << "Failed to read max minors";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800622 return -errno;
623 }
Jeff Sharkey95440eb2017-09-18 18:19:28 -0600624 return std::stoi(tmp);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800625 }
Yu Ning942d4e82016-01-08 17:36:47 +0800626 default: {
627 if (isVirtioBlkDevice(majorId)) {
628 // drivers/block/virtio_blk.c has "#define PART_BITS 4", so max is
629 // 2^4 - 1 = 15
630 return 15;
631 }
632 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800633 }
634
Yu Ning942d4e82016-01-08 17:36:47 +0800635 LOG(ERROR) << "Unsupported block major type " << majorId;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800636 return -ENOTSUP;
637}
638
639} // namespace vold
640} // namespace android