blob: 781d3e90534316bb552238c5d3c89573606f759c [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;
Dmitry Shmidt06dc6e52018-05-11 17:22:42 -070079static const unsigned int kMajorBlockDynamicMin = 234;
80static const unsigned int kMajorBlockDynamicMax = 512;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080081
82static const char* kGptBasicData = "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7";
83static const char* kGptAndroidMeta = "19A710A2-B3CA-11E4-B026-10604B889DCF";
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070084static const char* kGptAndroidExpand = "193D1EA4-B3CA-11E4-B075-10604B889DCF";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080085
86enum class Table {
87 kUnknown,
88 kMbr,
89 kGpt,
90};
91
Yu Ning942d4e82016-01-08 17:36:47 +080092static bool isVirtioBlkDevice(unsigned int major) {
93 /*
94 * The new emulator's "ranchu" virtual board no longer includes a goldfish
95 * MMC-based SD card device; instead, it emulates SD cards with virtio-blk,
96 * which has been supported by upstream kernel and QEMU for quite a while.
97 * Unfortunately, the virtio-blk block device driver does not use a fixed
98 * major number, but relies on the kernel to assign one from a specific
99 * range of block majors, which are allocated for "LOCAL/EXPERIMENAL USE"
100 * per Documentation/devices.txt. This is true even for the latest Linux
101 * kernel (4.4; see init() in drivers/block/virtio_blk.c).
102 *
103 * This makes it difficult for vold to detect a virtio-blk based SD card.
104 * The current solution checks two conditions (both must be met):
105 *
106 * a) If the running environment is the emulator;
107 * b) If the major number is an experimental block device major number (for
108 * x86/x86_64 3.10 ranchu kernels, virtio-blk always gets major number
109 * 253, but it is safer to match the range than just one value).
110 *
111 * Other conditions could be used, too, e.g. the hardware name should be
112 * "ranchu", the device's sysfs path should end with "/block/vd[d-z]", etc.
113 * But just having a) and b) is enough for now.
114 */
115 return IsRunningInEmulator() && major >= kMajorBlockExperimentalMin
116 && major <= kMajorBlockExperimentalMax;
117}
118
Dmitry Shmidt06dc6e52018-05-11 17:22:42 -0700119static bool isNvmeBlkDevice(unsigned int major, const std::string& sysPath) {
120 return sysPath.find("nvme") != std::string::npos
121 && major >= kMajorBlockDynamicMin
122 && major <= kMajorBlockDynamicMax;
123}
124
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700125Disk::Disk(const std::string& eventPath, dev_t device,
126 const std::string& nickname, int flags) :
127 mDevice(device), mSize(-1), mNickname(nickname), mFlags(flags), mCreated(
128 false), mJustPartitioned(false) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700129 mId = StringPrintf("disk:%u,%u", major(device), minor(device));
130 mEventPath = eventPath;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800131 mSysPath = StringPrintf("/sys/%s", eventPath.c_str());
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700132 mDevPath = StringPrintf("/dev/block/vold/%s", mId.c_str());
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800133 CreateDeviceNode(mDevPath, mDevice);
134}
135
136Disk::~Disk() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700137 CHECK(!mCreated);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800138 DestroyDeviceNode(mDevPath);
139}
140
141std::shared_ptr<VolumeBase> Disk::findVolume(const std::string& id) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700142 for (auto vol : mVolumes) {
143 if (vol->getId() == id) {
144 return vol;
145 }
146 auto stackedVol = vol->findVolume(id);
147 if (stackedVol != nullptr) {
148 return stackedVol;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800149 }
150 }
151 return nullptr;
152}
153
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700154void Disk::listVolumes(VolumeBase::Type type, std::list<std::string>& list) {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700155 for (const auto& vol : mVolumes) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700156 if (vol->getType() == type) {
157 list.push_back(vol->getId());
158 }
159 // TODO: consider looking at stacked volumes
160 }
161}
162
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700163status_t Disk::create() {
164 CHECK(!mCreated);
165 mCreated = true;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600166
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600167 auto listener = VolumeManager::Instance()->getListener();
168 if (listener) listener->onDiskCreated(getId(), mFlags);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600169
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700170 readMetadata();
171 readPartitions();
172 return OK;
173}
174
175status_t Disk::destroy() {
176 CHECK(mCreated);
177 destroyAllVolumes();
178 mCreated = false;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600179
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600180 auto listener = VolumeManager::Instance()->getListener();
181 if (listener) listener->onDiskDestroyed(getId());
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600182
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700183 return OK;
184}
185
186void Disk::createPublicVolume(dev_t device) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700187 auto vol = std::shared_ptr<VolumeBase>(new PublicVolume(device));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700188 if (mJustPartitioned) {
189 LOG(DEBUG) << "Device just partitioned; silently formatting";
190 vol->setSilent(true);
191 vol->create();
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700192 vol->format("auto");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700193 vol->destroy();
194 vol->setSilent(false);
195 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700196
Jeff Sharkey9c484982015-03-31 10:35:33 -0700197 mVolumes.push_back(vol);
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700198 vol->setDiskId(getId());
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700199 vol->create();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700200}
201
Jeff Sharkey9c484982015-03-31 10:35:33 -0700202void Disk::createPrivateVolume(dev_t device, const std::string& partGuid) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700203 std::string normalizedGuid;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700204 if (NormalizeHex(partGuid, normalizedGuid)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700205 LOG(WARNING) << "Invalid GUID " << partGuid;
206 return;
207 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700208
209 std::string keyRaw;
210 if (!ReadFileToString(BuildKeyPath(normalizedGuid), &keyRaw)) {
211 PLOG(ERROR) << "Failed to load key for GUID " << normalizedGuid;
212 return;
213 }
214
215 LOG(DEBUG) << "Found key for GUID " << normalizedGuid;
216
217 auto vol = std::shared_ptr<VolumeBase>(new PrivateVolume(device, keyRaw));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700218 if (mJustPartitioned) {
219 LOG(DEBUG) << "Device just partitioned; silently formatting";
220 vol->setSilent(true);
221 vol->create();
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700222 vol->format("auto");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700223 vol->destroy();
224 vol->setSilent(false);
225 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700226
227 mVolumes.push_back(vol);
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700228 vol->setDiskId(getId());
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700229 vol->setPartGuid(partGuid);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700230 vol->create();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700231}
232
233void Disk::destroyAllVolumes() {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700234 for (const auto& vol : mVolumes) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700235 vol->destroy();
236 }
237 mVolumes.clear();
238}
239
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800240status_t Disk::readMetadata() {
241 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 }
Dmitry Shmidt06dc6e52018-05-11 17:22:42 -0700303 if (isNvmeBlkDevice(majorId, mSysPath)) {
304 std::string path(mSysPath + "/device/model");
305 std::string tmp;
306 if (!ReadFileToString(path, &tmp)) {
307 PLOG(WARNING) << "Failed to read vendor from " << path;
308 return -errno;
309 }
310 mLabel = tmp;
311 break;
312 }
Yu Ning942d4e82016-01-08 17:36:47 +0800313 LOG(WARNING) << "Unsupported block major type " << majorId;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800314 return -ENOTSUP;
315 }
316 }
317
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600318 auto listener = VolumeManager::Instance()->getListener();
319 if (listener) listener->onDiskMetadataChanged(getId(),
320 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;
341 status_t res = ForkExecvp(cmd, output);
342 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) {
Jeff Sharkey37ba1252018-01-19 10:55:18 +0900389 case 0x06: // FAT16
390 case 0x07: // HPFS/NTFS/exFAT
391 case 0x0b: // W95 FAT32 (LBA)
392 case 0x0c: // W95 FAT32 (LBA)
393 case 0x0e: // W95 FAT16 (LBA)
394 createPublicVolume(partDevice);
395 break;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800396 }
397 } else if (table == Table::kGpt) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600398 if (++it == split.end()) continue;
399 auto typeGuid = *it;
400 if (++it == split.end()) continue;
401 auto partGuid = *it;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800402
Jeff Sharkey3472e522017-10-06 18:02:53 -0600403 if (android::base::EqualsIgnoreCase(typeGuid, kGptBasicData)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700404 createPublicVolume(partDevice);
Jeff Sharkey3472e522017-10-06 18:02:53 -0600405 } else if (android::base::EqualsIgnoreCase(typeGuid, kGptAndroidExpand)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700406 createPrivateVolume(partDevice, partGuid);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800407 }
408 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800409 }
410 }
411
412 // Ugly last ditch effort, treat entire disk as partition
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700413 if (table == Table::kUnknown || !foundParts) {
414 LOG(WARNING) << mId << " has unknown partition table; trying entire device";
Jeff Sharkey63123c02015-06-26 11:16:14 -0700415
416 std::string fsType;
417 std::string unused;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600418 if (ReadMetadataUntrusted(mDevPath, &fsType, &unused, &unused) == OK) {
Jeff Sharkey63123c02015-06-26 11:16:14 -0700419 createPublicVolume(mDevice);
420 } else {
421 LOG(WARNING) << mId << " failed to identify, giving up";
422 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800423 }
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700424
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600425 auto listener = VolumeManager::Instance()->getListener();
426 if (listener) listener->onDiskScanned(getId());
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600427
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700428 mJustPartitioned = false;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800429 return OK;
430}
431
Jeff Sharkey9c484982015-03-31 10:35:33 -0700432status_t Disk::unmountAll() {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700433 for (const auto& vol : mVolumes) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700434 vol->unmount();
435 }
436 return OK;
437}
438
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800439status_t Disk::partitionPublic() {
Jeff Sharkeydadccee2015-09-23 14:13:45 -0700440 int res;
441
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700442 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700443 mJustPartitioned = true;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800444
Jeff Sharkeydadccee2015-09-23 14:13:45 -0700445 // First nuke any existing partition table
446 std::vector<std::string> cmd;
447 cmd.push_back(kSgdiskPath);
448 cmd.push_back("--zap-all");
449 cmd.push_back(mDevPath);
450
451 // Zap sometimes returns an error when it actually succeeded, so
452 // just log as warning and keep rolling forward.
453 if ((res = ForkExecvp(cmd)) != 0) {
454 LOG(WARNING) << "Failed to zap; status " << res;
455 }
456
Jeff Sharkey68f1b8b2017-10-18 14:09:52 -0600457 // Now let's build the new MBR table. We heavily rely on sgdisk to
458 // force optimal alignment on the created partitions.
459 cmd.clear();
460 cmd.push_back(kSgdiskPath);
461 cmd.push_back("--new=0:0:-0");
462 cmd.push_back("--typecode=0:0c00");
463 cmd.push_back("--gpttombr=1");
464 cmd.push_back(mDevPath);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800465
Jeff Sharkey68f1b8b2017-10-18 14:09:52 -0600466 if ((res = ForkExecvp(cmd)) != 0) {
467 LOG(ERROR) << "Failed to partition; status " << res;
468 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800469 }
470
Jeff Sharkey68f1b8b2017-10-18 14:09:52 -0600471 return OK;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800472}
473
474status_t Disk::partitionPrivate() {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700475 return partitionMixed(0);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800476}
477
478status_t Disk::partitionMixed(int8_t ratio) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700479 int res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700480
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700481 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700482 mJustPartitioned = true;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700483
484 // First nuke any existing partition table
485 std::vector<std::string> cmd;
486 cmd.push_back(kSgdiskPath);
487 cmd.push_back("--zap-all");
488 cmd.push_back(mDevPath);
489
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700490 // Zap sometimes returns an error when it actually succeeded, so
491 // just log as warning and keep rolling forward.
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700492 if ((res = ForkExecvp(cmd)) != 0) {
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700493 LOG(WARNING) << "Failed to zap; status " << res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700494 }
495
496 // We've had some success above, so generate both the private partition
497 // GUID and encryption key and persist them.
498 std::string partGuidRaw;
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600499 if (GenerateRandomUuid(partGuidRaw) != OK) {
500 LOG(ERROR) << "Failed to generate GUID";
501 return -EIO;
502 }
503
Jeff Sharkey9c484982015-03-31 10:35:33 -0700504 std::string keyRaw;
Greg Kaiser57f9af62018-02-16 13:13:58 -0800505 if (ReadRandomBytes(cryptfs_get_keysize(), keyRaw) != OK) {
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600506 LOG(ERROR) << "Failed to generate key";
Jeff Sharkey9c484982015-03-31 10:35:33 -0700507 return -EIO;
508 }
509
510 std::string partGuid;
511 StrToHex(partGuidRaw, partGuid);
512
513 if (!WriteStringToFile(keyRaw, BuildKeyPath(partGuid))) {
514 LOG(ERROR) << "Failed to persist key";
515 return -EIO;
516 } else {
517 LOG(DEBUG) << "Persisted key for GUID " << partGuid;
518 }
519
520 // Now let's build the new GPT table. We heavily rely on sgdisk to
521 // force optimal alignment on the created partitions.
522 cmd.clear();
523 cmd.push_back(kSgdiskPath);
524
525 // If requested, create a public partition first. Mixed-mode partitioning
526 // like this is an experimental feature.
527 if (ratio > 0) {
528 if (ratio < 10 || ratio > 90) {
529 LOG(ERROR) << "Mixed partition ratio must be between 10-90%";
530 return -EINVAL;
531 }
532
533 uint64_t splitMb = ((mSize / 100) * ratio) / 1024 / 1024;
534 cmd.push_back(StringPrintf("--new=0:0:+%" PRId64 "M", splitMb));
535 cmd.push_back(StringPrintf("--typecode=0:%s", kGptBasicData));
536 cmd.push_back("--change-name=0:shared");
537 }
538
539 // Define a metadata partition which is designed for future use; there
540 // should only be one of these per physical device, even if there are
541 // multiple private volumes.
542 cmd.push_back("--new=0:0:+16M");
543 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidMeta));
544 cmd.push_back("--change-name=0:android_meta");
545
546 // Define a single private partition filling the rest of disk.
547 cmd.push_back("--new=0:0:-0");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700548 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidExpand));
Jeff Sharkey9c484982015-03-31 10:35:33 -0700549 cmd.push_back(StringPrintf("--partition-guid=0:%s", partGuid.c_str()));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700550 cmd.push_back("--change-name=0:android_expand");
Jeff Sharkey9c484982015-03-31 10:35:33 -0700551
552 cmd.push_back(mDevPath);
553
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700554 if ((res = ForkExecvp(cmd)) != 0) {
555 LOG(ERROR) << "Failed to partition; status " << res;
556 return res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700557 }
558
559 return OK;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800560}
561
562int Disk::getMaxMinors() {
563 // Figure out maximum partition devices supported
Yu Ning942d4e82016-01-08 17:36:47 +0800564 unsigned int majorId = major(mDevice);
565 switch (majorId) {
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600566 case kMajorBlockLoop: {
567 std::string tmp;
568 if (!ReadFileToString(kSysfsLoopMaxMinors, &tmp)) {
569 LOG(ERROR) << "Failed to read max minors";
570 return -errno;
571 }
Jeff Sharkey95440eb2017-09-18 18:19:28 -0600572 return std::stoi(tmp);
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600573 }
Jeff Sharkeyf3ee2002015-04-19 15:55:42 -0700574 case kMajorBlockScsiA: case kMajorBlockScsiB: case kMajorBlockScsiC: case kMajorBlockScsiD:
575 case kMajorBlockScsiE: case kMajorBlockScsiF: case kMajorBlockScsiG: case kMajorBlockScsiH:
576 case kMajorBlockScsiI: case kMajorBlockScsiJ: case kMajorBlockScsiK: case kMajorBlockScsiL:
577 case kMajorBlockScsiM: case kMajorBlockScsiN: case kMajorBlockScsiO: case kMajorBlockScsiP: {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800578 // Per Documentation/devices.txt this is static
579 return 15;
580 }
581 case kMajorBlockMmc: {
582 // Per Documentation/devices.txt this is dynamic
583 std::string tmp;
Pierre-Hugues Hussonf347cd02017-11-28 15:42:56 +0100584 if (!ReadFileToString(kSysfsMmcMaxMinors, &tmp) &&
585 !ReadFileToString(kSysfsMmcMaxMinorsDeprecated, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700586 LOG(ERROR) << "Failed to read max minors";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800587 return -errno;
588 }
Jeff Sharkey95440eb2017-09-18 18:19:28 -0600589 return std::stoi(tmp);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800590 }
Yu Ning942d4e82016-01-08 17:36:47 +0800591 default: {
592 if (isVirtioBlkDevice(majorId)) {
593 // drivers/block/virtio_blk.c has "#define PART_BITS 4", so max is
594 // 2^4 - 1 = 15
595 return 15;
596 }
Dmitry Shmidt06dc6e52018-05-11 17:22:42 -0700597 if (isNvmeBlkDevice(majorId, mSysPath)) {
598 // despite kernel nvme driver supports up to 1M minors,
599 // #define NVME_MINORS (1U << MINORBITS)
600 // sgdisk can not support more than 127 partitions, due to
601 // #define MAX_MBR_PARTS 128
602 return 127;
603 }
Yu Ning942d4e82016-01-08 17:36:47 +0800604 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800605 }
606
Yu Ning942d4e82016-01-08 17:36:47 +0800607 LOG(ERROR) << "Unsupported block major type " << majorId;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800608 return -ENOTSUP;
609}
610
611} // namespace vold
612} // namespace android