blob: 9fcf5e1e18336b96ae0f7dc5f0f28744eff0f64e [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 }
260 mLabel = tmp;
261 break;
262 }
263 case kMajorBlockMmc: {
264 std::string path(mSysPath + "/device/manfid");
265 std::string tmp;
266 if (!ReadFileToString(path, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700267 PLOG(WARNING) << "Failed to read manufacturer from " << path;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800268 return -errno;
269 }
Jeff Sharkey3472e522017-10-06 18:02:53 -0600270 int64_t manfid;
271 if (!android::base::ParseInt(tmp, &manfid)) {
272 PLOG(WARNING) << "Failed to parse manufacturer " << tmp;
273 return -EINVAL;
274 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800275 // Our goal here is to give the user a meaningful label, ideally
276 // matching whatever is silk-screened on the card. To reduce
277 // user confusion, this list doesn't contain white-label manfid.
278 switch (manfid) {
279 case 0x000003: mLabel = "SanDisk"; break;
280 case 0x00001b: mLabel = "Samsung"; break;
281 case 0x000028: mLabel = "Lexar"; break;
282 case 0x000074: mLabel = "Transcend"; break;
283 }
284 break;
285 }
286 default: {
Yu Ning942d4e82016-01-08 17:36:47 +0800287 if (isVirtioBlkDevice(majorId)) {
288 LOG(DEBUG) << "Recognized experimental block major ID " << majorId
289 << " as virtio-blk (emulator's virtual SD card device)";
290 mLabel = "Virtual";
291 break;
292 }
293 LOG(WARNING) << "Unsupported block major type " << majorId;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800294 return -ENOTSUP;
295 }
296 }
297
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600298 auto listener = VolumeManager::Instance()->getListener();
299 if (listener) listener->onDiskMetadataChanged(getId(),
300 mSize, mLabel, mSysPath);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600301
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800302 return OK;
303}
304
305status_t Disk::readPartitions() {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600306 int maxMinors = getMaxMinors();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800307 if (maxMinors < 0) {
308 return -ENOTSUP;
309 }
310
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700311 destroyAllVolumes();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800312
313 // Parse partition table
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700314
315 std::vector<std::string> cmd;
316 cmd.push_back(kSgdiskPath);
317 cmd.push_back("--android-dump");
318 cmd.push_back(mDevPath);
319
320 std::vector<std::string> output;
321 status_t res = ForkExecvp(cmd, output);
322 if (res != OK) {
323 LOG(WARNING) << "sgdisk failed to scan " << mDevPath;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600324
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600325 auto listener = VolumeManager::Instance()->getListener();
326 if (listener) listener->onDiskScanned(getId());
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600327
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700328 mJustPartitioned = false;
329 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800330 }
331
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800332 Table table = Table::kUnknown;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700333 bool foundParts = false;
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700334 for (const auto& line : output) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600335 auto split = android::base::Split(line, kSgdiskToken);
336 auto it = split.begin();
337 if (it == split.end()) continue;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700338
Jeff Sharkey3472e522017-10-06 18:02:53 -0600339 if (*it == "DISK") {
340 if (++it == split.end()) continue;
341 if (*it == "mbr") {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800342 table = Table::kMbr;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600343 } else if (*it == "gpt") {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800344 table = Table::kGpt;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600345 } else {
346 LOG(WARNING) << "Invalid partition table " << *it;
347 continue;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800348 }
Jeff Sharkey3472e522017-10-06 18:02:53 -0600349 } else if (*it == "PART") {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700350 foundParts = true;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600351
352 if (++it == split.end()) continue;
353 int i = 0;
354 if (!android::base::ParseInt(*it, &i, 1, maxMinors)) {
355 LOG(WARNING) << "Invalid partition number " << *it;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800356 continue;
357 }
358 dev_t partDevice = makedev(major(mDevice), minor(mDevice) + i);
359
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800360 if (table == Table::kMbr) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600361 if (++it == split.end()) continue;
362 int type = 0;
363 if (!android::base::ParseInt("0x" + *it, &type)) {
364 LOG(WARNING) << "Invalid partition type " << *it;
365 continue;
366 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800367
Jeff Sharkey3472e522017-10-06 18:02:53 -0600368 switch (type) {
Jeff Sharkey37ba1252018-01-19 10:55:18 +0900369 case 0x06: // FAT16
370 case 0x07: // HPFS/NTFS/exFAT
371 case 0x0b: // W95 FAT32 (LBA)
372 case 0x0c: // W95 FAT32 (LBA)
373 case 0x0e: // W95 FAT16 (LBA)
374 createPublicVolume(partDevice);
375 break;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800376 }
377 } else if (table == Table::kGpt) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600378 if (++it == split.end()) continue;
379 auto typeGuid = *it;
380 if (++it == split.end()) continue;
381 auto partGuid = *it;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800382
Jeff Sharkey3472e522017-10-06 18:02:53 -0600383 if (android::base::EqualsIgnoreCase(typeGuid, kGptBasicData)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700384 createPublicVolume(partDevice);
Jeff Sharkey3472e522017-10-06 18:02:53 -0600385 } else if (android::base::EqualsIgnoreCase(typeGuid, kGptAndroidExpand)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700386 createPrivateVolume(partDevice, partGuid);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800387 }
388 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800389 }
390 }
391
392 // Ugly last ditch effort, treat entire disk as partition
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700393 if (table == Table::kUnknown || !foundParts) {
394 LOG(WARNING) << mId << " has unknown partition table; trying entire device";
Jeff Sharkey63123c02015-06-26 11:16:14 -0700395
396 std::string fsType;
397 std::string unused;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600398 if (ReadMetadataUntrusted(mDevPath, &fsType, &unused, &unused) == OK) {
Jeff Sharkey63123c02015-06-26 11:16:14 -0700399 createPublicVolume(mDevice);
400 } else {
401 LOG(WARNING) << mId << " failed to identify, giving up";
402 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800403 }
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700404
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600405 auto listener = VolumeManager::Instance()->getListener();
406 if (listener) listener->onDiskScanned(getId());
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600407
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700408 mJustPartitioned = false;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800409 return OK;
410}
411
Jeff Sharkey9c484982015-03-31 10:35:33 -0700412status_t Disk::unmountAll() {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700413 for (const auto& vol : mVolumes) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700414 vol->unmount();
415 }
416 return OK;
417}
418
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800419status_t Disk::partitionPublic() {
Jeff Sharkeydadccee2015-09-23 14:13:45 -0700420 int res;
421
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700422 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700423 mJustPartitioned = true;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800424
Jeff Sharkeydadccee2015-09-23 14:13:45 -0700425 // First nuke any existing partition table
426 std::vector<std::string> cmd;
427 cmd.push_back(kSgdiskPath);
428 cmd.push_back("--zap-all");
429 cmd.push_back(mDevPath);
430
431 // Zap sometimes returns an error when it actually succeeded, so
432 // just log as warning and keep rolling forward.
433 if ((res = ForkExecvp(cmd)) != 0) {
434 LOG(WARNING) << "Failed to zap; status " << res;
435 }
436
Jeff Sharkey68f1b8b2017-10-18 14:09:52 -0600437 // Now let's build the new MBR table. We heavily rely on sgdisk to
438 // force optimal alignment on the created partitions.
439 cmd.clear();
440 cmd.push_back(kSgdiskPath);
441 cmd.push_back("--new=0:0:-0");
442 cmd.push_back("--typecode=0:0c00");
443 cmd.push_back("--gpttombr=1");
444 cmd.push_back(mDevPath);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800445
Jeff Sharkey68f1b8b2017-10-18 14:09:52 -0600446 if ((res = ForkExecvp(cmd)) != 0) {
447 LOG(ERROR) << "Failed to partition; status " << res;
448 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800449 }
450
Jeff Sharkey68f1b8b2017-10-18 14:09:52 -0600451 return OK;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800452}
453
454status_t Disk::partitionPrivate() {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700455 return partitionMixed(0);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800456}
457
458status_t Disk::partitionMixed(int8_t ratio) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700459 int res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700460
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700461 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700462 mJustPartitioned = true;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700463
464 // First nuke any existing partition table
465 std::vector<std::string> cmd;
466 cmd.push_back(kSgdiskPath);
467 cmd.push_back("--zap-all");
468 cmd.push_back(mDevPath);
469
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700470 // Zap sometimes returns an error when it actually succeeded, so
471 // just log as warning and keep rolling forward.
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700472 if ((res = ForkExecvp(cmd)) != 0) {
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700473 LOG(WARNING) << "Failed to zap; status " << res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700474 }
475
476 // We've had some success above, so generate both the private partition
477 // GUID and encryption key and persist them.
478 std::string partGuidRaw;
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600479 if (GenerateRandomUuid(partGuidRaw) != OK) {
480 LOG(ERROR) << "Failed to generate GUID";
481 return -EIO;
482 }
483
Jeff Sharkey9c484982015-03-31 10:35:33 -0700484 std::string keyRaw;
Greg Kaiser57f9af62018-02-16 13:13:58 -0800485 if (ReadRandomBytes(cryptfs_get_keysize(), keyRaw) != OK) {
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600486 LOG(ERROR) << "Failed to generate key";
Jeff Sharkey9c484982015-03-31 10:35:33 -0700487 return -EIO;
488 }
489
490 std::string partGuid;
491 StrToHex(partGuidRaw, partGuid);
492
493 if (!WriteStringToFile(keyRaw, BuildKeyPath(partGuid))) {
494 LOG(ERROR) << "Failed to persist key";
495 return -EIO;
496 } else {
497 LOG(DEBUG) << "Persisted key for GUID " << partGuid;
498 }
499
500 // Now let's build the new GPT table. We heavily rely on sgdisk to
501 // force optimal alignment on the created partitions.
502 cmd.clear();
503 cmd.push_back(kSgdiskPath);
504
505 // If requested, create a public partition first. Mixed-mode partitioning
506 // like this is an experimental feature.
507 if (ratio > 0) {
508 if (ratio < 10 || ratio > 90) {
509 LOG(ERROR) << "Mixed partition ratio must be between 10-90%";
510 return -EINVAL;
511 }
512
513 uint64_t splitMb = ((mSize / 100) * ratio) / 1024 / 1024;
514 cmd.push_back(StringPrintf("--new=0:0:+%" PRId64 "M", splitMb));
515 cmd.push_back(StringPrintf("--typecode=0:%s", kGptBasicData));
516 cmd.push_back("--change-name=0:shared");
517 }
518
519 // Define a metadata partition which is designed for future use; there
520 // should only be one of these per physical device, even if there are
521 // multiple private volumes.
522 cmd.push_back("--new=0:0:+16M");
523 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidMeta));
524 cmd.push_back("--change-name=0:android_meta");
525
526 // Define a single private partition filling the rest of disk.
527 cmd.push_back("--new=0:0:-0");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700528 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidExpand));
Jeff Sharkey9c484982015-03-31 10:35:33 -0700529 cmd.push_back(StringPrintf("--partition-guid=0:%s", partGuid.c_str()));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700530 cmd.push_back("--change-name=0:android_expand");
Jeff Sharkey9c484982015-03-31 10:35:33 -0700531
532 cmd.push_back(mDevPath);
533
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700534 if ((res = ForkExecvp(cmd)) != 0) {
535 LOG(ERROR) << "Failed to partition; status " << res;
536 return res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700537 }
538
539 return OK;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800540}
541
542int Disk::getMaxMinors() {
543 // Figure out maximum partition devices supported
Yu Ning942d4e82016-01-08 17:36:47 +0800544 unsigned int majorId = major(mDevice);
545 switch (majorId) {
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600546 case kMajorBlockLoop: {
547 std::string tmp;
548 if (!ReadFileToString(kSysfsLoopMaxMinors, &tmp)) {
549 LOG(ERROR) << "Failed to read max minors";
550 return -errno;
551 }
Jeff Sharkey95440eb2017-09-18 18:19:28 -0600552 return std::stoi(tmp);
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600553 }
Jeff Sharkeyf3ee2002015-04-19 15:55:42 -0700554 case kMajorBlockScsiA: case kMajorBlockScsiB: case kMajorBlockScsiC: case kMajorBlockScsiD:
555 case kMajorBlockScsiE: case kMajorBlockScsiF: case kMajorBlockScsiG: case kMajorBlockScsiH:
556 case kMajorBlockScsiI: case kMajorBlockScsiJ: case kMajorBlockScsiK: case kMajorBlockScsiL:
557 case kMajorBlockScsiM: case kMajorBlockScsiN: case kMajorBlockScsiO: case kMajorBlockScsiP: {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800558 // Per Documentation/devices.txt this is static
559 return 15;
560 }
561 case kMajorBlockMmc: {
562 // Per Documentation/devices.txt this is dynamic
563 std::string tmp;
Pierre-Hugues Hussonf347cd02017-11-28 15:42:56 +0100564 if (!ReadFileToString(kSysfsMmcMaxMinors, &tmp) &&
565 !ReadFileToString(kSysfsMmcMaxMinorsDeprecated, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700566 LOG(ERROR) << "Failed to read max minors";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800567 return -errno;
568 }
Jeff Sharkey95440eb2017-09-18 18:19:28 -0600569 return std::stoi(tmp);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800570 }
Yu Ning942d4e82016-01-08 17:36:47 +0800571 default: {
572 if (isVirtioBlkDevice(majorId)) {
573 // drivers/block/virtio_blk.c has "#define PART_BITS 4", so max is
574 // 2^4 - 1 = 15
575 return 15;
576 }
577 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800578 }
579
Yu Ning942d4e82016-01-08 17:36:47 +0800580 LOG(ERROR) << "Unsupported block major type " << majorId;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800581 return -ENOTSUP;
582}
583
584} // namespace vold
585} // namespace android