blob: 14de74f82e7cc9c720c1211c00e35bc085ccbc54 [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
Jeff Sharkey9c484982015-03-31 10:35:33 -070033#include <vector>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080034#include <fcntl.h>
Jeff Sharkey38cfc022015-03-30 21:22:07 -070035#include <inttypes.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080036#include <stdio.h>
37#include <stdlib.h>
38#include <sys/types.h>
39#include <sys/stat.h>
Elliott Hughes0e08e842017-05-18 09:08:24 -070040#include <sys/sysmacros.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080041#include <sys/mount.h>
42
Dan Albertae9e8902015-03-16 10:35:17 -070043using android::base::ReadFileToString;
Jeff Sharkey9c484982015-03-31 10:35:33 -070044using android::base::WriteStringToFile;
Dan Albertae9e8902015-03-16 10:35:17 -070045using android::base::StringPrintf;
46
Jeff Sharkeydeb24052015-03-02 21:01:40 -080047namespace android {
48namespace vold {
49
50static const char* kSgdiskPath = "/system/bin/sgdisk";
51static const char* kSgdiskToken = " \t\n";
52
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060053static const char* kSysfsLoopMaxMinors = "/sys/module/loop/parameters/max_part";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080054static const char* kSysfsMmcMaxMinors = "/sys/module/mmcblk/parameters/perdev_minors";
55
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060056static const unsigned int kMajorBlockLoop = 7;
Jeff Sharkeyf3ee2002015-04-19 15:55:42 -070057static const unsigned int kMajorBlockScsiA = 8;
58static const unsigned int kMajorBlockScsiB = 65;
59static const unsigned int kMajorBlockScsiC = 66;
60static const unsigned int kMajorBlockScsiD = 67;
61static const unsigned int kMajorBlockScsiE = 68;
62static const unsigned int kMajorBlockScsiF = 69;
63static const unsigned int kMajorBlockScsiG = 70;
64static const unsigned int kMajorBlockScsiH = 71;
65static const unsigned int kMajorBlockScsiI = 128;
66static const unsigned int kMajorBlockScsiJ = 129;
67static const unsigned int kMajorBlockScsiK = 130;
68static const unsigned int kMajorBlockScsiL = 131;
69static const unsigned int kMajorBlockScsiM = 132;
70static const unsigned int kMajorBlockScsiN = 133;
71static const unsigned int kMajorBlockScsiO = 134;
72static const unsigned int kMajorBlockScsiP = 135;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080073static const unsigned int kMajorBlockMmc = 179;
Yu Ning942d4e82016-01-08 17:36:47 +080074static const unsigned int kMajorBlockExperimentalMin = 240;
75static const unsigned int kMajorBlockExperimentalMax = 254;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080076
77static const char* kGptBasicData = "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7";
78static const char* kGptAndroidMeta = "19A710A2-B3CA-11E4-B026-10604B889DCF";
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070079static const char* kGptAndroidExpand = "193D1EA4-B3CA-11E4-B075-10604B889DCF";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080080
81enum class Table {
82 kUnknown,
83 kMbr,
84 kGpt,
85};
86
Yu Ning942d4e82016-01-08 17:36:47 +080087static bool isVirtioBlkDevice(unsigned int major) {
88 /*
89 * The new emulator's "ranchu" virtual board no longer includes a goldfish
90 * MMC-based SD card device; instead, it emulates SD cards with virtio-blk,
91 * which has been supported by upstream kernel and QEMU for quite a while.
92 * Unfortunately, the virtio-blk block device driver does not use a fixed
93 * major number, but relies on the kernel to assign one from a specific
94 * range of block majors, which are allocated for "LOCAL/EXPERIMENAL USE"
95 * per Documentation/devices.txt. This is true even for the latest Linux
96 * kernel (4.4; see init() in drivers/block/virtio_blk.c).
97 *
98 * This makes it difficult for vold to detect a virtio-blk based SD card.
99 * The current solution checks two conditions (both must be met):
100 *
101 * a) If the running environment is the emulator;
102 * b) If the major number is an experimental block device major number (for
103 * x86/x86_64 3.10 ranchu kernels, virtio-blk always gets major number
104 * 253, but it is safer to match the range than just one value).
105 *
106 * Other conditions could be used, too, e.g. the hardware name should be
107 * "ranchu", the device's sysfs path should end with "/block/vd[d-z]", etc.
108 * But just having a) and b) is enough for now.
109 */
110 return IsRunningInEmulator() && major >= kMajorBlockExperimentalMin
111 && major <= kMajorBlockExperimentalMax;
112}
113
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700114Disk::Disk(const std::string& eventPath, dev_t device,
115 const std::string& nickname, int flags) :
116 mDevice(device), mSize(-1), mNickname(nickname), mFlags(flags), mCreated(
117 false), mJustPartitioned(false) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700118 mId = StringPrintf("disk:%u,%u", major(device), minor(device));
119 mEventPath = eventPath;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800120 mSysPath = StringPrintf("/sys/%s", eventPath.c_str());
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700121 mDevPath = StringPrintf("/dev/block/vold/%s", mId.c_str());
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800122 CreateDeviceNode(mDevPath, mDevice);
123}
124
125Disk::~Disk() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700126 CHECK(!mCreated);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800127 DestroyDeviceNode(mDevPath);
128}
129
130std::shared_ptr<VolumeBase> Disk::findVolume(const std::string& id) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700131 for (auto vol : mVolumes) {
132 if (vol->getId() == id) {
133 return vol;
134 }
135 auto stackedVol = vol->findVolume(id);
136 if (stackedVol != nullptr) {
137 return stackedVol;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800138 }
139 }
140 return nullptr;
141}
142
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700143void Disk::listVolumes(VolumeBase::Type type, std::list<std::string>& list) {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700144 for (const auto& vol : mVolumes) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700145 if (vol->getType() == type) {
146 list.push_back(vol->getId());
147 }
148 // TODO: consider looking at stacked volumes
149 }
150}
151
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700152status_t Disk::create() {
153 CHECK(!mCreated);
154 mCreated = true;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600155
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600156 auto listener = VolumeManager::Instance()->getListener();
157 if (listener) listener->onDiskCreated(getId(), mFlags);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600158
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700159 readMetadata();
160 readPartitions();
161 return OK;
162}
163
164status_t Disk::destroy() {
165 CHECK(mCreated);
166 destroyAllVolumes();
167 mCreated = false;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600168
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600169 auto listener = VolumeManager::Instance()->getListener();
170 if (listener) listener->onDiskDestroyed(getId());
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600171
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700172 return OK;
173}
174
175void Disk::createPublicVolume(dev_t device) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700176 auto vol = std::shared_ptr<VolumeBase>(new PublicVolume(device));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700177 if (mJustPartitioned) {
178 LOG(DEBUG) << "Device just partitioned; silently formatting";
179 vol->setSilent(true);
180 vol->create();
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700181 vol->format("auto");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700182 vol->destroy();
183 vol->setSilent(false);
184 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700185
Jeff Sharkey9c484982015-03-31 10:35:33 -0700186 mVolumes.push_back(vol);
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700187 vol->setDiskId(getId());
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700188 vol->create();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700189}
190
Jeff Sharkey9c484982015-03-31 10:35:33 -0700191void Disk::createPrivateVolume(dev_t device, const std::string& partGuid) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700192 std::string normalizedGuid;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700193 if (NormalizeHex(partGuid, normalizedGuid)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700194 LOG(WARNING) << "Invalid GUID " << partGuid;
195 return;
196 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700197
198 std::string keyRaw;
199 if (!ReadFileToString(BuildKeyPath(normalizedGuid), &keyRaw)) {
200 PLOG(ERROR) << "Failed to load key for GUID " << normalizedGuid;
201 return;
202 }
203
204 LOG(DEBUG) << "Found key for GUID " << normalizedGuid;
205
206 auto vol = std::shared_ptr<VolumeBase>(new PrivateVolume(device, keyRaw));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700207 if (mJustPartitioned) {
208 LOG(DEBUG) << "Device just partitioned; silently formatting";
209 vol->setSilent(true);
210 vol->create();
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700211 vol->format("auto");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700212 vol->destroy();
213 vol->setSilent(false);
214 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700215
216 mVolumes.push_back(vol);
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700217 vol->setDiskId(getId());
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700218 vol->setPartGuid(partGuid);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700219 vol->create();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700220}
221
222void Disk::destroyAllVolumes() {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700223 for (const auto& vol : mVolumes) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700224 vol->destroy();
225 }
226 mVolumes.clear();
227}
228
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800229status_t Disk::readMetadata() {
230 mSize = -1;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700231 mLabel.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800232
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700233 int fd = open(mDevPath.c_str(), O_RDONLY | O_CLOEXEC);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700234 if (fd != -1) {
235 if (ioctl(fd, BLKGETSIZE64, &mSize)) {
236 mSize = -1;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800237 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700238 close(fd);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800239 }
240
Yu Ning942d4e82016-01-08 17:36:47 +0800241 unsigned int majorId = major(mDevice);
242 switch (majorId) {
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600243 case kMajorBlockLoop: {
244 mLabel = "Virtual";
245 break;
246 }
Jeff Sharkeyf3ee2002015-04-19 15:55:42 -0700247 case kMajorBlockScsiA: case kMajorBlockScsiB: case kMajorBlockScsiC: case kMajorBlockScsiD:
248 case kMajorBlockScsiE: case kMajorBlockScsiF: case kMajorBlockScsiG: case kMajorBlockScsiH:
249 case kMajorBlockScsiI: case kMajorBlockScsiJ: case kMajorBlockScsiK: case kMajorBlockScsiL:
250 case kMajorBlockScsiM: case kMajorBlockScsiN: case kMajorBlockScsiO: case kMajorBlockScsiP: {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800251 std::string path(mSysPath + "/device/vendor");
252 std::string tmp;
253 if (!ReadFileToString(path, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700254 PLOG(WARNING) << "Failed to read vendor from " << path;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800255 return -errno;
256 }
257 mLabel = tmp;
258 break;
259 }
260 case kMajorBlockMmc: {
261 std::string path(mSysPath + "/device/manfid");
262 std::string tmp;
263 if (!ReadFileToString(path, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700264 PLOG(WARNING) << "Failed to read manufacturer from " << path;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800265 return -errno;
266 }
Jeff Sharkey3472e522017-10-06 18:02:53 -0600267 int64_t manfid;
268 if (!android::base::ParseInt(tmp, &manfid)) {
269 PLOG(WARNING) << "Failed to parse manufacturer " << tmp;
270 return -EINVAL;
271 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800272 // Our goal here is to give the user a meaningful label, ideally
273 // matching whatever is silk-screened on the card. To reduce
274 // user confusion, this list doesn't contain white-label manfid.
275 switch (manfid) {
276 case 0x000003: mLabel = "SanDisk"; break;
277 case 0x00001b: mLabel = "Samsung"; break;
278 case 0x000028: mLabel = "Lexar"; break;
279 case 0x000074: mLabel = "Transcend"; break;
280 }
281 break;
282 }
283 default: {
Yu Ning942d4e82016-01-08 17:36:47 +0800284 if (isVirtioBlkDevice(majorId)) {
285 LOG(DEBUG) << "Recognized experimental block major ID " << majorId
286 << " as virtio-blk (emulator's virtual SD card device)";
287 mLabel = "Virtual";
288 break;
289 }
290 LOG(WARNING) << "Unsupported block major type " << majorId;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800291 return -ENOTSUP;
292 }
293 }
294
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600295 auto listener = VolumeManager::Instance()->getListener();
296 if (listener) listener->onDiskMetadataChanged(getId(),
297 mSize, mLabel, mSysPath);
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600298
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800299 return OK;
300}
301
302status_t Disk::readPartitions() {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600303 int maxMinors = getMaxMinors();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800304 if (maxMinors < 0) {
305 return -ENOTSUP;
306 }
307
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700308 destroyAllVolumes();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800309
310 // Parse partition table
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700311
312 std::vector<std::string> cmd;
313 cmd.push_back(kSgdiskPath);
314 cmd.push_back("--android-dump");
315 cmd.push_back(mDevPath);
316
317 std::vector<std::string> output;
318 status_t res = ForkExecvp(cmd, output);
319 if (res != OK) {
320 LOG(WARNING) << "sgdisk failed to scan " << mDevPath;
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600321
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600322 auto listener = VolumeManager::Instance()->getListener();
323 if (listener) listener->onDiskScanned(getId());
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600324
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700325 mJustPartitioned = false;
326 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800327 }
328
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800329 Table table = Table::kUnknown;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700330 bool foundParts = false;
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700331 for (const auto& line : output) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600332 auto split = android::base::Split(line, kSgdiskToken);
333 auto it = split.begin();
334 if (it == split.end()) continue;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700335
Jeff Sharkey3472e522017-10-06 18:02:53 -0600336 if (*it == "DISK") {
337 if (++it == split.end()) continue;
338 if (*it == "mbr") {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800339 table = Table::kMbr;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600340 } else if (*it == "gpt") {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800341 table = Table::kGpt;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600342 } else {
343 LOG(WARNING) << "Invalid partition table " << *it;
344 continue;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800345 }
Jeff Sharkey3472e522017-10-06 18:02:53 -0600346 } else if (*it == "PART") {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700347 foundParts = true;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600348
349 if (++it == split.end()) continue;
350 int i = 0;
351 if (!android::base::ParseInt(*it, &i, 1, maxMinors)) {
352 LOG(WARNING) << "Invalid partition number " << *it;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800353 continue;
354 }
355 dev_t partDevice = makedev(major(mDevice), minor(mDevice) + i);
356
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800357 if (table == Table::kMbr) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600358 if (++it == split.end()) continue;
359 int type = 0;
360 if (!android::base::ParseInt("0x" + *it, &type)) {
361 LOG(WARNING) << "Invalid partition type " << *it;
362 continue;
363 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800364
Jeff Sharkey3472e522017-10-06 18:02:53 -0600365 switch (type) {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800366 case 0x06: // FAT16
367 case 0x0b: // W95 FAT32 (LBA)
368 case 0x0c: // W95 FAT32 (LBA)
369 case 0x0e: // W95 FAT16 (LBA)
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700370 createPublicVolume(partDevice);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800371 break;
372 }
373 } else if (table == Table::kGpt) {
Jeff Sharkey3472e522017-10-06 18:02:53 -0600374 if (++it == split.end()) continue;
375 auto typeGuid = *it;
376 if (++it == split.end()) continue;
377 auto partGuid = *it;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800378
Jeff Sharkey3472e522017-10-06 18:02:53 -0600379 if (android::base::EqualsIgnoreCase(typeGuid, kGptBasicData)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700380 createPublicVolume(partDevice);
Jeff Sharkey3472e522017-10-06 18:02:53 -0600381 } else if (android::base::EqualsIgnoreCase(typeGuid, kGptAndroidExpand)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700382 createPrivateVolume(partDevice, partGuid);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800383 }
384 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800385 }
386 }
387
388 // Ugly last ditch effort, treat entire disk as partition
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700389 if (table == Table::kUnknown || !foundParts) {
390 LOG(WARNING) << mId << " has unknown partition table; trying entire device";
Jeff Sharkey63123c02015-06-26 11:16:14 -0700391
392 std::string fsType;
393 std::string unused;
Jeff Sharkey3472e522017-10-06 18:02:53 -0600394 if (ReadMetadataUntrusted(mDevPath, &fsType, &unused, &unused) == OK) {
Jeff Sharkey63123c02015-06-26 11:16:14 -0700395 createPublicVolume(mDevice);
396 } else {
397 LOG(WARNING) << mId << " failed to identify, giving up";
398 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800399 }
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700400
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600401 auto listener = VolumeManager::Instance()->getListener();
402 if (listener) listener->onDiskScanned(getId());
Jeff Sharkeycbe69fc2017-09-15 16:50:28 -0600403
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700404 mJustPartitioned = false;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800405 return OK;
406}
407
Jeff Sharkey9c484982015-03-31 10:35:33 -0700408status_t Disk::unmountAll() {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700409 for (const auto& vol : mVolumes) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700410 vol->unmount();
411 }
412 return OK;
413}
414
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800415status_t Disk::partitionPublic() {
Jeff Sharkeydadccee2015-09-23 14:13:45 -0700416 int res;
417
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700418 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700419 mJustPartitioned = true;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800420
Jeff Sharkeydadccee2015-09-23 14:13:45 -0700421 // First nuke any existing partition table
422 std::vector<std::string> cmd;
423 cmd.push_back(kSgdiskPath);
424 cmd.push_back("--zap-all");
425 cmd.push_back(mDevPath);
426
427 // Zap sometimes returns an error when it actually succeeded, so
428 // just log as warning and keep rolling forward.
429 if ((res = ForkExecvp(cmd)) != 0) {
430 LOG(WARNING) << "Failed to zap; status " << res;
431 }
432
Jeff Sharkey68f1b8b2017-10-18 14:09:52 -0600433 // Now let's build the new MBR table. We heavily rely on sgdisk to
434 // force optimal alignment on the created partitions.
435 cmd.clear();
436 cmd.push_back(kSgdiskPath);
437 cmd.push_back("--new=0:0:-0");
438 cmd.push_back("--typecode=0:0c00");
439 cmd.push_back("--gpttombr=1");
440 cmd.push_back(mDevPath);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800441
Jeff Sharkey68f1b8b2017-10-18 14:09:52 -0600442 if ((res = ForkExecvp(cmd)) != 0) {
443 LOG(ERROR) << "Failed to partition; status " << res;
444 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800445 }
446
Jeff Sharkey68f1b8b2017-10-18 14:09:52 -0600447 return OK;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800448}
449
450status_t Disk::partitionPrivate() {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700451 return partitionMixed(0);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800452}
453
454status_t Disk::partitionMixed(int8_t ratio) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700455 int res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700456
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600457 if (e4crypt_is_native()
458 && !android::base::GetBoolProperty("persist.sys.adoptable_fbe", false)) {
Jeff Sharkey15717512016-08-23 13:48:50 -0600459 LOG(ERROR) << "Private volumes not yet supported on FBE devices";
460 return -EINVAL;
461 }
462
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;
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600487 if (ReadRandomBytes(16, keyRaw) != OK) {
488 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;
566 if (!ReadFileToString(kSysfsMmcMaxMinors, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700567 LOG(ERROR) << "Failed to read max minors";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800568 return -errno;
569 }
Jeff Sharkey95440eb2017-09-18 18:19:28 -0600570 return std::stoi(tmp);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800571 }
Yu Ning942d4e82016-01-08 17:36:47 +0800572 default: {
573 if (isVirtioBlkDevice(majorId)) {
574 // drivers/block/virtio_blk.c has "#define PART_BITS 4", so max is
575 // 2^4 - 1 = 15
576 return 15;
577 }
578 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800579 }
580
Yu Ning942d4e82016-01-08 17:36:47 +0800581 LOG(ERROR) << "Unsupported block major type " << majorId;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800582 return -ENOTSUP;
583}
584
585} // namespace vold
586} // namespace android