blob: 833db1df95ffefaf6001d3995702d3e0fd6ee1e3 [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 Sharkeydeb24052015-03-02 21:01:40 -080024
Dan Albertae9e8902015-03-16 10:35:17 -070025#include <base/file.h>
26#include <base/stringprintf.h>
Jeff Sharkey36801cc2015-03-13 16:09:20 -070027#include <base/logging.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080028#include <diskconfig/diskconfig.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080029
Jeff Sharkey9c484982015-03-31 10:35:33 -070030#include <vector>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080031#include <fcntl.h>
Jeff Sharkey38cfc022015-03-30 21:22:07 -070032#include <inttypes.h>
Jeff Sharkeydeb24052015-03-02 21:01:40 -080033#include <stdio.h>
34#include <stdlib.h>
35#include <sys/types.h>
36#include <sys/stat.h>
37#include <sys/mount.h>
38
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -070039#define ENTIRE_DEVICE_FALLBACK 0
40
Dan Albertae9e8902015-03-16 10:35:17 -070041using android::base::ReadFileToString;
Jeff Sharkey9c484982015-03-31 10:35:33 -070042using android::base::WriteStringToFile;
Dan Albertae9e8902015-03-16 10:35:17 -070043using android::base::StringPrintf;
44
Jeff Sharkeydeb24052015-03-02 21:01:40 -080045namespace android {
46namespace vold {
47
48static const char* kSgdiskPath = "/system/bin/sgdisk";
49static const char* kSgdiskToken = " \t\n";
50
51static const char* kSysfsMmcMaxMinors = "/sys/module/mmcblk/parameters/perdev_minors";
52
53static const unsigned int kMajorBlockScsi = 8;
54static const unsigned int kMajorBlockMmc = 179;
55
56static const char* kGptBasicData = "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7";
57static const char* kGptAndroidMeta = "19A710A2-B3CA-11E4-B026-10604B889DCF";
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070058static const char* kGptAndroidExpand = "193D1EA4-B3CA-11E4-B075-10604B889DCF";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080059
Jeff Sharkey9c484982015-03-31 10:35:33 -070060static const char* kKeyPath = "/data/misc/vold";
61
Jeff Sharkeydeb24052015-03-02 21:01:40 -080062enum class Table {
63 kUnknown,
64 kMbr,
65 kGpt,
66};
67
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070068Disk::Disk(const std::string& eventPath, dev_t device,
69 const std::string& nickname, int flags) :
70 mDevice(device), mSize(-1), mNickname(nickname), mFlags(flags), mCreated(
71 false), mJustPartitioned(false) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -070072 mId = StringPrintf("disk:%u,%u", major(device), minor(device));
73 mEventPath = eventPath;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080074 mSysPath = StringPrintf("/sys/%s", eventPath.c_str());
Jeff Sharkey36801cc2015-03-13 16:09:20 -070075 mDevPath = StringPrintf("/dev/block/vold/%s", mId.c_str());
Jeff Sharkeydeb24052015-03-02 21:01:40 -080076 CreateDeviceNode(mDevPath, mDevice);
77}
78
79Disk::~Disk() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -070080 CHECK(!mCreated);
Jeff Sharkeydeb24052015-03-02 21:01:40 -080081 DestroyDeviceNode(mDevPath);
82}
83
84std::shared_ptr<VolumeBase> Disk::findVolume(const std::string& id) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -070085 for (auto vol : mVolumes) {
86 if (vol->getId() == id) {
87 return vol;
88 }
89 auto stackedVol = vol->findVolume(id);
90 if (stackedVol != nullptr) {
91 return stackedVol;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080092 }
93 }
94 return nullptr;
95}
96
Jeff Sharkey36801cc2015-03-13 16:09:20 -070097status_t Disk::create() {
98 CHECK(!mCreated);
99 mCreated = true;
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700100 notifyEvent(ResponseCode::DiskCreated, StringPrintf("%d", mFlags));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700101 readMetadata();
102 readPartitions();
103 return OK;
104}
105
106status_t Disk::destroy() {
107 CHECK(mCreated);
108 destroyAllVolumes();
109 mCreated = false;
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700110 notifyEvent(ResponseCode::DiskDestroyed);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700111 return OK;
112}
113
Jeff Sharkey9c484982015-03-31 10:35:33 -0700114static std::string BuildKeyPath(const std::string& partGuid) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700115 return StringPrintf("%s/expand_%s.key", kKeyPath, partGuid.c_str());
Jeff Sharkey9c484982015-03-31 10:35:33 -0700116}
117
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700118void Disk::createPublicVolume(dev_t device) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700119 auto vol = std::shared_ptr<VolumeBase>(new PublicVolume(device));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700120 if (mJustPartitioned) {
121 LOG(DEBUG) << "Device just partitioned; silently formatting";
122 vol->setSilent(true);
123 vol->create();
124 vol->format();
125 vol->destroy();
126 vol->setSilent(false);
127 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700128
Jeff Sharkey9c484982015-03-31 10:35:33 -0700129 mVolumes.push_back(vol);
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700130 vol->setDiskId(getId());
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700131 vol->create();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700132}
133
Jeff Sharkey9c484982015-03-31 10:35:33 -0700134void Disk::createPrivateVolume(dev_t device, const std::string& partGuid) {
135 std::string tmp;
136 std::string normalizedGuid;
137 if (HexToStr(partGuid, tmp)) {
138 LOG(WARNING) << "Invalid GUID " << partGuid;
139 return;
140 }
141 StrToHex(tmp, normalizedGuid);
142
143 std::string keyRaw;
144 if (!ReadFileToString(BuildKeyPath(normalizedGuid), &keyRaw)) {
145 PLOG(ERROR) << "Failed to load key for GUID " << normalizedGuid;
146 return;
147 }
148
149 LOG(DEBUG) << "Found key for GUID " << normalizedGuid;
150
151 auto vol = std::shared_ptr<VolumeBase>(new PrivateVolume(device, keyRaw));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700152 if (mJustPartitioned) {
153 LOG(DEBUG) << "Device just partitioned; silently formatting";
154 vol->setSilent(true);
155 vol->create();
156 vol->format();
157 vol->destroy();
158 vol->setSilent(false);
159 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700160
161 mVolumes.push_back(vol);
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700162 vol->setDiskId(getId());
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700163 vol->create();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700164}
165
166void Disk::destroyAllVolumes() {
167 for (auto vol : mVolumes) {
168 vol->destroy();
169 }
170 mVolumes.clear();
171}
172
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800173status_t Disk::readMetadata() {
174 mSize = -1;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700175 mLabel.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800176
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700177 int fd = open(mDevPath.c_str(), O_RDONLY | O_CLOEXEC);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700178 if (fd != -1) {
179 if (ioctl(fd, BLKGETSIZE64, &mSize)) {
180 mSize = -1;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800181 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700182 close(fd);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800183 }
184
185 switch (major(mDevice)) {
186 case kMajorBlockScsi: {
187 std::string path(mSysPath + "/device/vendor");
188 std::string tmp;
189 if (!ReadFileToString(path, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700190 PLOG(WARNING) << "Failed to read vendor from " << path;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800191 return -errno;
192 }
193 mLabel = tmp;
194 break;
195 }
196 case kMajorBlockMmc: {
197 std::string path(mSysPath + "/device/manfid");
198 std::string tmp;
199 if (!ReadFileToString(path, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700200 PLOG(WARNING) << "Failed to read manufacturer from " << path;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800201 return -errno;
202 }
203 uint64_t manfid = strtoll(tmp.c_str(), nullptr, 16);
204 // Our goal here is to give the user a meaningful label, ideally
205 // matching whatever is silk-screened on the card. To reduce
206 // user confusion, this list doesn't contain white-label manfid.
207 switch (manfid) {
208 case 0x000003: mLabel = "SanDisk"; break;
209 case 0x00001b: mLabel = "Samsung"; break;
210 case 0x000028: mLabel = "Lexar"; break;
211 case 0x000074: mLabel = "Transcend"; break;
212 }
213 break;
214 }
215 default: {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700216 LOG(WARNING) << "Unsupported block major type" << major(mDevice);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800217 return -ENOTSUP;
218 }
219 }
220
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700221 notifyEvent(ResponseCode::DiskSizeChanged, StringPrintf("%" PRId64, mSize));
222 notifyEvent(ResponseCode::DiskLabelChanged, mLabel);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800223 return OK;
224}
225
226status_t Disk::readPartitions() {
Jeff Sharkey7d9d0112015-04-14 23:14:23 -0700227 std::lock_guard<std::mutex> lock(mLock);
228
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800229 int8_t maxMinors = getMaxMinors();
230 if (maxMinors < 0) {
231 return -ENOTSUP;
232 }
233
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700234 destroyAllVolumes();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800235
236 // Parse partition table
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700237
238 std::vector<std::string> cmd;
239 cmd.push_back(kSgdiskPath);
240 cmd.push_back("--android-dump");
241 cmd.push_back(mDevPath);
242
243 std::vector<std::string> output;
244 status_t res = ForkExecvp(cmd, output);
245 if (res != OK) {
246 LOG(WARNING) << "sgdisk failed to scan " << mDevPath;
247 mJustPartitioned = false;
248 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800249 }
250
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800251 Table table = Table::kUnknown;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700252 bool foundParts = false;
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700253 for (auto line : output) {
254 char* cline = (char*) line.c_str();
255 char* token = strtok(cline, kSgdiskToken);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700256 if (token == nullptr) continue;
257
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800258 if (!strcmp(token, "DISK")) {
259 const char* type = strtok(nullptr, kSgdiskToken);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800260 if (!strcmp(type, "mbr")) {
261 table = Table::kMbr;
262 } else if (!strcmp(type, "gpt")) {
263 table = Table::kGpt;
264 }
265 } else if (!strcmp(token, "PART")) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700266 foundParts = true;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800267 int i = strtol(strtok(nullptr, kSgdiskToken), nullptr, 10);
268 if (i <= 0 || i > maxMinors) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700269 LOG(WARNING) << mId << " is ignoring partition " << i
270 << " beyond max supported devices";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800271 continue;
272 }
273 dev_t partDevice = makedev(major(mDevice), minor(mDevice) + i);
274
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800275 if (table == Table::kMbr) {
276 const char* type = strtok(nullptr, kSgdiskToken);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800277
278 switch (strtol(type, nullptr, 16)) {
279 case 0x06: // FAT16
280 case 0x0b: // W95 FAT32 (LBA)
281 case 0x0c: // W95 FAT32 (LBA)
282 case 0x0e: // W95 FAT16 (LBA)
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700283 createPublicVolume(partDevice);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800284 break;
285 }
286 } else if (table == Table::kGpt) {
287 const char* typeGuid = strtok(nullptr, kSgdiskToken);
288 const char* partGuid = strtok(nullptr, kSgdiskToken);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800289
290 if (!strcasecmp(typeGuid, kGptBasicData)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700291 createPublicVolume(partDevice);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700292 } else if (!strcasecmp(typeGuid, kGptAndroidExpand)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700293 createPrivateVolume(partDevice, partGuid);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800294 }
295 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800296 }
297 }
298
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700299#if ENTIRE_DEVICE_FALLBACK
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800300 // Ugly last ditch effort, treat entire disk as partition
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700301 if (table == Table::kUnknown || !foundParts) {
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700302 // TODO: use blkid to confirm filesystem before doing this
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700303 LOG(WARNING) << mId << " has unknown partition table; trying entire device";
304 createPublicVolume(mDevice);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800305 }
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700306#endif
307
Jeff Sharkey613b26f2015-04-19 14:57:55 -0700308 notifyEvent(ResponseCode::DiskScanned);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800309
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700310 mJustPartitioned = false;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800311 return OK;
312}
313
Jeff Sharkey9c484982015-03-31 10:35:33 -0700314status_t Disk::unmountAll() {
315 for (auto vol : mVolumes) {
316 vol->unmount();
317 }
318 return OK;
319}
320
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800321status_t Disk::partitionPublic() {
Jeff Sharkey7d9d0112015-04-14 23:14:23 -0700322 std::lock_guard<std::mutex> lock(mLock);
323
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800324 // TODO: improve this code
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700325 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700326 mJustPartitioned = true;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800327
328 struct disk_info dinfo;
329 memset(&dinfo, 0, sizeof(dinfo));
330
331 if (!(dinfo.part_lst = (struct part_info *) malloc(
332 MAX_NUM_PARTS * sizeof(struct part_info)))) {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800333 return -1;
334 }
335
336 memset(dinfo.part_lst, 0, MAX_NUM_PARTS * sizeof(struct part_info));
337 dinfo.device = strdup(mDevPath.c_str());
338 dinfo.scheme = PART_SCHEME_MBR;
339 dinfo.sect_size = 512;
340 dinfo.skip_lba = 2048;
341 dinfo.num_lba = 0;
342 dinfo.num_parts = 1;
343
344 struct part_info *pinfo = &dinfo.part_lst[0];
345
346 pinfo->name = strdup("android_sdcard");
347 pinfo->flags |= PART_ACTIVE_FLAG;
348 pinfo->type = PC_PART_TYPE_FAT32;
349 pinfo->len_kb = -1;
350
351 int rc = apply_disk_config(&dinfo, 0);
352 if (rc) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700353 LOG(ERROR) << "Failed to apply disk configuration: " << rc;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800354 goto out;
355 }
356
357out:
358 free(pinfo->name);
359 free(dinfo.device);
360 free(dinfo.part_lst);
361
362 return rc;
363}
364
365status_t Disk::partitionPrivate() {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700366 return partitionMixed(0);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800367}
368
369status_t Disk::partitionMixed(int8_t ratio) {
Jeff Sharkey7d9d0112015-04-14 23:14:23 -0700370 std::lock_guard<std::mutex> lock(mLock);
371
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700372 int res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700373
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700374 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700375 mJustPartitioned = true;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700376
377 // First nuke any existing partition table
378 std::vector<std::string> cmd;
379 cmd.push_back(kSgdiskPath);
380 cmd.push_back("--zap-all");
381 cmd.push_back(mDevPath);
382
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700383 // Zap sometimes returns an error when it actually succeeded, so
384 // just log as warning and keep rolling forward.
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700385 if ((res = ForkExecvp(cmd)) != 0) {
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700386 LOG(WARNING) << "Failed to zap; status " << res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700387 }
388
389 // We've had some success above, so generate both the private partition
390 // GUID and encryption key and persist them.
391 std::string partGuidRaw;
392 std::string keyRaw;
393 if (ReadRandomBytes(16, partGuidRaw) || ReadRandomBytes(16, keyRaw)) {
394 LOG(ERROR) << "Failed to generate GUID or key";
395 return -EIO;
396 }
397
398 std::string partGuid;
399 StrToHex(partGuidRaw, partGuid);
400
401 if (!WriteStringToFile(keyRaw, BuildKeyPath(partGuid))) {
402 LOG(ERROR) << "Failed to persist key";
403 return -EIO;
404 } else {
405 LOG(DEBUG) << "Persisted key for GUID " << partGuid;
406 }
407
408 // Now let's build the new GPT table. We heavily rely on sgdisk to
409 // force optimal alignment on the created partitions.
410 cmd.clear();
411 cmd.push_back(kSgdiskPath);
412
413 // If requested, create a public partition first. Mixed-mode partitioning
414 // like this is an experimental feature.
415 if (ratio > 0) {
416 if (ratio < 10 || ratio > 90) {
417 LOG(ERROR) << "Mixed partition ratio must be between 10-90%";
418 return -EINVAL;
419 }
420
421 uint64_t splitMb = ((mSize / 100) * ratio) / 1024 / 1024;
422 cmd.push_back(StringPrintf("--new=0:0:+%" PRId64 "M", splitMb));
423 cmd.push_back(StringPrintf("--typecode=0:%s", kGptBasicData));
424 cmd.push_back("--change-name=0:shared");
425 }
426
427 // Define a metadata partition which is designed for future use; there
428 // should only be one of these per physical device, even if there are
429 // multiple private volumes.
430 cmd.push_back("--new=0:0:+16M");
431 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidMeta));
432 cmd.push_back("--change-name=0:android_meta");
433
434 // Define a single private partition filling the rest of disk.
435 cmd.push_back("--new=0:0:-0");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700436 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidExpand));
Jeff Sharkey9c484982015-03-31 10:35:33 -0700437 cmd.push_back(StringPrintf("--partition-guid=0:%s", partGuid.c_str()));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700438 cmd.push_back("--change-name=0:android_expand");
Jeff Sharkey9c484982015-03-31 10:35:33 -0700439
440 cmd.push_back(mDevPath);
441
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700442 if ((res = ForkExecvp(cmd)) != 0) {
443 LOG(ERROR) << "Failed to partition; status " << res;
444 return res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700445 }
446
447 return OK;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800448}
449
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700450void Disk::notifyEvent(int event) {
451 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(event,
452 getId().c_str(), false);
453}
454
455void Disk::notifyEvent(int event, const std::string& value) {
456 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(event,
457 StringPrintf("%s %s", getId().c_str(), value.c_str()).c_str(), false);
458}
459
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800460int Disk::getMaxMinors() {
461 // Figure out maximum partition devices supported
462 switch (major(mDevice)) {
463 case kMajorBlockScsi: {
464 // Per Documentation/devices.txt this is static
465 return 15;
466 }
467 case kMajorBlockMmc: {
468 // Per Documentation/devices.txt this is dynamic
469 std::string tmp;
470 if (!ReadFileToString(kSysfsMmcMaxMinors, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700471 LOG(ERROR) << "Failed to read max minors";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800472 return -errno;
473 }
474 return atoi(tmp.c_str());
475 }
476 }
477
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700478 LOG(ERROR) << "Unsupported block major type " << major(mDevice);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800479 return -ENOTSUP;
480}
481
482} // namespace vold
483} // namespace android