blob: 151937fdbda4b83a0b3abb82e0c14f494f1d9a45 [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"
23#include "ResponseCode.h"
Jeff Sharkey15717512016-08-23 13:48:50 -060024#include "Ext4Crypt.h"
Jeff Sharkeydeb24052015-03-02 21:01:40 -080025
Elliott Hughes7e128fb2015-12-04 15:50:53 -080026#include <android-base/file.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>
29#include <android-base/logging.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080030#include <diskconfig/diskconfig.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080031
Jeff Sharkey9c484982015-03-31 10:35:33 -070032#include <vector>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080033#include <fcntl.h>
Jeff Sharkey38cfc022015-03-30 21:22:07 -070034#include <inttypes.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080035#include <stdio.h>
36#include <stdlib.h>
37#include <sys/types.h>
38#include <sys/stat.h>
Elliott Hughes0e08e842017-05-18 09:08:24 -070039#include <sys/sysmacros.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080040#include <sys/mount.h>
41
Dan Albertae9e8902015-03-16 10:35:17 -070042using android::base::ReadFileToString;
Jeff Sharkey9c484982015-03-31 10:35:33 -070043using android::base::WriteStringToFile;
Dan Albertae9e8902015-03-16 10:35:17 -070044using android::base::StringPrintf;
45
Jeff Sharkeydeb24052015-03-02 21:01:40 -080046namespace android {
47namespace vold {
48
49static const char* kSgdiskPath = "/system/bin/sgdisk";
50static const char* kSgdiskToken = " \t\n";
51
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060052static const char* kSysfsLoopMaxMinors = "/sys/module/loop/parameters/max_part";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080053static const char* kSysfsMmcMaxMinors = "/sys/module/mmcblk/parameters/perdev_minors";
54
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -060055static const unsigned int kMajorBlockLoop = 7;
Jeff Sharkeyf3ee2002015-04-19 15:55:42 -070056static const unsigned int kMajorBlockScsiA = 8;
57static const unsigned int kMajorBlockScsiB = 65;
58static const unsigned int kMajorBlockScsiC = 66;
59static const unsigned int kMajorBlockScsiD = 67;
60static const unsigned int kMajorBlockScsiE = 68;
61static const unsigned int kMajorBlockScsiF = 69;
62static const unsigned int kMajorBlockScsiG = 70;
63static const unsigned int kMajorBlockScsiH = 71;
64static const unsigned int kMajorBlockScsiI = 128;
65static const unsigned int kMajorBlockScsiJ = 129;
66static const unsigned int kMajorBlockScsiK = 130;
67static const unsigned int kMajorBlockScsiL = 131;
68static const unsigned int kMajorBlockScsiM = 132;
69static const unsigned int kMajorBlockScsiN = 133;
70static const unsigned int kMajorBlockScsiO = 134;
71static const unsigned int kMajorBlockScsiP = 135;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080072static const unsigned int kMajorBlockMmc = 179;
Yu Ning942d4e82016-01-08 17:36:47 +080073static const unsigned int kMajorBlockExperimentalMin = 240;
74static const unsigned int kMajorBlockExperimentalMax = 254;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080075
76static const char* kGptBasicData = "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7";
77static const char* kGptAndroidMeta = "19A710A2-B3CA-11E4-B026-10604B889DCF";
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070078static const char* kGptAndroidExpand = "193D1EA4-B3CA-11E4-B075-10604B889DCF";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080079
80enum class Table {
81 kUnknown,
82 kMbr,
83 kGpt,
84};
85
Yu Ning942d4e82016-01-08 17:36:47 +080086static bool isVirtioBlkDevice(unsigned int major) {
87 /*
88 * The new emulator's "ranchu" virtual board no longer includes a goldfish
89 * MMC-based SD card device; instead, it emulates SD cards with virtio-blk,
90 * which has been supported by upstream kernel and QEMU for quite a while.
91 * Unfortunately, the virtio-blk block device driver does not use a fixed
92 * major number, but relies on the kernel to assign one from a specific
93 * range of block majors, which are allocated for "LOCAL/EXPERIMENAL USE"
94 * per Documentation/devices.txt. This is true even for the latest Linux
95 * kernel (4.4; see init() in drivers/block/virtio_blk.c).
96 *
97 * This makes it difficult for vold to detect a virtio-blk based SD card.
98 * The current solution checks two conditions (both must be met):
99 *
100 * a) If the running environment is the emulator;
101 * b) If the major number is an experimental block device major number (for
102 * x86/x86_64 3.10 ranchu kernels, virtio-blk always gets major number
103 * 253, but it is safer to match the range than just one value).
104 *
105 * Other conditions could be used, too, e.g. the hardware name should be
106 * "ranchu", the device's sysfs path should end with "/block/vd[d-z]", etc.
107 * But just having a) and b) is enough for now.
108 */
109 return IsRunningInEmulator() && major >= kMajorBlockExperimentalMin
110 && major <= kMajorBlockExperimentalMax;
111}
112
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700113Disk::Disk(const std::string& eventPath, dev_t device,
114 const std::string& nickname, int flags) :
115 mDevice(device), mSize(-1), mNickname(nickname), mFlags(flags), mCreated(
116 false), mJustPartitioned(false) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700117 mId = StringPrintf("disk:%u,%u", major(device), minor(device));
118 mEventPath = eventPath;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800119 mSysPath = StringPrintf("/sys/%s", eventPath.c_str());
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700120 mDevPath = StringPrintf("/dev/block/vold/%s", mId.c_str());
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800121 CreateDeviceNode(mDevPath, mDevice);
122}
123
124Disk::~Disk() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700125 CHECK(!mCreated);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800126 DestroyDeviceNode(mDevPath);
127}
128
129std::shared_ptr<VolumeBase> Disk::findVolume(const std::string& id) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700130 for (auto vol : mVolumes) {
131 if (vol->getId() == id) {
132 return vol;
133 }
134 auto stackedVol = vol->findVolume(id);
135 if (stackedVol != nullptr) {
136 return stackedVol;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800137 }
138 }
139 return nullptr;
140}
141
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700142void Disk::listVolumes(VolumeBase::Type type, std::list<std::string>& list) {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700143 for (const auto& vol : mVolumes) {
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700144 if (vol->getType() == type) {
145 list.push_back(vol->getId());
146 }
147 // TODO: consider looking at stacked volumes
148 }
149}
150
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700151status_t Disk::create() {
152 CHECK(!mCreated);
153 mCreated = true;
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600154#if ENABLE_BINDER
155 auto listener = VolumeManager::Instance()->getListener();
156 if (listener) listener->onDiskCreated(getId(), mFlags);
157#else
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700158 notifyEvent(ResponseCode::DiskCreated, StringPrintf("%d", mFlags));
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600159#endif
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700160 readMetadata();
161 readPartitions();
162 return OK;
163}
164
165status_t Disk::destroy() {
166 CHECK(mCreated);
167 destroyAllVolumes();
168 mCreated = false;
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600169#if ENABLE_BINDER
170 auto listener = VolumeManager::Instance()->getListener();
171 if (listener) listener->onDiskDestroyed(getId());
172#else
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700173 notifyEvent(ResponseCode::DiskDestroyed);
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600174#endif
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 }
270 uint64_t manfid = strtoll(tmp.c_str(), nullptr, 16);
271 // Our goal here is to give the user a meaningful label, ideally
272 // matching whatever is silk-screened on the card. To reduce
273 // user confusion, this list doesn't contain white-label manfid.
274 switch (manfid) {
275 case 0x000003: mLabel = "SanDisk"; break;
276 case 0x00001b: mLabel = "Samsung"; break;
277 case 0x000028: mLabel = "Lexar"; break;
278 case 0x000074: mLabel = "Transcend"; break;
279 }
280 break;
281 }
282 default: {
Yu Ning942d4e82016-01-08 17:36:47 +0800283 if (isVirtioBlkDevice(majorId)) {
284 LOG(DEBUG) << "Recognized experimental block major ID " << majorId
285 << " as virtio-blk (emulator's virtual SD card device)";
286 mLabel = "Virtual";
287 break;
288 }
289 LOG(WARNING) << "Unsupported block major type " << majorId;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800290 return -ENOTSUP;
291 }
292 }
293
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600294#if ENABLE_BINDER
295 auto listener = VolumeManager::Instance()->getListener();
296 if (listener) listener->onDiskMetadataChanged(getId(),
297 mSize, mLabel, mSysPath);
298#else
Jeff Sharkeyd087bbc2016-03-10 12:11:09 -0700299 notifyEvent(ResponseCode::DiskSizeChanged, StringPrintf("%" PRIu64, mSize));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700300 notifyEvent(ResponseCode::DiskLabelChanged, mLabel);
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700301 notifyEvent(ResponseCode::DiskSysPathChanged, mSysPath);
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600302#endif
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800303 return OK;
304}
305
306status_t Disk::readPartitions() {
307 int8_t maxMinors = getMaxMinors();
308 if (maxMinors < 0) {
309 return -ENOTSUP;
310 }
311
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700312 destroyAllVolumes();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800313
314 // Parse partition table
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700315
316 std::vector<std::string> cmd;
317 cmd.push_back(kSgdiskPath);
318 cmd.push_back("--android-dump");
319 cmd.push_back(mDevPath);
320
321 std::vector<std::string> output;
322 status_t res = ForkExecvp(cmd, output);
323 if (res != OK) {
324 LOG(WARNING) << "sgdisk failed to scan " << mDevPath;
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600325#if ENABLE_BINDER
326 auto listener = VolumeManager::Instance()->getListener();
327 if (listener) listener->onDiskScanned(getId());
328#else
Jeff Sharkeyba6747f2015-04-28 21:17:43 -0700329 notifyEvent(ResponseCode::DiskScanned);
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600330#endif
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700331 mJustPartitioned = false;
332 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800333 }
334
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800335 Table table = Table::kUnknown;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700336 bool foundParts = false;
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700337 for (const auto& line : output) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700338 char* cline = (char*) line.c_str();
339 char* token = strtok(cline, kSgdiskToken);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700340 if (token == nullptr) continue;
341
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800342 if (!strcmp(token, "DISK")) {
343 const char* type = strtok(nullptr, kSgdiskToken);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800344 if (!strcmp(type, "mbr")) {
345 table = Table::kMbr;
346 } else if (!strcmp(type, "gpt")) {
347 table = Table::kGpt;
348 }
349 } else if (!strcmp(token, "PART")) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700350 foundParts = true;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800351 int i = strtol(strtok(nullptr, kSgdiskToken), nullptr, 10);
352 if (i <= 0 || i > maxMinors) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700353 LOG(WARNING) << mId << " is ignoring partition " << i
354 << " beyond max supported devices";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800355 continue;
356 }
357 dev_t partDevice = makedev(major(mDevice), minor(mDevice) + i);
358
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800359 if (table == Table::kMbr) {
360 const char* type = strtok(nullptr, kSgdiskToken);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800361
362 switch (strtol(type, nullptr, 16)) {
363 case 0x06: // FAT16
364 case 0x0b: // W95 FAT32 (LBA)
365 case 0x0c: // W95 FAT32 (LBA)
366 case 0x0e: // W95 FAT16 (LBA)
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700367 createPublicVolume(partDevice);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800368 break;
369 }
370 } else if (table == Table::kGpt) {
371 const char* typeGuid = strtok(nullptr, kSgdiskToken);
372 const char* partGuid = strtok(nullptr, kSgdiskToken);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800373
374 if (!strcasecmp(typeGuid, kGptBasicData)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700375 createPublicVolume(partDevice);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700376 } else if (!strcasecmp(typeGuid, kGptAndroidExpand)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700377 createPrivateVolume(partDevice, partGuid);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800378 }
379 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800380 }
381 }
382
383 // Ugly last ditch effort, treat entire disk as partition
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700384 if (table == Table::kUnknown || !foundParts) {
385 LOG(WARNING) << mId << " has unknown partition table; trying entire device";
Jeff Sharkey63123c02015-06-26 11:16:14 -0700386
387 std::string fsType;
388 std::string unused;
389 if (ReadMetadataUntrusted(mDevPath, fsType, unused, unused) == OK) {
390 createPublicVolume(mDevice);
391 } else {
392 LOG(WARNING) << mId << " failed to identify, giving up";
393 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800394 }
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700395
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600396#if ENABLE_BINDER
397 auto listener = VolumeManager::Instance()->getListener();
398 if (listener) listener->onDiskScanned(getId());
399#else
Jeff Sharkey613b26f2015-04-19 14:57:55 -0700400 notifyEvent(ResponseCode::DiskScanned);
Jeff Sharkey814e9d32017-09-13 11:49:44 -0600401#endif
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700402 mJustPartitioned = false;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800403 return OK;
404}
405
Jeff Sharkey9c484982015-03-31 10:35:33 -0700406status_t Disk::unmountAll() {
Chih-Hung Hsieh11a2ce82016-07-27 14:11:02 -0700407 for (const auto& vol : mVolumes) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700408 vol->unmount();
409 }
410 return OK;
411}
412
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800413status_t Disk::partitionPublic() {
Jeff Sharkeydadccee2015-09-23 14:13:45 -0700414 int res;
415
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800416 // TODO: improve this code
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700417 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700418 mJustPartitioned = true;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800419
Jeff Sharkeydadccee2015-09-23 14:13:45 -0700420 // First nuke any existing partition table
421 std::vector<std::string> cmd;
422 cmd.push_back(kSgdiskPath);
423 cmd.push_back("--zap-all");
424 cmd.push_back(mDevPath);
425
426 // Zap sometimes returns an error when it actually succeeded, so
427 // just log as warning and keep rolling forward.
428 if ((res = ForkExecvp(cmd)) != 0) {
429 LOG(WARNING) << "Failed to zap; status " << res;
430 }
431
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800432 struct disk_info dinfo;
433 memset(&dinfo, 0, sizeof(dinfo));
434
435 if (!(dinfo.part_lst = (struct part_info *) malloc(
436 MAX_NUM_PARTS * sizeof(struct part_info)))) {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800437 return -1;
438 }
439
440 memset(dinfo.part_lst, 0, MAX_NUM_PARTS * sizeof(struct part_info));
441 dinfo.device = strdup(mDevPath.c_str());
442 dinfo.scheme = PART_SCHEME_MBR;
443 dinfo.sect_size = 512;
444 dinfo.skip_lba = 2048;
445 dinfo.num_lba = 0;
446 dinfo.num_parts = 1;
447
448 struct part_info *pinfo = &dinfo.part_lst[0];
449
450 pinfo->name = strdup("android_sdcard");
451 pinfo->flags |= PART_ACTIVE_FLAG;
452 pinfo->type = PC_PART_TYPE_FAT32;
453 pinfo->len_kb = -1;
454
455 int rc = apply_disk_config(&dinfo, 0);
456 if (rc) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700457 LOG(ERROR) << "Failed to apply disk configuration: " << rc;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800458 goto out;
459 }
460
461out:
462 free(pinfo->name);
463 free(dinfo.device);
464 free(dinfo.part_lst);
465
466 return rc;
467}
468
469status_t Disk::partitionPrivate() {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700470 return partitionMixed(0);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800471}
472
473status_t Disk::partitionMixed(int8_t ratio) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700474 int res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700475
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600476 if (e4crypt_is_native()
477 && !android::base::GetBoolProperty("persist.sys.adoptable_fbe", false)) {
Jeff Sharkey15717512016-08-23 13:48:50 -0600478 LOG(ERROR) << "Private volumes not yet supported on FBE devices";
479 return -EINVAL;
480 }
481
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700482 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700483 mJustPartitioned = true;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700484
485 // First nuke any existing partition table
486 std::vector<std::string> cmd;
487 cmd.push_back(kSgdiskPath);
488 cmd.push_back("--zap-all");
489 cmd.push_back(mDevPath);
490
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700491 // Zap sometimes returns an error when it actually succeeded, so
492 // just log as warning and keep rolling forward.
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700493 if ((res = ForkExecvp(cmd)) != 0) {
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700494 LOG(WARNING) << "Failed to zap; status " << res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700495 }
496
497 // We've had some success above, so generate both the private partition
498 // GUID and encryption key and persist them.
499 std::string partGuidRaw;
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600500 if (GenerateRandomUuid(partGuidRaw) != OK) {
501 LOG(ERROR) << "Failed to generate GUID";
502 return -EIO;
503 }
504
Jeff Sharkey9c484982015-03-31 10:35:33 -0700505 std::string keyRaw;
Jeff Sharkey46bb69f2017-06-21 13:52:23 -0600506 if (ReadRandomBytes(16, keyRaw) != OK) {
507 LOG(ERROR) << "Failed to generate key";
Jeff Sharkey9c484982015-03-31 10:35:33 -0700508 return -EIO;
509 }
510
511 std::string partGuid;
512 StrToHex(partGuidRaw, partGuid);
513
514 if (!WriteStringToFile(keyRaw, BuildKeyPath(partGuid))) {
515 LOG(ERROR) << "Failed to persist key";
516 return -EIO;
517 } else {
518 LOG(DEBUG) << "Persisted key for GUID " << partGuid;
519 }
520
521 // Now let's build the new GPT table. We heavily rely on sgdisk to
522 // force optimal alignment on the created partitions.
523 cmd.clear();
524 cmd.push_back(kSgdiskPath);
525
526 // If requested, create a public partition first. Mixed-mode partitioning
527 // like this is an experimental feature.
528 if (ratio > 0) {
529 if (ratio < 10 || ratio > 90) {
530 LOG(ERROR) << "Mixed partition ratio must be between 10-90%";
531 return -EINVAL;
532 }
533
534 uint64_t splitMb = ((mSize / 100) * ratio) / 1024 / 1024;
535 cmd.push_back(StringPrintf("--new=0:0:+%" PRId64 "M", splitMb));
536 cmd.push_back(StringPrintf("--typecode=0:%s", kGptBasicData));
537 cmd.push_back("--change-name=0:shared");
538 }
539
540 // Define a metadata partition which is designed for future use; there
541 // should only be one of these per physical device, even if there are
542 // multiple private volumes.
543 cmd.push_back("--new=0:0:+16M");
544 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidMeta));
545 cmd.push_back("--change-name=0:android_meta");
546
547 // Define a single private partition filling the rest of disk.
548 cmd.push_back("--new=0:0:-0");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700549 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidExpand));
Jeff Sharkey9c484982015-03-31 10:35:33 -0700550 cmd.push_back(StringPrintf("--partition-guid=0:%s", partGuid.c_str()));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700551 cmd.push_back("--change-name=0:android_expand");
Jeff Sharkey9c484982015-03-31 10:35:33 -0700552
553 cmd.push_back(mDevPath);
554
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700555 if ((res = ForkExecvp(cmd)) != 0) {
556 LOG(ERROR) << "Failed to partition; status " << res;
557 return res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700558 }
559
560 return OK;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800561}
562
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700563void Disk::notifyEvent(int event) {
564 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(event,
565 getId().c_str(), false);
566}
567
568void Disk::notifyEvent(int event, const std::string& value) {
569 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(event,
570 StringPrintf("%s %s", getId().c_str(), value.c_str()).c_str(), false);
571}
572
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800573int Disk::getMaxMinors() {
574 // Figure out maximum partition devices supported
Yu Ning942d4e82016-01-08 17:36:47 +0800575 unsigned int majorId = major(mDevice);
576 switch (majorId) {
Jeff Sharkeyfa1c6772017-03-25 22:49:13 -0600577 case kMajorBlockLoop: {
578 std::string tmp;
579 if (!ReadFileToString(kSysfsLoopMaxMinors, &tmp)) {
580 LOG(ERROR) << "Failed to read max minors";
581 return -errno;
582 }
583 return atoi(tmp.c_str());
584 }
Jeff Sharkeyf3ee2002015-04-19 15:55:42 -0700585 case kMajorBlockScsiA: case kMajorBlockScsiB: case kMajorBlockScsiC: case kMajorBlockScsiD:
586 case kMajorBlockScsiE: case kMajorBlockScsiF: case kMajorBlockScsiG: case kMajorBlockScsiH:
587 case kMajorBlockScsiI: case kMajorBlockScsiJ: case kMajorBlockScsiK: case kMajorBlockScsiL:
588 case kMajorBlockScsiM: case kMajorBlockScsiN: case kMajorBlockScsiO: case kMajorBlockScsiP: {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800589 // Per Documentation/devices.txt this is static
590 return 15;
591 }
592 case kMajorBlockMmc: {
593 // Per Documentation/devices.txt this is dynamic
594 std::string tmp;
595 if (!ReadFileToString(kSysfsMmcMaxMinors, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700596 LOG(ERROR) << "Failed to read max minors";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800597 return -errno;
598 }
599 return atoi(tmp.c_str());
600 }
Yu Ning942d4e82016-01-08 17:36:47 +0800601 default: {
602 if (isVirtioBlkDevice(majorId)) {
603 // drivers/block/virtio_blk.c has "#define PART_BITS 4", so max is
604 // 2^4 - 1 = 15
605 return 15;
606 }
607 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800608 }
609
Yu Ning942d4e82016-01-08 17:36:47 +0800610 LOG(ERROR) << "Unsupported block major type " << majorId;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800611 return -ENOTSUP;
612}
613
614} // namespace vold
615} // namespace android