blob: 5b5d76945370e2b60210e648d66e6e6b1a6e544e [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
Jeff Sharkeyf3ee2002015-04-19 15:55:42 -070053static const unsigned int kMajorBlockScsiA = 8;
54static const unsigned int kMajorBlockScsiB = 65;
55static const unsigned int kMajorBlockScsiC = 66;
56static const unsigned int kMajorBlockScsiD = 67;
57static const unsigned int kMajorBlockScsiE = 68;
58static const unsigned int kMajorBlockScsiF = 69;
59static const unsigned int kMajorBlockScsiG = 70;
60static const unsigned int kMajorBlockScsiH = 71;
61static const unsigned int kMajorBlockScsiI = 128;
62static const unsigned int kMajorBlockScsiJ = 129;
63static const unsigned int kMajorBlockScsiK = 130;
64static const unsigned int kMajorBlockScsiL = 131;
65static const unsigned int kMajorBlockScsiM = 132;
66static const unsigned int kMajorBlockScsiN = 133;
67static const unsigned int kMajorBlockScsiO = 134;
68static const unsigned int kMajorBlockScsiP = 135;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080069static const unsigned int kMajorBlockMmc = 179;
70
71static const char* kGptBasicData = "EBD0A0A2-B9E5-4433-87C0-68B6B72699C7";
72static const char* kGptAndroidMeta = "19A710A2-B3CA-11E4-B026-10604B889DCF";
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070073static const char* kGptAndroidExpand = "193D1EA4-B3CA-11E4-B075-10604B889DCF";
Jeff Sharkeydeb24052015-03-02 21:01:40 -080074
Jeff Sharkey9c484982015-03-31 10:35:33 -070075static const char* kKeyPath = "/data/misc/vold";
76
Jeff Sharkeydeb24052015-03-02 21:01:40 -080077enum class Table {
78 kUnknown,
79 kMbr,
80 kGpt,
81};
82
Jeff Sharkeyce6a9132015-04-08 21:07:21 -070083Disk::Disk(const std::string& eventPath, dev_t device,
84 const std::string& nickname, int flags) :
85 mDevice(device), mSize(-1), mNickname(nickname), mFlags(flags), mCreated(
86 false), mJustPartitioned(false) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -070087 mId = StringPrintf("disk:%u,%u", major(device), minor(device));
88 mEventPath = eventPath;
Jeff Sharkeydeb24052015-03-02 21:01:40 -080089 mSysPath = StringPrintf("/sys/%s", eventPath.c_str());
Jeff Sharkey36801cc2015-03-13 16:09:20 -070090 mDevPath = StringPrintf("/dev/block/vold/%s", mId.c_str());
Jeff Sharkeydeb24052015-03-02 21:01:40 -080091 CreateDeviceNode(mDevPath, mDevice);
92}
93
94Disk::~Disk() {
Jeff Sharkey36801cc2015-03-13 16:09:20 -070095 CHECK(!mCreated);
Jeff Sharkeydeb24052015-03-02 21:01:40 -080096 DestroyDeviceNode(mDevPath);
97}
98
99std::shared_ptr<VolumeBase> Disk::findVolume(const std::string& id) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700100 for (auto vol : mVolumes) {
101 if (vol->getId() == id) {
102 return vol;
103 }
104 auto stackedVol = vol->findVolume(id);
105 if (stackedVol != nullptr) {
106 return stackedVol;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800107 }
108 }
109 return nullptr;
110}
111
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700112status_t Disk::create() {
113 CHECK(!mCreated);
114 mCreated = true;
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700115 notifyEvent(ResponseCode::DiskCreated, StringPrintf("%d", mFlags));
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700116 readMetadata();
117 readPartitions();
118 return OK;
119}
120
121status_t Disk::destroy() {
122 CHECK(mCreated);
123 destroyAllVolumes();
124 mCreated = false;
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700125 notifyEvent(ResponseCode::DiskDestroyed);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700126 return OK;
127}
128
Jeff Sharkey9c484982015-03-31 10:35:33 -0700129static std::string BuildKeyPath(const std::string& partGuid) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700130 return StringPrintf("%s/expand_%s.key", kKeyPath, partGuid.c_str());
Jeff Sharkey9c484982015-03-31 10:35:33 -0700131}
132
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700133void Disk::createPublicVolume(dev_t device) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700134 auto vol = std::shared_ptr<VolumeBase>(new PublicVolume(device));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700135 if (mJustPartitioned) {
136 LOG(DEBUG) << "Device just partitioned; silently formatting";
137 vol->setSilent(true);
138 vol->create();
139 vol->format();
140 vol->destroy();
141 vol->setSilent(false);
142 }
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700143
Jeff Sharkey9c484982015-03-31 10:35:33 -0700144 mVolumes.push_back(vol);
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700145 vol->setDiskId(getId());
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700146 vol->create();
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700147}
148
Jeff Sharkey9c484982015-03-31 10:35:33 -0700149void Disk::createPrivateVolume(dev_t device, const std::string& partGuid) {
150 std::string tmp;
151 std::string normalizedGuid;
152 if (HexToStr(partGuid, tmp)) {
153 LOG(WARNING) << "Invalid GUID " << partGuid;
154 return;
155 }
156 StrToHex(tmp, normalizedGuid);
157
158 std::string keyRaw;
159 if (!ReadFileToString(BuildKeyPath(normalizedGuid), &keyRaw)) {
160 PLOG(ERROR) << "Failed to load key for GUID " << normalizedGuid;
161 return;
162 }
163
164 LOG(DEBUG) << "Found key for GUID " << normalizedGuid;
165
166 auto vol = std::shared_ptr<VolumeBase>(new PrivateVolume(device, keyRaw));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700167 if (mJustPartitioned) {
168 LOG(DEBUG) << "Device just partitioned; silently formatting";
169 vol->setSilent(true);
170 vol->create();
171 vol->format();
172 vol->destroy();
173 vol->setSilent(false);
174 }
Jeff Sharkey9c484982015-03-31 10:35:33 -0700175
176 mVolumes.push_back(vol);
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700177 vol->setDiskId(getId());
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 Sharkeydeb24052015-03-02 21:01:40 -0800241 return OK;
242}
243
244status_t Disk::readPartitions() {
245 int8_t maxMinors = getMaxMinors();
246 if (maxMinors < 0) {
247 return -ENOTSUP;
248 }
249
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700250 destroyAllVolumes();
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800251
252 // Parse partition table
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700253
254 std::vector<std::string> cmd;
255 cmd.push_back(kSgdiskPath);
256 cmd.push_back("--android-dump");
257 cmd.push_back(mDevPath);
258
259 std::vector<std::string> output;
260 status_t res = ForkExecvp(cmd, output);
261 if (res != OK) {
262 LOG(WARNING) << "sgdisk failed to scan " << mDevPath;
Jeff Sharkeyba6747f2015-04-28 21:17:43 -0700263 notifyEvent(ResponseCode::DiskScanned);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700264 mJustPartitioned = false;
265 return res;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800266 }
267
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800268 Table table = Table::kUnknown;
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700269 bool foundParts = false;
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700270 for (auto line : output) {
271 char* cline = (char*) line.c_str();
272 char* token = strtok(cline, kSgdiskToken);
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700273 if (token == nullptr) continue;
274
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800275 if (!strcmp(token, "DISK")) {
276 const char* type = strtok(nullptr, kSgdiskToken);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800277 if (!strcmp(type, "mbr")) {
278 table = Table::kMbr;
279 } else if (!strcmp(type, "gpt")) {
280 table = Table::kGpt;
281 }
282 } else if (!strcmp(token, "PART")) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700283 foundParts = true;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800284 int i = strtol(strtok(nullptr, kSgdiskToken), nullptr, 10);
285 if (i <= 0 || i > maxMinors) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700286 LOG(WARNING) << mId << " is ignoring partition " << i
287 << " beyond max supported devices";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800288 continue;
289 }
290 dev_t partDevice = makedev(major(mDevice), minor(mDevice) + i);
291
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800292 if (table == Table::kMbr) {
293 const char* type = strtok(nullptr, kSgdiskToken);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800294
295 switch (strtol(type, nullptr, 16)) {
296 case 0x06: // FAT16
297 case 0x0b: // W95 FAT32 (LBA)
298 case 0x0c: // W95 FAT32 (LBA)
299 case 0x0e: // W95 FAT16 (LBA)
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700300 createPublicVolume(partDevice);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800301 break;
302 }
303 } else if (table == Table::kGpt) {
304 const char* typeGuid = strtok(nullptr, kSgdiskToken);
305 const char* partGuid = strtok(nullptr, kSgdiskToken);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800306
307 if (!strcasecmp(typeGuid, kGptBasicData)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700308 createPublicVolume(partDevice);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700309 } else if (!strcasecmp(typeGuid, kGptAndroidExpand)) {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700310 createPrivateVolume(partDevice, partGuid);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800311 }
312 }
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800313 }
314 }
315
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700316#if ENTIRE_DEVICE_FALLBACK
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800317 // Ugly last ditch effort, treat entire disk as partition
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700318 if (table == Table::kUnknown || !foundParts) {
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700319 // TODO: use blkid to confirm filesystem before doing this
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700320 LOG(WARNING) << mId << " has unknown partition table; trying entire device";
321 createPublicVolume(mDevice);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800322 }
Jeff Sharkeyf1b996d2015-04-17 17:35:20 -0700323#endif
324
Jeff Sharkey613b26f2015-04-19 14:57:55 -0700325 notifyEvent(ResponseCode::DiskScanned);
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700326 mJustPartitioned = false;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800327 return OK;
328}
329
Jeff Sharkey9c484982015-03-31 10:35:33 -0700330status_t Disk::unmountAll() {
331 for (auto vol : mVolumes) {
332 vol->unmount();
333 }
334 return OK;
335}
336
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800337status_t Disk::partitionPublic() {
338 // TODO: improve this code
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700339 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700340 mJustPartitioned = true;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800341
342 struct disk_info dinfo;
343 memset(&dinfo, 0, sizeof(dinfo));
344
345 if (!(dinfo.part_lst = (struct part_info *) malloc(
346 MAX_NUM_PARTS * sizeof(struct part_info)))) {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800347 return -1;
348 }
349
350 memset(dinfo.part_lst, 0, MAX_NUM_PARTS * sizeof(struct part_info));
351 dinfo.device = strdup(mDevPath.c_str());
352 dinfo.scheme = PART_SCHEME_MBR;
353 dinfo.sect_size = 512;
354 dinfo.skip_lba = 2048;
355 dinfo.num_lba = 0;
356 dinfo.num_parts = 1;
357
358 struct part_info *pinfo = &dinfo.part_lst[0];
359
360 pinfo->name = strdup("android_sdcard");
361 pinfo->flags |= PART_ACTIVE_FLAG;
362 pinfo->type = PC_PART_TYPE_FAT32;
363 pinfo->len_kb = -1;
364
365 int rc = apply_disk_config(&dinfo, 0);
366 if (rc) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700367 LOG(ERROR) << "Failed to apply disk configuration: " << rc;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800368 goto out;
369 }
370
371out:
372 free(pinfo->name);
373 free(dinfo.device);
374 free(dinfo.part_lst);
375
376 return rc;
377}
378
379status_t Disk::partitionPrivate() {
Jeff Sharkey9c484982015-03-31 10:35:33 -0700380 return partitionMixed(0);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800381}
382
383status_t Disk::partitionMixed(int8_t ratio) {
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700384 int res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700385
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700386 destroyAllVolumes();
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700387 mJustPartitioned = true;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700388
389 // First nuke any existing partition table
390 std::vector<std::string> cmd;
391 cmd.push_back(kSgdiskPath);
392 cmd.push_back("--zap-all");
393 cmd.push_back(mDevPath);
394
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700395 // Zap sometimes returns an error when it actually succeeded, so
396 // just log as warning and keep rolling forward.
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700397 if ((res = ForkExecvp(cmd)) != 0) {
Jeff Sharkeyffeb0072015-04-14 22:22:34 -0700398 LOG(WARNING) << "Failed to zap; status " << res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700399 }
400
401 // We've had some success above, so generate both the private partition
402 // GUID and encryption key and persist them.
403 std::string partGuidRaw;
404 std::string keyRaw;
405 if (ReadRandomBytes(16, partGuidRaw) || ReadRandomBytes(16, keyRaw)) {
406 LOG(ERROR) << "Failed to generate GUID or key";
407 return -EIO;
408 }
409
410 std::string partGuid;
411 StrToHex(partGuidRaw, partGuid);
412
413 if (!WriteStringToFile(keyRaw, BuildKeyPath(partGuid))) {
414 LOG(ERROR) << "Failed to persist key";
415 return -EIO;
416 } else {
417 LOG(DEBUG) << "Persisted key for GUID " << partGuid;
418 }
419
420 // Now let's build the new GPT table. We heavily rely on sgdisk to
421 // force optimal alignment on the created partitions.
422 cmd.clear();
423 cmd.push_back(kSgdiskPath);
424
425 // If requested, create a public partition first. Mixed-mode partitioning
426 // like this is an experimental feature.
427 if (ratio > 0) {
428 if (ratio < 10 || ratio > 90) {
429 LOG(ERROR) << "Mixed partition ratio must be between 10-90%";
430 return -EINVAL;
431 }
432
433 uint64_t splitMb = ((mSize / 100) * ratio) / 1024 / 1024;
434 cmd.push_back(StringPrintf("--new=0:0:+%" PRId64 "M", splitMb));
435 cmd.push_back(StringPrintf("--typecode=0:%s", kGptBasicData));
436 cmd.push_back("--change-name=0:shared");
437 }
438
439 // Define a metadata partition which is designed for future use; there
440 // should only be one of these per physical device, even if there are
441 // multiple private volumes.
442 cmd.push_back("--new=0:0:+16M");
443 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidMeta));
444 cmd.push_back("--change-name=0:android_meta");
445
446 // Define a single private partition filling the rest of disk.
447 cmd.push_back("--new=0:0:-0");
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700448 cmd.push_back(StringPrintf("--typecode=0:%s", kGptAndroidExpand));
Jeff Sharkey9c484982015-03-31 10:35:33 -0700449 cmd.push_back(StringPrintf("--partition-guid=0:%s", partGuid.c_str()));
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700450 cmd.push_back("--change-name=0:android_expand");
Jeff Sharkey9c484982015-03-31 10:35:33 -0700451
452 cmd.push_back(mDevPath);
453
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700454 if ((res = ForkExecvp(cmd)) != 0) {
455 LOG(ERROR) << "Failed to partition; status " << res;
456 return res;
Jeff Sharkey9c484982015-03-31 10:35:33 -0700457 }
458
459 return OK;
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800460}
461
Jeff Sharkeyce6a9132015-04-08 21:07:21 -0700462void Disk::notifyEvent(int event) {
463 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(event,
464 getId().c_str(), false);
465}
466
467void Disk::notifyEvent(int event, const std::string& value) {
468 VolumeManager::Instance()->getBroadcaster()->sendBroadcast(event,
469 StringPrintf("%s %s", getId().c_str(), value.c_str()).c_str(), false);
470}
471
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800472int Disk::getMaxMinors() {
473 // Figure out maximum partition devices supported
474 switch (major(mDevice)) {
Jeff Sharkeyf3ee2002015-04-19 15:55:42 -0700475 case kMajorBlockScsiA: case kMajorBlockScsiB: case kMajorBlockScsiC: case kMajorBlockScsiD:
476 case kMajorBlockScsiE: case kMajorBlockScsiF: case kMajorBlockScsiG: case kMajorBlockScsiH:
477 case kMajorBlockScsiI: case kMajorBlockScsiJ: case kMajorBlockScsiK: case kMajorBlockScsiL:
478 case kMajorBlockScsiM: case kMajorBlockScsiN: case kMajorBlockScsiO: case kMajorBlockScsiP: {
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800479 // Per Documentation/devices.txt this is static
480 return 15;
481 }
482 case kMajorBlockMmc: {
483 // Per Documentation/devices.txt this is dynamic
484 std::string tmp;
485 if (!ReadFileToString(kSysfsMmcMaxMinors, &tmp)) {
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700486 LOG(ERROR) << "Failed to read max minors";
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800487 return -errno;
488 }
489 return atoi(tmp.c_str());
490 }
491 }
492
Jeff Sharkey36801cc2015-03-13 16:09:20 -0700493 LOG(ERROR) << "Unsupported block major type " << major(mDevice);
Jeff Sharkeydeb24052015-03-02 21:01:40 -0800494 return -ENOTSUP;
495}
496
497} // namespace vold
498} // namespace android