blob: d7b19ac3973654adfe65c035ce15bae8bfdbccc4 [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";
81static const char* kGptAndroidMeta = "19A710A2-B3CA-11E4-B026-10604B889DCF";
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070082static const char* kGptAndroidExpand = "193D1EA4-B3CA-11E4-B075-10604B889DCF";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080083
84enum class Table {
85 kUnknown,
86 kMbr,
87 kGpt,
88};
89
Yu Ning942d4e82016-01-08 17:36:47 +080090static bool isVirtioBlkDevice(unsigned int major) {
91 /*
92 * The new emulator's "ranchu" virtual board no longer includes a goldfish
93 * MMC-based SD card device; instead, it emulates SD cards with virtio-blk,
94 * which has been supported by upstream kernel and QEMU for quite a while.
95 * Unfortunately, the virtio-blk block device driver does not use a fixed
96 * major number, but relies on the kernel to assign one from a specific
97 * range of block majors, which are allocated for "LOCAL/EXPERIMENAL USE"
98 * per Documentation/devices.txt. This is true even for the latest Linux
99 * kernel (4.4; see init() in drivers/block/virtio_blk.c).
100 *
101 * This makes it difficult for vold to detect a virtio-blk based SD card.
102 * The current solution checks two conditions (both must be met):
103 *
104 * a) If the running environment is the emulator;
105 * b) If the major number is an experimental block device major number (for
106 * x86/x86_64 3.10 ranchu kernels, virtio-blk always gets major number
107 * 253, but it is safer to match the range than just one value).
108 *
109 * Other conditions could be used, too, e.g. the hardware name should be
110 * "ranchu", the device's sysfs path should end with "/block/vd[d-z]", etc.
111 * But just having a) and b) is enough for now.
112 */
113 return IsRunningInEmulator() && major >= kMajorBlockExperimentalMin
114 && major <= kMajorBlockExperimentalMax;
115}
116
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700117Disk::Disk(const std::string& eventPath, dev_t device,
118 const std::string& nickname, int flags) :
119 mDevice(device), mSize(-1), mNickname(nickname), mFlags(flags), mCreated(
120 false), mJustPartitioned(false) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700121 mId = StringPrintf("disk:%u,%u", major(device), minor(device));
122 mEventPath = eventPath;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800123 mSysPath = StringPrintf("/sys/%s", eventPath.c_str());
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700124 mDevPath = StringPrintf("/dev/block/vold/%s", mId.c_str());
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800125 CreateDeviceNode(mDevPath, mDevice);
126}
127
128Disk::~Disk() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700129 CHECK(!mCreated);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800130 DestroyDeviceNode(mDevPath);
131}
132
133std::shared_ptr<VolumeBase> Disk::findVolume(const std::string& id) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700134 for (auto vol : mVolumes) {
135 if (vol->getId() == id) {
136 return vol;
137 }
138 auto stackedVol = vol->findVolume(id);
139 if (stackedVol != nullptr) {
140 return stackedVol;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800141 }
142 }
143 return nullptr;
144}
145
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700146void Disk::listVolumes(VolumeBase::Type type, std::list<std::string>& list) {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700147 for (const auto& vol : mVolumes) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700148 if (vol->getType() == type) {
149 list.push_back(vol->getId());
150 }
151 // TODO: consider looking at stacked volumes
152 }
153}
154
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700155status_t Disk::create() {
156 CHECK(!mCreated);
157 mCreated = true;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600158
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600159 auto listener = VolumeManager::Instance()->getListener();
160 if (listener) listener->onDiskCreated(getId(), mFlags);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600161
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700162 readMetadata();
163 readPartitions();
164 return OK;
165}
166
167status_t Disk::destroy() {
168 CHECK(mCreated);
169 destroyAllVolumes();
170 mCreated = false;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600171
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600172 auto listener = VolumeManager::Instance()->getListener();
173 if (listener) listener->onDiskDestroyed(getId());
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600174
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700175 return OK;
176}
177
178void Disk::createPublicVolume(dev_t device) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700179 auto vol = std::shared_ptr<VolumeBase>(new PublicVolume(device));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700180 if (mJustPartitioned) {
181 LOG(DEBUG) << "Device just partitioned; silently formatting";
182 vol->setSilent(true);
183 vol->create();
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700184 vol->format("auto");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700185 vol->destroy();
186 vol->setSilent(false);
187 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700188
Jeff Sharkey9c484982015-03-31 10:35:33 -0700189 mVolumes.push_back(vol);
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700190 vol->setDiskId(getId());
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700191 vol->create();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700192}
193
Jeff Sharkey9c484982015-03-31 10:35:33 -0700194void Disk::createPrivateVolume(dev_t device, const std::string& partGuid) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700195 std::string normalizedGuid;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700196 if (NormalizeHex(partGuid, normalizedGuid)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700197 LOG(WARNING) << "Invalid GUID " << partGuid;
198 return;
199 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700200
201 std::string keyRaw;
202 if (!ReadFileToString(BuildKeyPath(normalizedGuid), &keyRaw)) {
203 PLOG(ERROR) << "Failed to load key for GUID " << normalizedGuid;
204 return;
205 }
206
207 LOG(DEBUG) << "Found key for GUID " << normalizedGuid;
208
209 auto vol = std::shared_ptr<VolumeBase>(new PrivateVolume(device, keyRaw));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700210 if (mJustPartitioned) {
211 LOG(DEBUG) << "Device just partitioned; silently formatting";
212 vol->setSilent(true);
213 vol->create();
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700214 vol->format("auto");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700215 vol->destroy();
216 vol->setSilent(false);
217 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700218
219 mVolumes.push_back(vol);
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700220 vol->setDiskId(getId());
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700221 vol->setPartGuid(partGuid);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700222 vol->create();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700223}
224
225void Disk::destroyAllVolumes() {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700226 for (const auto& vol : mVolumes) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700227 vol->destroy();
228 }
229 mVolumes.clear();
230}
231
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800232status_t Disk::readMetadata() {
233 mSize = -1;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700234 mLabel.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800235
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700236 int fd = open(mDevPath.c_str(), O_RDONLY | O_CLOEXEC);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700237 if (fd != -1) {
238 if (ioctl(fd, BLKGETSIZE64, &mSize)) {
239 mSize = -1;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800240 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700241 close(fd);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800242 }
243
Yu Ning942d4e82016-01-08 17:36:47 +0800244 unsigned int majorId = major(mDevice);
245 switch (majorId) {
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600246 case kMajorBlockLoop: {
247 mLabel = "Virtual";
248 break;
249 }
Jeff Sharkeyf3ee2002015-04-19 15:55:42 -0700250 case kMajorBlockScsiA: case kMajorBlockScsiB: case kMajorBlockScsiC: case kMajorBlockScsiD:
251 case kMajorBlockScsiE: case kMajorBlockScsiF: case kMajorBlockScsiG: case kMajorBlockScsiH:
252 case kMajorBlockScsiI: case kMajorBlockScsiJ: case kMajorBlockScsiK: case kMajorBlockScsiL:
253 case kMajorBlockScsiM: case kMajorBlockScsiN: case kMajorBlockScsiO: case kMajorBlockScsiP: {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800254 std::string path(mSysPath + "/device/vendor");
255 std::string tmp;
256 if (!ReadFileToString(path, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700257 PLOG(WARNING) << "Failed to read vendor from " << path;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800258 return -errno;
259 }
Jeff Sharkeye50314d2018-02-24 18:23:35 -0700260 tmp = android::base::Trim(tmp);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800261 mLabel = tmp;
262 break;
263 }
264 case kMajorBlockMmc: {
265 std::string path(mSysPath + "/device/manfid");
266 std::string tmp;
267 if (!ReadFileToString(path, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700268 PLOG(WARNING) << "Failed to read manufacturer from " << path;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800269 return -errno;
270 }
Jeff Sharkeye50314d2018-02-24 18:23:35 -0700271 tmp = android::base::Trim(tmp);
Jeff Sharkey3472e522017-10-06 18:02:53 -0600272 int64_t manfid;
273 if (!android::base::ParseInt(tmp, &manfid)) {
274 PLOG(WARNING) << "Failed to parse manufacturer " << tmp;
275 return -EINVAL;
276 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800277 // Our goal here is to give the user a meaningful label, ideally
278 // matching whatever is silk-screened on the card. To reduce
279 // user confusion, this list doesn't contain white-label manfid.
280 switch (manfid) {
281 case 0x000003: mLabel = "SanDisk"; break;
282 case 0x00001b: mLabel = "Samsung"; break;
283 case 0x000028: mLabel = "Lexar"; break;
284 case 0x000074: mLabel = "Transcend"; break;
285 }
286 break;
287 }
288 default: {
Yu Ning942d4e82016-01-08 17:36:47 +0800289 if (isVirtioBlkDevice(majorId)) {
290 LOG(DEBUG) << "Recognized experimental block major ID " << majorId
291 << " as virtio-blk (emulator's virtual SD card device)";
292 mLabel = "Virtual";
293 break;
294 }
295 LOG(WARNING) << "Unsupported block major type " << majorId;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800296 return -ENOTSUP;
297 }
298 }
299
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600300 auto listener = VolumeManager::Instance()->getListener();
301 if (listener) listener->onDiskMetadataChanged(getId(),
302 mSize, mLabel, mSysPath);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600303
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800304 return OK;
305}
306
307status_t Disk::readPartitions() {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600308 int maxMinors = getMaxMinors();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800309 if (maxMinors < 0) {
310 return -ENOTSUP;
311 }
312
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700313 destroyAllVolumes();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800314
315 // Parse partition table
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700316
317 std::vector<std::string> cmd;
318 cmd.push_back(kSgdiskPath);
319 cmd.push_back("--android-dump");
320 cmd.push_back(mDevPath);
321
322 std::vector<std::string> output;
323 status_t res = ForkExecvp(cmd, output);
324 if (res != OK) {
325 LOG(WARNING) << "sgdisk failed to scan " << mDevPath;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600326
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600327 auto listener = VolumeManager::Instance()->getListener();
328 if (listener) listener->onDiskScanned(getId());
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600329
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700330 mJustPartitioned = false;
331 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800332 }
333
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800334 Table table = Table::kUnknown;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700335 bool foundParts = false;
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700336 for (const auto& line : output) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600337 auto split = android::base::Split(line, kSgdiskToken);
338 auto it = split.begin();
339 if (it == split.end()) continue;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700340
Jeff Sharkey3472e522017-10-06 18:02:53 -0600341 if (*it == "DISK") {
342 if (++it == split.end()) continue;
343 if (*it == "mbr") {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800344 table = Table::kMbr;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600345 } else if (*it == "gpt") {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800346 table = Table::kGpt;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600347 } else {
348 LOG(WARNING) << "Invalid partition table " << *it;
349 continue;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800350 }
Jeff Sharkey3472e522017-10-06 18:02:53 -0600351 } else if (*it == "PART") {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700352 foundParts = true;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600353
354 if (++it == split.end()) continue;
355 int i = 0;
356 if (!android::base::ParseInt(*it, &i, 1, maxMinors)) {
357 LOG(WARNING) << "Invalid partition number " << *it;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800358 continue;
359 }
360 dev_t partDevice = makedev(major(mDevice), minor(mDevice) + i);
361
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800362 if (table == Table::kMbr) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600363 if (++it == split.end()) continue;
364 int type = 0;
365 if (!android::base::ParseInt("0x" + *it, &type)) {
366 LOG(WARNING) << "Invalid partition type " << *it;
367 continue;
368 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800369
Jeff Sharkey3472e522017-10-06 18:02:53 -0600370 switch (type) {
Jeff Sharkey37ba1252018-01-19 10:55:18 +0900371 case 0x06: // FAT16
372 case 0x07: // HPFS/NTFS/exFAT
373 case 0x0b: // W95 FAT32 (LBA)
374 case 0x0c: // W95 FAT32 (LBA)
375 case 0x0e: // W95 FAT16 (LBA)
376 createPublicVolume(partDevice);
377 break;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800378 }
379 } else if (table == Table::kGpt) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600380 if (++it == split.end()) continue;
381 auto typeGuid = *it;
382 if (++it == split.end()) continue;
383 auto partGuid = *it;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800384
Jeff Sharkey3472e522017-10-06 18:02:53 -0600385 if (android::base::EqualsIgnoreCase(typeGuid, kGptBasicData)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700386 createPublicVolume(partDevice);
Jeff Sharkey3472e522017-10-06 18:02:53 -0600387 } else if (android::base::EqualsIgnoreCase(typeGuid, kGptAndroidExpand)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700388 createPrivateVolume(partDevice, partGuid);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800389 }
390 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800391 }
392 }
393
394 // Ugly last ditch effort, treat entire disk as partition
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700395 if (table == Table::kUnknown || !foundParts) {
396 LOG(WARNING) << mId << " has unknown partition table; trying entire device";
Jeff Sharkey63123c02015-06-26 11:16:14 -0700397
398 std::string fsType;
399 std::string unused;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600400 if (ReadMetadataUntrusted(mDevPath, &fsType, &unused, &unused) == OK) {
Jeff Sharkey63123c02015-06-26 11:16:14 -0700401 createPublicVolume(mDevice);
402 } else {
403 LOG(WARNING) << mId << " failed to identify, giving up";
404 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800405 }
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700406
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600407 auto listener = VolumeManager::Instance()->getListener();
408 if (listener) listener->onDiskScanned(getId());
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600409
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700410 mJustPartitioned = false;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800411 return OK;
412}
413
Jeff Sharkey9c484982015-03-31 10:35:33 -0700414status_t Disk::unmountAll() {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700415 for (const auto& vol : mVolumes) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700416 vol->unmount();
417 }
418 return OK;
419}
420
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800421status_t Disk::partitionPublic() {
Jeff Sharkeydadccee2015-09-23 14:13:45 -0700422 int res;
423
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700424 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700425 mJustPartitioned = true;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800426
Jeff Sharkeydadccee2015-09-23 14:13:45 -0700427 // First nuke any existing partition table
428 std::vector<std::string> cmd;
429 cmd.push_back(kSgdiskPath);
430 cmd.push_back("--zap-all");
431 cmd.push_back(mDevPath);
432
433 // Zap sometimes returns an error when it actually succeeded, so
434 // just log as warning and keep rolling forward.
435 if ((res = ForkExecvp(cmd)) != 0) {
436 LOG(WARNING) << "Failed to zap; status " << res;
437 }
438
Jeff Sharkey68f1b8b2017-10-18 14:09:52 -0600439 // Now let's build the new MBR table. We heavily rely on sgdisk to
440 // force optimal alignment on the created partitions.
441 cmd.clear();
442 cmd.push_back(kSgdiskPath);
443 cmd.push_back("--new=0:0:-0");
444 cmd.push_back("--typecode=0:0c00");
445 cmd.push_back("--gpttombr=1");
446 cmd.push_back(mDevPath);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800447
Jeff Sharkey68f1b8b2017-10-18 14:09:52 -0600448 if ((res = ForkExecvp(cmd)) != 0) {
449 LOG(ERROR) << "Failed to partition; status " << res;
450 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800451 }
452
Jeff Sharkey68f1b8b2017-10-18 14:09:52 -0600453 return OK;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800454}
455
456status_t Disk::partitionPrivate() {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700457 return partitionMixed(0);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800458}
459
460status_t Disk::partitionMixed(int8_t ratio) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700461 int res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700462
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700463 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700464 mJustPartitioned = true;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700465
466 // First nuke any existing partition table
467 std::vector<std::string> cmd;
468 cmd.push_back(kSgdiskPath);
469 cmd.push_back("--zap-all");
470 cmd.push_back(mDevPath);
471
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700472 // Zap sometimes returns an error when it actually succeeded, so
473 // just log as warning and keep rolling forward.
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700474 if ((res = ForkExecvp(cmd)) != 0) {
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700475 LOG(WARNING) << "Failed to zap; status " << res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700476 }
477
478 // We've had some success above, so generate both the private partition
479 // GUID and encryption key and persist them.
480 std::string partGuidRaw;
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600481 if (GenerateRandomUuid(partGuidRaw) != OK) {
482 LOG(ERROR) << "Failed to generate GUID";
483 return -EIO;
484 }
485
Jeff Sharkey9c484982015-03-31 10:35:33 -0700486 std::string keyRaw;
Greg Kaiser57f9af62018-02-16 13:13:58 -0800487 if (ReadRandomBytes(cryptfs_get_keysize(), keyRaw) != OK) {
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600488 LOG(ERROR) << "Failed to generate key";
Jeff Sharkey9c484982015-03-31 10:35:33 -0700489 return -EIO;
490 }
491
492 std::string partGuid;
493 StrToHex(partGuidRaw, partGuid);
494
495 if (!WriteStringToFile(keyRaw, BuildKeyPath(partGuid))) {
496 LOG(ERROR) << "Failed to persist key";
497 return -EIO;
498 } else {
499 LOG(DEBUG) << "Persisted key for GUID " << partGuid;
500 }
501
502 // Now let's build the new GPT table. We heavily rely on sgdisk to
503 // force optimal alignment on the created partitions.
504 cmd.clear();
505 cmd.push_back(kSgdiskPath);
506
507 // If requested, create a public partition first. Mixed-mode partitioning
508 // like this is an experimental feature.
509 if (ratio > 0) {
510 if (ratio < 10 || ratio > 90) {
511 LOG(ERROR) << "Mixed partition ratio must be between 10-90%";
512 return -EINVAL;
513 }
514
515 uint64_t splitMb = ((mSize / 100) * ratio) / 1024 / 1024;
516 cmd.push_back(StringPrintf("--new=0:0:+%" PRId64 "M", splitMb));
517 cmd.push_back(StringPrintf("--typecode=0:%s", kGptBasicData));
518 cmd.push_back("--change-name=0:shared");
519 }
520
521 // Define a metadata partition which is designed for future use; there
522 // should only be one of these per physical device, even if there are
523 // multiple private volumes.
524 cmd.push_back("--new=0:0:+16M");
525 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidMeta));
526 cmd.push_back("--change-name=0:android_meta");
527
528 // Define a single private partition filling the rest of disk.
529 cmd.push_back("--new=0:0:-0");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700530 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidExpand));
Jeff Sharkey9c484982015-03-31 10:35:33 -0700531 cmd.push_back(StringPrintf("--partition-guid=0:%s", partGuid.c_str()));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700532 cmd.push_back("--change-name=0:android_expand");
Jeff Sharkey9c484982015-03-31 10:35:33 -0700533
534 cmd.push_back(mDevPath);
535
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700536 if ((res = ForkExecvp(cmd)) != 0) {
537 LOG(ERROR) << "Failed to partition; status " << res;
538 return res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700539 }
540
541 return OK;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800542}
543
544int Disk::getMaxMinors() {
545 // Figure out maximum partition devices supported
Yu Ning942d4e82016-01-08 17:36:47 +0800546 unsigned int majorId = major(mDevice);
547 switch (majorId) {
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600548 case kMajorBlockLoop: {
549 std::string tmp;
550 if (!ReadFileToString(kSysfsLoopMaxMinors, &tmp)) {
551 LOG(ERROR) << "Failed to read max minors";
552 return -errno;
553 }
Jeff Sharkey95440eb2017-09-18 18:19:28 -0600554 return std::stoi(tmp);
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600555 }
Jeff Sharkeyf3ee2002015-04-19 15:55:42 -0700556 case kMajorBlockScsiA: case kMajorBlockScsiB: case kMajorBlockScsiC: case kMajorBlockScsiD:
557 case kMajorBlockScsiE: case kMajorBlockScsiF: case kMajorBlockScsiG: case kMajorBlockScsiH:
558 case kMajorBlockScsiI: case kMajorBlockScsiJ: case kMajorBlockScsiK: case kMajorBlockScsiL:
559 case kMajorBlockScsiM: case kMajorBlockScsiN: case kMajorBlockScsiO: case kMajorBlockScsiP: {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800560 // Per Documentation/devices.txt this is static
561 return 15;
562 }
563 case kMajorBlockMmc: {
564 // Per Documentation/devices.txt this is dynamic
565 std::string tmp;
Pierre-Hugues Hussonf347cd02017-11-28 15:42:56 +0100566 if (!ReadFileToString(kSysfsMmcMaxMinors, &tmp) &&
567 !ReadFileToString(kSysfsMmcMaxMinorsDeprecated, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700568 LOG(ERROR) << "Failed to read max minors";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800569 return -errno;
570 }
Jeff Sharkey95440eb2017-09-18 18:19:28 -0600571 return std::stoi(tmp);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800572 }
Yu Ning942d4e82016-01-08 17:36:47 +0800573 default: {
574 if (isVirtioBlkDevice(majorId)) {
575 // drivers/block/virtio_blk.c has "#define PART_BITS 4", so max is
576 // 2^4 - 1 = 15
577 return 15;
578 }
579 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800580 }
581
Yu Ning942d4e82016-01-08 17:36:47 +0800582 LOG(ERROR) << "Unsupported block major type " << majorId;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800583 return -ENOTSUP;
584}
585
586} // namespace vold
587} // namespace android