blob: 1e76beecfbbe54348b3d70a0b13e470f127e7aba [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
Dan Albertae9e8902015-03-16 10:35:17 -070039using android::base::ReadFileToString;
Jeff Sharkey9c484982015-03-31 10:35:33 -070040using android::base::WriteStringToFile;
Dan Albertae9e8902015-03-16 10:35:17 -070041using android::base::StringPrintf;
42
Jeff Sharkeydeb24052015-03-02 21:01:40 -080043namespace android {
44namespace vold {
45
46static const char* kSgdiskPath = "/system/bin/sgdisk";
47static const char* kSgdiskToken = " \t\n";
48
49static const char* kSysfsMmcMaxMinors = "/sys/module/mmcblk/parameters/perdev_minors";
50
Jeff Sharkeyf3ee2002015-04-19 15:55:42 -070051static const unsigned int kMajorBlockScsiA = 8;
52static const unsigned int kMajorBlockScsiB = 65;
53static const unsigned int kMajorBlockScsiC = 66;
54static const unsigned int kMajorBlockScsiD = 67;
55static const unsigned int kMajorBlockScsiE = 68;
56static const unsigned int kMajorBlockScsiF = 69;
57static const unsigned int kMajorBlockScsiG = 70;
58static const unsigned int kMajorBlockScsiH = 71;
59static const unsigned int kMajorBlockScsiI = 128;
60static const unsigned int kMajorBlockScsiJ = 129;
61static const unsigned int kMajorBlockScsiK = 130;
62static const unsigned int kMajorBlockScsiL = 131;
63static const unsigned int kMajorBlockScsiM = 132;
64static const unsigned int kMajorBlockScsiN = 133;
65static const unsigned int kMajorBlockScsiO = 134;
66static const unsigned int kMajorBlockScsiP = 135;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080067static const unsigned int kMajorBlockMmc = 179;
68
69static const char* kGptBasicData = "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7";
70static const char* kGptAndroidMeta = "19A710A2-B3CA-11E4-B026-10604B889DCF";
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070071static const char* kGptAndroidExpand = "193D1EA4-B3CA-11E4-B075-10604B889DCF";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080072
73enum class Table {
74 kUnknown,
75 kMbr,
76 kGpt,
77};
78
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070079Disk::Disk(const std::string& eventPath, dev_t device,
80 const std::string& nickname, int flags) :
81 mDevice(device), mSize(-1), mNickname(nickname), mFlags(flags), mCreated(
82 false), mJustPartitioned(false) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -070083 mId = StringPrintf("disk:%u,%u", major(device), minor(device));
84 mEventPath = eventPath;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080085 mSysPath = StringPrintf("/sys/%s", eventPath.c_str());
Jeff Sharkey36801cc2015-03-13 16:09:20 -070086 mDevPath = StringPrintf("/dev/block/vold/%s", mId.c_str());
Jeff Sharkeydeb24052015-03-02 21:01:40 -080087 CreateDeviceNode(mDevPath, mDevice);
88}
89
90Disk::~Disk() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -070091 CHECK(!mCreated);
Jeff Sharkeydeb24052015-03-02 21:01:40 -080092 DestroyDeviceNode(mDevPath);
93}
94
95std::shared_ptr<VolumeBase> Disk::findVolume(const std::string& id) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -070096 for (auto vol : mVolumes) {
97 if (vol->getId() == id) {
98 return vol;
99 }
100 auto stackedVol = vol->findVolume(id);
101 if (stackedVol != nullptr) {
102 return stackedVol;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800103 }
104 }
105 return nullptr;
106}
107
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700108void Disk::listVolumes(VolumeBase::Type type, std::list<std::string>& list) {
109 for (auto vol : mVolumes) {
110 if (vol->getType() == type) {
111 list.push_back(vol->getId());
112 }
113 // TODO: consider looking at stacked volumes
114 }
115}
116
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700117status_t Disk::create() {
118 CHECK(!mCreated);
119 mCreated = true;
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700120 notifyEvent(ResponseCode::DiskCreated, StringPrintf("%d", mFlags));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700121 readMetadata();
122 readPartitions();
123 return OK;
124}
125
126status_t Disk::destroy() {
127 CHECK(mCreated);
128 destroyAllVolumes();
129 mCreated = false;
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700130 notifyEvent(ResponseCode::DiskDestroyed);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700131 return OK;
132}
133
134void Disk::createPublicVolume(dev_t device) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700135 auto vol = std::shared_ptr<VolumeBase>(new PublicVolume(device));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700136 if (mJustPartitioned) {
137 LOG(DEBUG) << "Device just partitioned; silently formatting";
138 vol->setSilent(true);
139 vol->create();
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700140 vol->format("auto");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700141 vol->destroy();
142 vol->setSilent(false);
143 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700144
Jeff Sharkey9c484982015-03-31 10:35:33 -0700145 mVolumes.push_back(vol);
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700146 vol->setDiskId(getId());
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700147 vol->create();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700148}
149
Jeff Sharkey9c484982015-03-31 10:35:33 -0700150void Disk::createPrivateVolume(dev_t device, const std::string& partGuid) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700151 std::string normalizedGuid;
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700152 if (NormalizeHex(partGuid, normalizedGuid)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700153 LOG(WARNING) << "Invalid GUID " << partGuid;
154 return;
155 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700156
157 std::string keyRaw;
158 if (!ReadFileToString(BuildKeyPath(normalizedGuid), &keyRaw)) {
159 PLOG(ERROR) << "Failed to load key for GUID " << normalizedGuid;
160 return;
161 }
162
163 LOG(DEBUG) << "Found key for GUID " << normalizedGuid;
164
165 auto vol = std::shared_ptr<VolumeBase>(new PrivateVolume(device, keyRaw));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700166 if (mJustPartitioned) {
167 LOG(DEBUG) << "Device just partitioned; silently formatting";
168 vol->setSilent(true);
169 vol->create();
Jeff Sharkeyd0640f62015-05-21 22:35:42 -0700170 vol->format("auto");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700171 vol->destroy();
172 vol->setSilent(false);
173 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700174
175 mVolumes.push_back(vol);
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700176 vol->setDiskId(getId());
Jeff Sharkeybc40cc82015-06-18 14:25:08 -0700177 vol->setPartGuid(partGuid);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700178 vol->create();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700179}
180
181void Disk::destroyAllVolumes() {
182 for (auto vol : mVolumes) {
183 vol->destroy();
184 }
185 mVolumes.clear();
186}
187
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800188status_t Disk::readMetadata() {
189 mSize = -1;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700190 mLabel.clear();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800191
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700192 int fd = open(mDevPath.c_str(), O_RDONLY | O_CLOEXEC);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700193 if (fd != -1) {
194 if (ioctl(fd, BLKGETSIZE64, &mSize)) {
195 mSize = -1;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800196 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700197 close(fd);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800198 }
199
200 switch (major(mDevice)) {
Jeff Sharkeyf3ee2002015-04-19 15:55:42 -0700201 case kMajorBlockScsiA: case kMajorBlockScsiB: case kMajorBlockScsiC: case kMajorBlockScsiD:
202 case kMajorBlockScsiE: case kMajorBlockScsiF: case kMajorBlockScsiG: case kMajorBlockScsiH:
203 case kMajorBlockScsiI: case kMajorBlockScsiJ: case kMajorBlockScsiK: case kMajorBlockScsiL:
204 case kMajorBlockScsiM: case kMajorBlockScsiN: case kMajorBlockScsiO: case kMajorBlockScsiP: {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800205 std::string path(mSysPath + "/device/vendor");
206 std::string tmp;
207 if (!ReadFileToString(path, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700208 PLOG(WARNING) << "Failed to read vendor from " << path;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800209 return -errno;
210 }
211 mLabel = tmp;
212 break;
213 }
214 case kMajorBlockMmc: {
215 std::string path(mSysPath + "/device/manfid");
216 std::string tmp;
217 if (!ReadFileToString(path, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700218 PLOG(WARNING) << "Failed to read manufacturer from " << path;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800219 return -errno;
220 }
221 uint64_t manfid = strtoll(tmp.c_str(), nullptr, 16);
222 // Our goal here is to give the user a meaningful label, ideally
223 // matching whatever is silk-screened on the card. To reduce
224 // user confusion, this list doesn't contain white-label manfid.
225 switch (manfid) {
226 case 0x000003: mLabel = "SanDisk"; break;
227 case 0x00001b: mLabel = "Samsung"; break;
228 case 0x000028: mLabel = "Lexar"; break;
229 case 0x000074: mLabel = "Transcend"; break;
230 }
231 break;
232 }
233 default: {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700234 LOG(WARNING) << "Unsupported block major type" << major(mDevice);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800235 return -ENOTSUP;
236 }
237 }
238
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700239 notifyEvent(ResponseCode::DiskSizeChanged, StringPrintf("%" PRId64, mSize));
240 notifyEvent(ResponseCode::DiskLabelChanged, mLabel);
Jeff Sharkeyc86ab6f2015-06-26 14:02:09 -0700241 notifyEvent(ResponseCode::DiskSysPathChanged, mSysPath);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800242 return OK;
243}
244
245status_t Disk::readPartitions() {
246 int8_t maxMinors = getMaxMinors();
247 if (maxMinors < 0) {
248 return -ENOTSUP;
249 }
250
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700251 destroyAllVolumes();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800252
253 // Parse partition table
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700254
255 std::vector<std::string> cmd;
256 cmd.push_back(kSgdiskPath);
257 cmd.push_back("--android-dump");
258 cmd.push_back(mDevPath);
259
260 std::vector<std::string> output;
261 status_t res = ForkExecvp(cmd, output);
262 if (res != OK) {
263 LOG(WARNING) << "sgdisk failed to scan " << mDevPath;
Jeff Sharkeyba6747f2015-04-28 21:17:43 -0700264 notifyEvent(ResponseCode::DiskScanned);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700265 mJustPartitioned = false;
266 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800267 }
268
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800269 Table table = Table::kUnknown;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700270 bool foundParts = false;
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700271 for (auto line : output) {
272 char* cline = (char*) line.c_str();
273 char* token = strtok(cline, kSgdiskToken);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700274 if (token == nullptr) continue;
275
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800276 if (!strcmp(token, "DISK")) {
277 const char* type = strtok(nullptr, kSgdiskToken);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800278 if (!strcmp(type, "mbr")) {
279 table = Table::kMbr;
280 } else if (!strcmp(type, "gpt")) {
281 table = Table::kGpt;
282 }
283 } else if (!strcmp(token, "PART")) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700284 foundParts = true;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800285 int i = strtol(strtok(nullptr, kSgdiskToken), nullptr, 10);
286 if (i <= 0 || i > maxMinors) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700287 LOG(WARNING) << mId << " is ignoring partition " << i
288 << " beyond max supported devices";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800289 continue;
290 }
291 dev_t partDevice = makedev(major(mDevice), minor(mDevice) + i);
292
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800293 if (table == Table::kMbr) {
294 const char* type = strtok(nullptr, kSgdiskToken);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800295
296 switch (strtol(type, nullptr, 16)) {
297 case 0x06: // FAT16
298 case 0x0b: // W95 FAT32 (LBA)
299 case 0x0c: // W95 FAT32 (LBA)
300 case 0x0e: // W95 FAT16 (LBA)
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700301 createPublicVolume(partDevice);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800302 break;
303 }
304 } else if (table == Table::kGpt) {
305 const char* typeGuid = strtok(nullptr, kSgdiskToken);
306 const char* partGuid = strtok(nullptr, kSgdiskToken);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800307
308 if (!strcasecmp(typeGuid, kGptBasicData)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700309 createPublicVolume(partDevice);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700310 } else if (!strcasecmp(typeGuid, kGptAndroidExpand)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700311 createPrivateVolume(partDevice, partGuid);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800312 }
313 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800314 }
315 }
316
317 // Ugly last ditch effort, treat entire disk as partition
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700318 if (table == Table::kUnknown || !foundParts) {
319 LOG(WARNING) << mId << " has unknown partition table; trying entire device";
Jeff Sharkey63123c02015-06-26 11:16:14 -0700320
321 std::string fsType;
322 std::string unused;
323 if (ReadMetadataUntrusted(mDevPath, fsType, unused, unused) == OK) {
324 createPublicVolume(mDevice);
325 } else {
326 LOG(WARNING) << mId << " failed to identify, giving up";
327 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800328 }
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700329
Jeff Sharkey613b26f2015-04-19 14:57:55 -0700330 notifyEvent(ResponseCode::DiskScanned);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700331 mJustPartitioned = false;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800332 return OK;
333}
334
Jeff Sharkey9c484982015-03-31 10:35:33 -0700335status_t Disk::unmountAll() {
336 for (auto vol : mVolumes) {
337 vol->unmount();
338 }
339 return OK;
340}
341
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800342status_t Disk::partitionPublic() {
343 // TODO: improve this code
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700344 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700345 mJustPartitioned = true;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800346
347 struct disk_info dinfo;
348 memset(&dinfo, 0, sizeof(dinfo));
349
350 if (!(dinfo.part_lst = (struct part_info *) malloc(
351 MAX_NUM_PARTS * sizeof(struct part_info)))) {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800352 return -1;
353 }
354
355 memset(dinfo.part_lst, 0, MAX_NUM_PARTS * sizeof(struct part_info));
356 dinfo.device = strdup(mDevPath.c_str());
357 dinfo.scheme = PART_SCHEME_MBR;
358 dinfo.sect_size = 512;
359 dinfo.skip_lba = 2048;
360 dinfo.num_lba = 0;
361 dinfo.num_parts = 1;
362
363 struct part_info *pinfo = &dinfo.part_lst[0];
364
365 pinfo->name = strdup("android_sdcard");
366 pinfo->flags |= PART_ACTIVE_FLAG;
367 pinfo->type = PC_PART_TYPE_FAT32;
368 pinfo->len_kb = -1;
369
370 int rc = apply_disk_config(&dinfo, 0);
371 if (rc) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700372 LOG(ERROR) << "Failed to apply disk configuration: " << rc;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800373 goto out;
374 }
375
376out:
377 free(pinfo->name);
378 free(dinfo.device);
379 free(dinfo.part_lst);
380
381 return rc;
382}
383
384status_t Disk::partitionPrivate() {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700385 return partitionMixed(0);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800386}
387
388status_t Disk::partitionMixed(int8_t ratio) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700389 int res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700390
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700391 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700392 mJustPartitioned = true;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700393
394 // First nuke any existing partition table
395 std::vector<std::string> cmd;
396 cmd.push_back(kSgdiskPath);
397 cmd.push_back("--zap-all");
398 cmd.push_back(mDevPath);
399
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700400 // Zap sometimes returns an error when it actually succeeded, so
401 // just log as warning and keep rolling forward.
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700402 if ((res = ForkExecvp(cmd)) != 0) {
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700403 LOG(WARNING) << "Failed to zap; status " << res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700404 }
405
406 // We've had some success above, so generate both the private partition
407 // GUID and encryption key and persist them.
408 std::string partGuidRaw;
409 std::string keyRaw;
410 if (ReadRandomBytes(16, partGuidRaw) || ReadRandomBytes(16, keyRaw)) {
411 LOG(ERROR) << "Failed to generate GUID or key";
412 return -EIO;
413 }
414
415 std::string partGuid;
416 StrToHex(partGuidRaw, partGuid);
417
418 if (!WriteStringToFile(keyRaw, BuildKeyPath(partGuid))) {
419 LOG(ERROR) << "Failed to persist key";
420 return -EIO;
421 } else {
422 LOG(DEBUG) << "Persisted key for GUID " << partGuid;
423 }
424
425 // Now let's build the new GPT table. We heavily rely on sgdisk to
426 // force optimal alignment on the created partitions.
427 cmd.clear();
428 cmd.push_back(kSgdiskPath);
429
430 // If requested, create a public partition first. Mixed-mode partitioning
431 // like this is an experimental feature.
432 if (ratio > 0) {
433 if (ratio < 10 || ratio > 90) {
434 LOG(ERROR) << "Mixed partition ratio must be between 10-90%";
435 return -EINVAL;
436 }
437
438 uint64_t splitMb = ((mSize / 100) * ratio) / 1024 / 1024;
439 cmd.push_back(StringPrintf("--new=0:0:+%" PRId64 "M", splitMb));
440 cmd.push_back(StringPrintf("--typecode=0:%s", kGptBasicData));
441 cmd.push_back("--change-name=0:shared");
442 }
443
444 // Define a metadata partition which is designed for future use; there
445 // should only be one of these per physical device, even if there are
446 // multiple private volumes.
447 cmd.push_back("--new=0:0:+16M");
448 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidMeta));
449 cmd.push_back("--change-name=0:android_meta");
450
451 // Define a single private partition filling the rest of disk.
452 cmd.push_back("--new=0:0:-0");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700453 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidExpand));
Jeff Sharkey9c484982015-03-31 10:35:33 -0700454 cmd.push_back(StringPrintf("--partition-guid=0:%s", partGuid.c_str()));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700455 cmd.push_back("--change-name=0:android_expand");
Jeff Sharkey9c484982015-03-31 10:35:33 -0700456
457 cmd.push_back(mDevPath);
458
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700459 if ((res = ForkExecvp(cmd)) != 0) {
460 LOG(ERROR) << "Failed to partition; status " << res;
461 return res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700462 }
463
464 return OK;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800465}
466
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700467void Disk::notifyEvent(int event) {
468 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(event,
469 getId().c_str(), false);
470}
471
472void Disk::notifyEvent(int event, const std::string& value) {
473 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(event,
474 StringPrintf("%s %s", getId().c_str(), value.c_str()).c_str(), false);
475}
476
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800477int Disk::getMaxMinors() {
478 // Figure out maximum partition devices supported
479 switch (major(mDevice)) {
Jeff Sharkeyf3ee2002015-04-19 15:55:42 -0700480 case kMajorBlockScsiA: case kMajorBlockScsiB: case kMajorBlockScsiC: case kMajorBlockScsiD:
481 case kMajorBlockScsiE: case kMajorBlockScsiF: case kMajorBlockScsiG: case kMajorBlockScsiH:
482 case kMajorBlockScsiI: case kMajorBlockScsiJ: case kMajorBlockScsiK: case kMajorBlockScsiL:
483 case kMajorBlockScsiM: case kMajorBlockScsiN: case kMajorBlockScsiO: case kMajorBlockScsiP: {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800484 // Per Documentation/devices.txt this is static
485 return 15;
486 }
487 case kMajorBlockMmc: {
488 // Per Documentation/devices.txt this is dynamic
489 std::string tmp;
490 if (!ReadFileToString(kSysfsMmcMaxMinors, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700491 LOG(ERROR) << "Failed to read max minors";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800492 return -errno;
493 }
494 return atoi(tmp.c_str());
495 }
496 }
497
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700498 LOG(ERROR) << "Unsupported block major type " << major(mDevice);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800499 return -ENOTSUP;
500}
501
502} // namespace vold
503} // namespace android